What's the Reason Docker Ubuntu Official Image Would Exit Immediately When You Run/Start It

What's the reason Docker Ubuntu official image would exit immediately when you run/start it?

Without -it the container has no TTY, and no stdin attached, so bash starts and completes directly after.

You can keep the container running by adding the -d option (so docker run -dit ubuntu) to start it in detached mode

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

Container exits even though it was started using -dit

The image has a default process (entrypoint) which is why you need to use:

docker run --rm --name my_container -it --entrypoint "/bin/bash" islandsound/vowpal_wabbit

(Otherwise you would use -it with a command like /bin/bash at the end.)

Also, you usually wouldn't use d (which runs the container in background) if you want an interactive session.

Rkt pod exited immediately (without logs)

I just found what was going on. This is just a problem with the run command. run have to come right after rkt. i know, it's stupid, but I may not be the only one facing this.
The right command :

sudo rkt run --insecure-options=image XXXXXXX.aci

How to keep Docker container running after starting services?

This is not really how you should design your Docker containers.

When designing a Docker container, you're supposed to build it such that there is only one process running (i.e. you should have one container for Nginx, and one for supervisord or the app it's running); additionally, that process should run in the foreground.

The container will "exit" when the process itself exits (in your case, that process is your bash script).


However, if you really need (or want) to run multiple service in your Docker container, consider starting from "Docker Base Image", which uses runit as a pseudo-init process (runit will stay online while Nginx and Supervisor run), which will stay in the foreground while your other processes do their thing.

They have substantial docs, so you should be able to achieve what you're trying to do reasonably easily.



Related Topics



Leave a reply



Submit