在 docker 撰写中无需"sleep infinity"命令即可启动远程容器



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

TL;博士

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

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

.. 但是使用sleep infinity命令,我启动的 Flask 服务器无法转发它的端口,appPort来自devsettings.json

相关 GitHub 问题:

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

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

设置

VS 代码容器的图像:Docker 组合中的 Docker

Dockerfile中的图像:ubuntu:仿生

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.yml中公开端口。

执行此操作的语法是:

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

在尝试使用命令启动 jupyter 笔记本时遇到类似的情况,找到了一种解决方法,最后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"

相关内容

  • 没有找到相关文章

最新更新