“docker run -d"后Docker容器会自动停止 [英] Docker container will automatically stop after "docker run -d"

查看:166
本文介绍了“docker run -d"后Docker容器会自动停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我目前阅读的教程,使用docker run -d"将从图像启动容器,容器将在后台运行.这是它的样子,我们可以看到我们已经有了容器 id.

root@docker:/home/root# docker run -d centos605e3928cdddb844526bab691af51d0c9262e0a1fc3d41de3f59be1a58e1bd1d

但是如果我运行docker ps",则什么也没有返回.

所以我尝试了docker ps -a",我可以看到容器已经退出:

root@docker:/home/root# docker ps -a容器 ID 图像命令创建状态端口名称605e3928cddd centos:latest "/bin/bash" 31 分钟前退出 (0) 31 分钟前 kickass_swartz

我做错了什么吗?如何解决此问题?

解决方案

centos dockerfile 有一个默认命令 bash.

这意味着,当在后台运行时 (-d),shell 会立即退出.

2017 年更新

最新版本的 docker 授权在 分离模式下运行容器 前景模式(-t-i-it)

在那种情况下,您不需要任何额外的命令,这就足够了:

docker run -t -d centos

bash 将在后台等待.
这最初是在 kalyani-chaudhari答案 并在球衣豆答案.

vonc@voncvb:~$ d ps -a容器 ID 图像命令创建状态端口名称4a50fd9e9189 centos /bin/bash"8 秒前向上 2 秒 Wonder_wright

请注意,对于 alpineMarinos An 报告 在评论中:

<块引用>

docker run -t -d alpine/git 不保持进程.
必须做:docker run --entrypoint "/bin/sh";-it alpine/git


原始答案 (2015)

这篇文章中所述:

<块引用>

建议使用 -d 代替 docker run -i -t image your-command 运行,因为你可以只用一个命令运行你的容器,而你不需要不需要通过点击 Ctrl + P + Q 来分离容器的终端.

<块引用>

但是,-d 选项有问题.除非命令继续在前台运行,否则您的容器会立即停止.
Docker 需要您的命令在前台继续运行.否则,它会认为您的应用程序停止并关闭容器.

<块引用>

问题是某些应用程序没有在前台运行.我们怎样才能让它更容易?

<块引用>

在这种情况下,您可以将 tail -f/dev/null 添加到您的命令中.
通过这样做,即使您的主命令在后台运行,您的容器也不会停止,因为 tail 一直在前台运行.

所以这行得通:

docker run -d centos tail -f/dev/null

或者在 Dockerfile 中:

ENTRYPOINT [尾"]CMD [-f",/dev/null"]

docker ps 会显示 centos 容器仍在运行.

从那里,您可以附加到它或从它分离(或 docker exec一些命令).

According to tutorial I read so far, use "docker run -d" will start a container from image, and the container will run in background. This is how it looks like, we can see we already have container id.

root@docker:/home/root# docker run -d centos
605e3928cdddb844526bab691af51d0c9262e0a1fc3d41de3f59be1a58e1bd1d

But if I ran "docker ps", nothing was returned.

So I tried "docker ps -a", I can see container already exited:

root@docker:/home/root# docker ps -a
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                         PORTS               NAMES
605e3928cddd        centos:latest         "/bin/bash"         31 minutes ago      Exited (0) 31 minutes ago                          kickass_swartz

Anything I did wrong? How can I troubleshoot this issue?

解决方案

The centos dockerfile has a default command bash.

That means, when run in background (-d), the shell exits immediately.

Update 2017

More recent versions of docker authorize to run a container both in detached mode and in foreground mode (-t, -i or -it)

In that case, you don't need any additional command and this is enough:

docker run -t -d centos

The bash will wait in the background.
That was initially reported in kalyani-chaudhari's answer and detailed in jersey bean's answer.

vonc@voncvb:~$ d ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4a50fd9e9189        centos              "/bin/bash"         8 seconds ago       Up 2 seconds                            wonderful_wright

Note that for alpine, Marinos An reports in the comments:

docker run -t -d alpine/git does not keep the process up.
Had to do: docker run --entrypoint "/bin/sh" -it alpine/git


Original answer (2015)

As mentioned in this article:

Instead of running with docker run -i -t image your-command, using -d is recommended because you can run your container with just one command and you don’t need to detach terminal of container by hitting Ctrl + P + Q.

However, there is a problem with -d option. Your container immediately stops unless the commands keep running in foreground.
Docker requires your command to keep running in the foreground. Otherwise, it thinks that your applications stops and shutdown the container.

The problem is that some application does not run in the foreground. How can we make it easier?

In this situation, you can add tail -f /dev/null to your command.
By doing this, even if your main command runs in the background, your container doesn’t stop because tail is keep running in the foreground.

So this would work:

docker run -d centos tail -f /dev/null

Or in Dockerfile:

ENTRYPOINT ["tail"]
CMD ["-f","/dev/null"]

A docker ps would show the centos container still running.

From there, you can attach to it or detach from it (or docker exec some commands).

这篇关于“docker run -d"后Docker容器会自动停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆