Docker 容器在启动后立即关闭

Docker container closes immediately after start

我想在我的 Windows 10 机器上为 VSCode 设置学习开发容器,但是当我在 VSCode 中按“打开容器中的文件夹”时,出现错误:

[2020-11-24T10:21:22.963Z] [PID 8208] 
[2020-11-24T10:21:23.070Z] [PID 8208] [8794 ms] Start: Run: docker ps -q -a --filter label=com.docker.compose.project=lecture2 --filter label=com.docker.compose.service=development
[2020-11-24T10:21:23.762Z] [PID 8208] [9486 ms] Start: Run: docker inspect --type container 03d485c0d0b0
[2020-11-24T10:21:24.630Z] [PID 8208] [10354 ms] Start: Inspecting container
[2020-11-24T10:21:24.631Z] [PID 8208] [10355 ms] Start: Run: docker inspect --type container 03d485c0d0b08ae5af7bc81124f9205d933ce77441829cfcf176a6dd767ab291
[2020-11-24T10:21:25.656Z] [PID 8208] [11380 ms] Start: Run: docker exec -i -u root -e VSCODE_REMOTE_CONTAINERS_SESSION=663b838f-c06e-4178-ae10-5f48efb218811606213272822 03d485c0d0b08ae5af7bc81124f9205d933ce77441829cfcf176a6dd767ab291 /bin/sh
[2020-11-24T10:21:25.678Z] [PID 8208] [11402 ms] Start: Run in container: uname -m
[2020-11-24T10:21:26.558Z] [PID 8208] [12282 ms] Start: Run in container: cat /etc/passwd
[2020-11-24T10:21:26.558Z] [PID 8208] [12282 ms] Stdin closed!
[2020-11-24T10:21:26.570Z] [PID 8208] [12294 ms] Shell server terminated (code: 1, signal: null)

Error response from daemon: Container 03d485c0d0b08ae5af7bc81124f9205d933ce77441829cfcf176a6dd767ab291 is not running

如果我只发出命令:

docker-compose up

容器立即启动和停止,控制台没有任何错误。

我的设置是这样的: Dockerfile:

FROM erlang:latest

WORKDIR /project

CMD tail -f /dev/null

docker-compose.yml:

version: '3'
services:
  development:
    build:
      context: .
    volumes:
      - ./:/project
      - build:/project/_build
      - deps:/project/deps

volumes:
  build:
  deps:

.devcontainer/devcontainer.json:

{
    "name": "Erlang dev container",

    "service": "development",
  
    "context": "..",

    "dockerComposeFile": "..\docker-compose.yml",

    "workspaceFolder": "/project",

    "settings": { 
        "terminal.integrated.shell.linux": "/bin/bash"
    },

    "extensions": []  
}

为什么不起作用?

PS。 我什至像这样更改了我的 Dockerfile:

FROM erlang:latest

WORKDIR /project

COPY . .

还有我的docker-compose.yml:

version: '3'
services:
  development:
    build:
      context: .

还是不行。

PSPS。 但!之后我在 devcontainer.json 行中更改:

"dockerComposeFile": "..\docker-compose.yml",

至:

"dockerFile": "..\Dockerfile",

然后VSCode才在容器中打开了我的文件夹。 但我不想 COPY 。 .在我的 Dockerfile 中,我想将我项目的文件夹作为卷添加到我的 docker-compose.yml 文件中。我该如何管理?

我通过将 tty:true 添加到我的 docker-compose 服务定义中解决了我的问题,如下所示:

version: '3'
services:
  development:
    build:
      context: .
    tty: true
    volumes: 
      - ./:/project