启动服务后如何保持Docker容器运行? [英] How to Keep Docker Container Running After Starting Services?
问题描述
我看过一些教程似乎做了同样的事情我想做,但由于某些原因,我的Docker容器退出。基本上,我在Docker容器内设置了一个Web服务器和几个守护进程。我通过一个名为 run-all.sh
的bash脚本来做最后的部分,这是通过我的Docker文件中的CMD运行的。 run-all.sh
看起来像这样:
I've seen a bunch of tutorials that seem do the same thing I'm trying to do, but for some reason my Docker containers exit. Basically, I'm setting up a web-server and a few daemons inside a Docker container. I do the final parts of this through a bash script called run-all.sh
that I run through CMD in my Dockerfile. run-all.sh
looks like this:
service supervisor start
service nginx start
我在Dockerfile里面启动它,如下所示:
And I start it inside of my Dockerfile as follows:
CMD ["sh", "/root/credentialize_and_run.sh"]
我可以看到,当我手动运行东西(即使用-i -t / bin / bash进行映像)时,所有服务都正常启动,一切都看起来像运行图像一样正常运行,但是一旦完成了我的进程启动,它就会退出。我想让进程无限期地运行,据我所知,容器必须继续运行才能发生这种情况。然而,当我运行 docker ps -a
时,我看到:
I can see that the services all start up correctly when I run things manually (i.e. getting on to the image with -i -t /bin/bash), and everything looks like it runs correctly when I run the image, but it exits once it finishes starting up my processes. I'd like the processes to run indefinitely, and as far as I understand, the container has to keep running for this to happen. Nevertheless, when I run docker ps -a
, I see:
➜ docker_test docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7706edc4189 some_name/some_repo:blah "sh /root/run-all.sh 8 minutes ago Exited (0) 8 minutes ago grave_jones
什么给了?为什么会退出?我知道我可以在我的最后放一个循环bash脚本,以保持它,但是什么是正确的方法来保持它退出?
What gives? Why is it exiting? I know I could just put a while loop at the end of my bash script to keep it up, but what's the right way to keep it from exiting?
推荐答案
我只是有同样的问题我发现如果您正在使用 -t
和 -d
标志运行容器,则它将继续运行。 / p>
I just had the same problem and I found out that if you are running your container with the -t
and -d
flag, it keeps running.
docker run -td <image>
这是标志的作用(根据 docker run --help
):
Here is what the flags do (according to docker run --help
):
-d, --detach=false Run container in background and print container ID
-t, --tty=false Allocate a pseudo-TTY
最重要的一个是 -t
。 -d
只需让您在后台运行容器。
The most important one is the -t
flag. -d
just lets you run the container in the background.
这篇关于启动服务后如何保持Docker容器运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!