为什么我需要 tty: true 在 docker-compose.yml 而其他图像不需要? [英] why do i need tty: true in docker-compose.yml and other images do not?
问题描述
我一直在寻找答案,但一直没有找到,在继续测试之前我需要了解.
I've been looking for the answer for a while, but I haven't found it and I need to understand before I go ahead with my tests.
我正在通过安装 bash 创建一个基于 Alpine 的镜像,如下图所示:
I am creating an image based on Alpine by installing bash as in the following image:
FROM alpine:3.12
RUN apk add --no-cache --upgrade bash rsync gzip
&& rm -rf /var/cache/apk/*
COPY ./docker/backup/hello.sh /hello.sh
RUN mkdir /backup
&& chmod u+x /hello.sh
WORKDIR /backup
ENTRYPOINT ["sh","/hello.sh"]
CMD ["/bin/bash"]
hello.sh
#!/bin/sh
echo "=> Hello Word"
echo "$@"
exec "$@"
我第一次尝试使用 bash 时无法使用以下命令访问:
The first time I tried bash I could not access with the following command:
docker-compose exec myalpine bash
但是搜索是否找到了答案,我不得不放入我的 docker-compose.yml tty: true
,并且我已经能够访问 myalpine
容器启动命令后的 shell docker-compose up -d
But searching if I found the answer, I had to put in my docker-compose.yml tty: true
, and I already was able to access the myalpine
container shell after launching the command docker-compose up -d
导致我的 docker-compose.yml 的一部分如下
Resulting in a part of my docker-compose.yml as follows
services:
myalpine:
build:
context: ./
dockerfile: ./docker/backup/Dockerfile
args:
- DOCKER_ENV=${DOCKER_ENV}
restart: unless-stopped
tty: true
container_name: ${PROJECT_NAME}-files
volumes:
- appdata:/app
- ./data/app/backup:/backup
mysql:
build:
context: ./
dockerfile: ./docker/mysql/Dockerfile
args:
- MYSQL_VERSION=${MYSQL_VERSION}
- DOCKER_ENV=${DOCKER_ENV}
restart: always
container_name: ${PROJECT_NAME}-mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- dbdata:/var/lib/mysql
networks:
- db
现在我的问题是为什么在我的 docker-compose 的其他服务中,比如 mysql
,我可以在不添加 tty:true
的情况下访问 bash?
And now my question is why in other services of my docker-compose like mysql
, I can access the bash without adding tty:true
?
示例:
docker-compose exec mysql bash
我可以在没有将 tty:true
添加到 docker-compose.yml 的情况下访问,所以 Alpine 的图像中肯定有一些我不理解并且想理解的东西.
I can access without having added tty:true
to the docker-compose.yml, so there must be something in Alpine's image that I don't understand and would like to understand.
推荐答案
我已经使用 dockerfile 和 docker-compose.yaml 的缩减版本复制了您的示例,并在运行 docker-compose up代码>.首先,我在没有附加
tty
的情况下运行,所以我有以下内容:
I had reproduced your example with a cut-down version of the dockerfile and the docker-compose.yaml, and after running docker-compose up
. First I ran without a tty
attached, so I have the following:
~$ docker-compose up
...
...
WARNING: Image for service myalpine was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating test-fies ... done
Attaching to test-fies
test-fies | => Hello Word
test-fies | /bin/bash
~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a72817b0e28 tty_up_myalpine "sh /hello.sh /bin/b…" 5 minutes ago Restarting (0) Less than a second ago myalpine-fies
如您所见,容器一直在重启.这样做的原因是 hello.sh
作为入口点将接收命令 /bin/bash
并将执行 bash.这将尝试创建一个 interactive shell
但由于没有 tty
,shell 的创建失败并且容器停止.因为容器被标记为restart:unless-stopped
,它将处于不断重启的循环中.
As you can see, the container is restarting all the time. Reason for this is that the hello.sh
as an entrypoint will receive the command /bin/bash
and will execute the bash. This will try to create an interactive shell
but since there is no tty
the creation of the shell fails and the container is stopped. Because the container is marked restart: unless-stopped
it will be in a constant loop of restarting.
由于容器未运行,您无法执行docker-compose exec myalpine bash
Since the container is not running, you are not able to execute docker-compose exec myalpine bash
一旦您向容器中添加了 tty
,bash
将能够创建交互式会话并启动容器.
Once you add a tty
to the container, bash
will be able to create interactive session and the container will be started.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
745f5139f510 tty_up_myalpine "sh /hello.sh /bin/b…" 10 seconds ago Up 9 seconds myalpine-fies
mysql
不是这种情况的原因是该映像最终启动了一个 daemon
进程,该进程是一个与 分离的非交互式进程>tty
.
Reason why this is not a case with mysql
is that this image ends up initiating a daemon
process which is a non-interactive process that is detached from a tty
.
这篇关于为什么我需要 tty: true 在 docker-compose.yml 而其他图像不需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!