1

我想仅使用 docker-compose.yml 以交互模式输入一个带有逗号的 docker 容器 /bin/bash 。这里有一个关于堆栈溢出的类似问题:Interactive shell using Docker Compose Answers provided there didn't work。这就是我的 docker-compose.yml 的样子:

version: "3"
services:
  server:
    image: golang:1.11.1
    volumes:
      - './server:/go'
    ports:
      - '8080:8080'

    command: '-ti'
    entrypoint:
      - '/bin/bash'

这是我的控制台输入和输出:

[bluebrown@firefly gowild]$ docker-compose up --build
Recreating gowild_server_1 ... done
Attaching to gowild_server_1
server_1  | bash: cannot set terminal process group (-1): Inappropriate ioctl for device
server_1  | bash: no job control in this shell
server_1  | root@d5884893075a:/go# exit
gowild_server_1 exited with code 0

阅读上述帖子,我当然也尝试过替换:

command: '-ti'

对于这两行:

stdin_open: true
tty: true

但是在执行此操作时,docker compose 在附加时会卡住:

[bluebrown@firefly gowild]$ docker-compose up --build
Recreating gowild_server_1 ... done
Attaching to gowild_server_1

没有任何事情发生。没有错误和退出,也没有“完成”消息。

当尝试它sh而不是bash它时,它会说以下内容command: '-it

server_1  | /bin/sh: 0: Illegal option -t

并且在替换它时也会像 bash 一样卡住。

请注意,我可以在没有命令和入口点的情况下构建和运行服务器,只需使用以下命令:

docker-compose up
docker-compose run --service-ports server

我的问题仍然是如何使用 docker-compose 和入口点来完成它,所以它只能用它来完成docker-compose up

更新:我正在使用 Linux manjaro

4

2 回答 2

1

我认为这里的问题是 docker-compose 可能会运行多个容器......所以它通常不能附加到特定容器的标准输入。显然,在您只有一个容器的情况下,应该没有混淆,因此它可以做到这一点 - 但是如果您稍后将另一个容器添加到 yml 中,这会改变行为,这将是令人困惑的并且在那时基本上是一个错误。

所以.. compose 不是这项工作的正确工具。

I have a little bash script called dockersh which makes it easy to drop into a shell of any docker image:

#!/bin/sh

IMAGE=$1
shift

# sanitise the name a little
NAME=$(echo $IMAGE | tr '/:' '-')

# generate a random ID in case we have multiple running
ID=$(env LC_CTYPE=C tr -dc "a-z0-9" < /dev/urandom | head -c 10)

docker run --rm -ti \
  --name $NAME-$ID \
  -v $PWD:/mnt/$(basename $PWD) \
  -v $HOME/.ssh:/root/.ssh \
  $IMAGE \
  "$@"

The .ssh mount is useful for cloning git repos etc. On linux, you could alternatively do that by mounting $SSH_AUTH_SOCK into the container, but that doesn't work on mac (at least for me).

For your case above, you can run as:

dockersh golang:1.11.1 bash

Although you might want to make this a little less generic for yourself and expose ports and mount into /go etc.

于 2018-10-18T08:58:40.443 回答
1

In that particular use case the solution should be like below. The reason for this is usually /bin/bash is used with -ti to enter a shell inside the container.

The same thing can be done with compose by using the run command with a particular service. Note that I am exposing the service ports too.

docker-compose run --service-ports server bash

https://docs.docker.com/compose/reference/run/

If the container is already running then exec should be enough.

于 2021-04-11T21:04:31.790 回答