【问题标题】:Start remote container without "sleep infinity" command in docker compose在 docker compose 中启动没有“sleep infinity”命令的远程容器
【发布时间】:2019-12-16 05:00:57
【问题描述】:

在 VS Code 中使用远程容器时,我想在不使用默认 sleep infinity 命令的情况下启动一个 Ubuntu 容器(稍后启动一个 Flask 服务器),以便稍后从主机访问该容器。

TL;DR

如果我从docker-compose.yml 中删除sleep infinity 命令,容器将无法启动,例如。 G。

Run: docker exec 12d95e1f14877bc4af7fa62e59f81b7ebf0f8983aa357eb077a09bf1951e4370 test -d /root/.vscode-server
Error response from daemon: Container 12d95e1f14877bc4af7fa62e59f81b7ebf0f8983aa357eb077a09bf1951e4370 is not running

.. 但是使用 sleep infinity 命令,我启动的 Flask 服务器无法从 devsettings.json 转发带有 appPort 的端口。

相关的 GitHub 问题:

https://github.com/microsoft/vscode-remote-release/issues/319

https://github.com/microsoft/vscode-remote-release/issues/259

设置

VS Code 容器的镜像:Docker compose 中的 Docker

Dockerfile 中的图像:ubuntu:bionic

Dockerfile

FROM ubuntu:bionic

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# Docker Compose version
ARG COMPOSE_VERSION=1.24.0
...
# Configure apt and install packages
RUN apt-get update \
    && apt-get -y install --no-install-recommends apt-utils 2>&1 \
    ....

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog

我尝试在Dockerfile中添加ENTRYPOINT ["bash", "/bin/bash"],但是没有效果。

devsettings.json

{
    "name": "Docker in Docker Compose",
    "dockerComposeFile": "docker-compose.yml",
    "service": "my-service",
    "workspaceFolder": "/workspace",

    // default command is "sleep infinity", can't use that as Flask should be accessible
    "overrideCommand": false,

    "appPort": ["5000:5000"],

    "extensions": [
        "peterjausovec.vscode-docker",
        "ms-python.python"
    ],
    "settings": {
        "remote.extensionKind": {
            "peterjausovec.vscode-docker": "workspace"
        }
    }
}

docker-compose.yml

version: '3'
services:
  my-service:
    build: 
      context: .
      dockerfile: Dockerfile

    volumes:
      # Update this to wherever you want VS Code to mount the folder of your project
      - ..:/workspace

      # This lets you avoid setting up Git again in the container
      - ~/.gitconfig:/root/.gitconfig

      # Forwards the local Docker socket to the container.
      - /var/run/docker.sock:/var/run/docker.sock 

    # Overrides default command so things don't shut down after the process ends.
    command: sleep infinity

【问题讨论】:

  • 我没有看到您提到要在您的 docker-compose 文件中发布的任何端口。这可能是原因。
  • @7_R3X 一直以来我都认为devsettings.jsonappPort 可以做到这一点,但如果再加上docker-compose.yml,显然不会。谢谢。随意创建一个答案。

标签: linux docker visual-studio-code docker-compose vscode-remote


【解决方案1】:

它不起作用的原因是你没有暴露docker-compose.yml中的端口。

这样做的语法是:

services:
  service_name:
    ports:
      - "8080:80"

Source

【讨论】:

  • 我仍然不知道为什么devsettings.json 中的appPort 没有为“Docker in Docker compose”-image 公开docker-compose.yml 的端口,因为它通常为单个 Dockerfile,但这解决了我的问题。
【解决方案2】:

在尝试使用命令启动jupyter notebook时遇到类似情况,找到了解决方法,将sleep inifinity放在最后。

./docker-compose.yml

....
ports:
      - 8888:8888    
command: bash -c "jupyter notebook --ip 0.0.0.0 --port 8888 --no-browser --allow-root && sleep infinity"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2018-04-10
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多