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?
You can accomplish what you want with either:
or
or
The command parameter as suggested by other answers (i.e. tail -f /dev/null) is completely optional, and is NOT required to get your container to stay running in the background.
Also note the Docker documentation suggests that combining -i and -t options will cause it to behave like a shell.
See:
https://docs.docker.com/engine/reference/run/#foreground
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:
The bash will wait in the background.
That was initially reported in kalyani-chaudhari's answer and detailed in jersey bean's answer.
Original answer (2015)
As mentioned in this article:
So this would work:
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).execute command as follows :
if you want to specify port then command as below:
verify the running container using following command:
Background
A Docker container runs a process (the "command" or "entrypoint") that keeps it alive. The container will continue to run as long as the command continues to run.
In your case, the command (
/bin/bash
, by default, oncentos:latest
) is exiting immediately (as bash does when it's not connected to a terminal and has nothing to run).Normally, when you run a container in daemon mode (with
-d
), the container is running some sort of daemon process (likehttpd
). In this case, as long as the httpd daemon is running, the container will remain alive.What you appear to be trying to do is to keep the container alive without a daemon process running inside the container. This is somewhat strange (because the container isn't doing anything useful until you interact with it, perhaps with
docker exec
), but there are certain cases where it might make sense to do something like this.(Did you mean to get to a bash prompt inside the container? That's easy!
docker run -it centos:latest
)Solution
A simple way to keep a container alive in daemon mode indefinitely is to run
sleep infinity
as the container's command. This does not rely doing strange things like allocating a TTY in daemon mode. Although it does rely on doing strange things like usingsleep
as your primary command.Alternative Solution
As indicated by cjsimon, the
-t
option allocates a "pseudo-tty". This tricks bash into continuing to run indefinitely because it thinks it is connected to an interactive TTY (even though you have no way to interact with that particular TTY if you don't pass-i
). Anyway, this should do the trick too:Not 100% sure whether
-t
will produce other weird interactions; maybe leave a comment below if it does.Maybe it is just me but on CentOS 7.3.1611 and Docker 1.12.6 but I ended up having to use a combination of the answers posted by @VonC & @Christopher Simon to get this working reliably. Nothing I did before this would stop the container from exiting after it ran CMD successfully. I am starting oracle-xe-11Gr2 and sshd.
Dockerfile
Then adding -d -t and -i to run
Finally after hours of bashing my head against the wall
For whatever reason the above will exit after executing CMD if the tail -f is removed, or any of the -t -d -i options are omitted.
Docker requires your command to keep running in the foreground. Otherwise, it thinks that your applications stops and shutdown the container.
So if your docker entry script is a background process like following:
The '&' makes the container stop and exit if there are no other foreground process triggered later. So the solution is just remove the '&' or have another foreground CMD running after it, such as