根据我目前阅读的教程,使用"docker run -d"将从image启动一个容器,并且容器将在后台运行。这就是它的样子,我们已经有了container id。

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

但如果我运行“docker ps”,什么都没有返回。

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

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

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


当前回答

我也面临着同样的问题,但方式不同。当我创建docker容器时。它会自动停止正在后台运行的未使用的容器。有时它还会停止正在使用的容器。 在我的情况下,这是因为docker的许可。Sock文件,它早先有。 你要做的是:-

Install docker again.(As i work on ubuntu i install it from here) Run the command to change the permissions. sudo chmod 666 /var/run/docker.sock Install docker-compose (this is optional as I have compose file to create many containers together) sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose check for the version to ensure that I have the latest one and not get problem with some deprications. Then I run the docker container build.

其他回答

我也面临着同样的问题,但方式不同。当我创建docker容器时。它会自动停止正在后台运行的未使用的容器。有时它还会停止正在使用的容器。 在我的情况下,这是因为docker的许可。Sock文件,它早先有。 你要做的是:-

Install docker again.(As i work on ubuntu i install it from here) Run the command to change the permissions. sudo chmod 666 /var/run/docker.sock Install docker-compose (this is optional as I have compose file to create many containers together) sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose check for the version to ensure that I have the latest one and not get problem with some deprications. Then I run the docker container build.

我已经在下面的帖子中解释了这个问题。

使用“exit”后如何保留docker alpine容器?

根据这个答案,添加-t标志将防止容器在后台运行时退出。然后使用docker exec -i -t <image> /bin/bash进入shell提示符。

docker run -t -d <image> <command>

-t选项似乎没有很好地记录,尽管帮助说它“分配了一个伪tty”。

你可以通过以下任何一种方式实现你想要的:

docker run -t -d <image-name>

or

docker run -i -d <image-name>

or

docker run -it -d <image-name>

其他答案所建议的命令参数(即tail -f /dev/null)是完全可选的,并不是让容器保持在后台运行所必需的。

还要注意Docker文档建议组合-i和-t选项会导致它像shell一样运行。

See:

https://docs.docker.com/engine/reference/run/#foreground

你可以简单地使用:

docker container run -d -it <container name or id> /bin/bash