为什么我需要tty:在docker-compose.yml中为true,而其他映像则不需要? [英] 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
容器启动命令 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
,我可以访问bash而无需添加 tty:true
?
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
我无需在docker-compose.yml中添加 tty:true
即可访问,因此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 <之后/code>.首先,我没有附加
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
,因此外壳程序创建失败并且容器已停止.由于容器标记为 restart:除非已停止
,所以它将处于不断重启的循环中.
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:在docker-compose.yml中为true,而其他映像则不需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!