Docker Container Started in Detached Mode Stopped After Process Execution

Docker container will automatically stop after docker run -d

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).

Why docker container exits immediately

A docker container exits when its main process finishes.

In this case it will exit when your start-all.sh script ends. I don't know enough about hadoop to tell you how to do it in this case, but you need to either leave something running in the foreground or use a process manager such as runit or supervisord to run the processes.

I think you must be mistaken about it working if you don't specify -d; it should have exactly the same effect. I suspect you launched it with a slightly different command or using -it which will change things.

A simple solution may be to add something like:

while true; do sleep 1000; done

to the end of the script. I don't like this however, as the script should really be monitoring the processes it kicked off.

(I should say I stole that code from https://github.com/sequenceiq/hadoop-docker/blob/master/bootstrap.sh)

Docker Detached Mode

You can start a docker container in detached mode with a -d option. So the container starts up and run in background. That means, you start up the container and could use the console after startup for other commands.

The opposite of detached mode is foreground mode. That is the default mode, when -d option is not used. In this mode, the console you are using to execute docker run will be attached to standard input, output and error. That means your console is attached to the container's process.

In detached mode, you can follow the standard output of your docker container with docker logs -f <container_ID>.

Just try both options. I always use the detached mode to run my containers. I hope I could explain it a little bit clearer.

Why is Docker entrypoint script not executed in detached mode?

If you take a look at the docker docs

-d=false: Detached mode: Run container in the background, print new container id

Detach modes are generally made for services not standalone scripts if you want to see the output. You can use detached mode on standalone scripts if you don't want to see the output. Services that should be ran in detached mode would include databases, web servers, est. Not one time ran scripts that go and quit.

Your container runs in the background then quits because the script has ended. Since -d option only prints out the container id that is all you will see

Container is not running

Container 79b3fa70b51d seems to only do an echo.

That means it starts, echo and then exits immediately.

The next docker exec command wouldn't find it running in order to attach itself to that container and execute any command: it is too late. The container has already exited.

The docker exec command runs a new command in a running container.

The command started using docker exec will only run while the container's primary process (PID 1) is running

Docker - Detached and Interactive?

When you run an image with only -d option, the container will exit immediately after the command executed. If you run with -itd option, the container will be detached but running in background, and you can attach back when you need. See the screenshot attached for more clarity.

Sample Image

Is there any way to start a Docker container in detached mode?

Unless you specifically attach (-a or -i options) when you start the container, by definition you are detached.

Creating a container simply builds the filesystem layer. Starting it runs the ENTRYPOINT (or CMD) process. Run does both the create and the start, as you surmised. So you cannot "attach" to a created container... there is no process to attach to.

Here I create a container (again, all this does is create the filesystem layer):

[sysadmin@vmr-132-9 ~]$ docker create --name=test centos:latest /bin/sh -c "while true; do echo hello world; sleep 1; done"

See it?

sysadmin@vmr-132-9 ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d5bf75a8077 centos:latest "/bin/sh -c 'while tr" 15 seconds ago Created test

It isn't doing anything yet. Now start it without attaching, nothing is printed to the terminal STDOUT, because I am not attached. But STDOUT is going to the log-driver (json-file)

[sysadmin@vmr-132-9 ~]$ docker start test test
[sysadmin@vmr-132-9 ~]$ docker logs test
hello world
hello world
hello world
hello world


Related Topics



Leave a reply



Submit