Posted By: Anonymous
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.
[email protected]:/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:
[email protected]:/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
That means, when run in background (
-d), the shell exits immediately.
In that case, you don’t need any additional command and this is enough:
docker run -t -d centos
[email protected]:~$ d ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4a50fd9e9189 centos "/bin/bash" 8 seconds ago Up 2 seconds wonderful_wright
docker run -t -d alpine/gitdoes 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
-dis 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
-doption. 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/nullto 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
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).