如何在 bash 中启动 Docker-ubuntu 容器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43419500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you start a Docker-ubuntu container into bash?
提问by Andrew
The answers from this questiondo not work.
这个问题的答案不起作用。
The docker container always exits before I can attach
or won't accept the -t
flag. I could list all of the commands I've tried, but it's a combination of start
exec
attach
with various -it
flags and /bin/bash
.
docker 容器总是在我可以attach
或不会接受-t
标志之前退出。我可以列出我尝试过的所有命令,但它是start
exec
attach
各种-it
标志和/bin/bash
.
How do I start an existing container into bash? Why is this so difficult? Is this an "improper" use of Docker?
如何将现有容器启动到 bash 中?为什么这如此困难?这是 Docker 的“不当”使用吗?
EDITS:
I created the container with docker run ubuntu
. The information about the container: 60b93bda690f ubuntu "/bin/bash" About an hour ago Exited (0) 50 minutes ago ecstatic_euclid
编辑:我用docker run ubuntu
. 容器的相关资料:60b93bda690f ubuntu "/bin/bash" About an hour ago Exited (0) 50 minutes ago ecstatic_euclid
回答by shizhz
First of all, a container is not a virtual machine. A container is an isolation environment for running a process. The life-cycle of the container is bound to the process running inside it. When the process exits, the container also exits, and the isolation environment is gone. The meaning of "attach to container" or "enter an container" actually means you go inside the isolation environment of the running process, so if your process has been exited, your container has also been exited, thus there's no container for you to attach
or enter
. So the command of docker attach
, docker exec
are target at runningcontainer.
首先,容器不是虚拟机。容器是运行进程的隔离环境。容器的生命周期与其内部运行的进程绑定在一起。当进程退出时,容器也退出,隔离环境就没了。“附加到容器”或“进入容器”的意思其实就是进入了运行进程的隔离环境中,所以如果你的进程已经退出,你的容器也已经退出,因此没有容器供你attach
或enter
. 所以的命令docker attach
,docker exec
是在目标上运行的容器。
Which process will be started when you docker run
is configured in a Dockerfile
and built into a docker image. Take image ubuntu
as an example, if you run docker inspect ubuntu
, you'll find the following configs in the output:
当您docker run
在 a 中进行配置Dockerfile
并构建到 docker 映像中时,将启动哪个进程。以 imageubuntu
为例,如果你运行docker inspect ubuntu
,你会在输出中找到以下配置:
"Cmd": ["/bin/bash"]
which means the process got started when you run docker run ubuntu
is /bin/bash
, but you're not in an interactive mode and does not allocate a tty to it, so the process exited immediately and the container exited. That's why you have no way to enter the container again.
这意味着当您运行docker run ubuntu
is时该进程已启动/bin/bash
,但您未处于交互模式且未为其分配 tty,因此该进程立即退出并且容器退出。这就是为什么您无法再次进入容器的原因。
To start a container and enter bash
, just try:
要启动容器并输入bash
,请尝试:
docker run -it ubuntu
Then you'll be brought into the container shell. If you open another terminal and docker ps
, you'll find the container is running and you can docker attach
to it or docker exec -it <container_id> bash
to enter it again.
然后您将被带入容器外壳。如果您打开另一个终端并docker ps
,您会发现容器正在运行,您可以docker attach
访问它或docker exec -it <container_id> bash
再次输入它。
You can also refer to this linkfor more info.
您也可以参考此链接了解更多信息。
回答by Scott Stensland
Here is a very simple Dockerfile with instructions as comments ... give it a whirl and see if you then have a working container you can exec login to
这是一个非常简单的 Dockerfile,带有作为注释的说明......试一试,看看你是否有一个可以执行登录的工作容器
FROM ubuntu:19.04
ENV TERM linux
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update
RUN apt-get install -y
CMD ["/bin/bash"]
# ... save this file as Dockerfile then in same dir issue following
#
# docker build --tag stens_ubuntu . # creates image stens_ubuntu
#
# docker run -d stens_ubuntu sleep infinity # launches container
#
# docker ps # show running containers
#
#
# ... find CONTAINER ID from above and put into something like this
#
# docker exec -ti $( docker ps | grep stens_ubuntu | cut -d' ' -f1 ) bash # login to running container
# docker exec -ti 3cea1993ed28 bash # login to running container using sample containerId
#
A container will exit normally when it has no work to do ... if you give it no work it will exit immediately upon launch for this reason ... typically the last command of your Dockerfile is the execution of some flavor of a server which stays alive due to an internal event loop and in so doing keeps alive its enclosing container ... short of that you can mention a server executable as the final parameter of your call to
容器在没有工作要做时会正常退出......如果你不给它任何工作,它会因为这个原因在启动时立即退出......通常你的 Dockerfile 的最后一个命令是执行某种风格的服务器由于内部事件循环而保持活动状态,并且这样做使其封闭容器保持活动状态......除此之外,您可以提到服务器可执行文件作为您调用的最终参数
docker run -d my-image-name my-server-executable