Docker container will automatically stop after "docker run -d"

docker keep container running daemon
docker container stops unexpectedly
docker container exits immediately
exit # docker container without killing it
docker run
docker stop all containers
docker stop container
docker entrypoint

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?

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 are not running on 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

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

Docker container stop automatically after running, I have an issue with the Docker container where the container stopped automatically after starting. I am not able to figure out what is the issue. 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, on centos:latest) is exiting immediately (as bash does when it's not connected to a terminal and has nothing to run).

According to this answer, adding the -t flag will prevent the container from exiting when running in the background. You can then use docker exec -i -t <image> /bin/bash to get into a shell prompt.

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

It seems that the -t option isn't documented very well, though the help says that it "allocates a pseudo-TTY."

Docker Container stopped automatically after starting, The container shell exits immediately as you are using Centos and the centos dockerfile uses bash as its default command, this will happen  Normally, a Docker container persists after it has exited. This allows you to run the container again, inspect its filesystem, and so on. However, sometimes you want to run a container and delete it immediately after it exits. Docker provides the --rm command line option for this purpose: docker run --rm my-docker

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, on centos: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 (like httpd). 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 using sleep as your primary command.

$ docker run -d centos:latest sleep infinity
$ docker ps
CONTAINER ID  IMAGE         COMMAND          CREATED       STATUS       PORTS NAMES
d651c7a9e0ad  centos:latest "sleep infinity" 2 seconds ago Up 2 seconds       nervous_visvesvaraya
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:

$ docker run -t -d centos:latest

Not 100% sure whether -t will produce other weird interactions; maybe leave a comment below if it does.

Docker container will automatically stop after “docker run -d , After the work was completed command specified at startup (in my example Note, if you attach to the container, it will stop when you exit, but you can start it  In this case, starting successfully means that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container which does not start at all from going into a restart loop. If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually

Hi this issue is because docker containers exit if there is no running application in the container.

-d 

option is just to run a container in deamon mode.

So the trick to make your container continuously running is point to a shell file in docker which will keep your application running.You can try with a start.sh file

Eg: docker run -d centos sh /yourlocation/start.sh

This start.sh should point to a never ending application.

In case if you dont want any application to be running,you can install monit which will keep your docker container running. Please let us know if these two cases worked for you to keep your container running.

All the best

The right way to keep docker container started when it used for , Your Docker container is exiting without a useful error message? You can either try to run your container without the -d flag, or check the exit code of a stopped container with (You could use sh here, but that's a bit uncomfortable at times). 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.

You can accomplish what you want with either:

docker run -t -d <image-name>

or

docker run -i -d <image-name>

or

docker run -it -d <image-name>

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

Find out Why Your Docker Container Keeps Crashing · vsupalov.com, It's possible to automatically restart crashed containers by specifying We can see from the above that after 3 minutes the container is stopped. After the container is configured with specs of the Dockerfile, it will be committed as an image. The image gets ready to rock & roll! Conclusion: As we can see, a docker container is independent of a docker image. A container can be restarted provided the unique ID of that container [use docker ps --all to get the id].

Ensuring Containers are Always Running with Docker's Restart Policy, We use the command "docker run" to run a container. docker list running and stopped containers In all above examples, when you start the container, you are automatically logged into it You can stop one or more (all) containers at once. Docker provides restart policies for containers. These policies can be configured to restart containers after particular events like failures, or if Docker itself stops. As part of Docker Swarm, you can even restart services automatically. $ docker run -dit — restart unless-stopped [CONTAINER]

How to List / Start / Stop / Delete docker Containers – The Geek Diary, The docker run command creates a container from a given image The detached container will stop when the root process is terminated to clean up the container's file system after the container exits. By default, if not explicitly set, the container's name is automatically generated by the Docker daemon. Recently one of my servers crashed and after the reboot, none of my Docker containers started. So I had to manually check each container and start the ones that were required. That is when I realized that I should implement a restart policy to control whether a container starts automatically or not.

Docker Run Command with Examples, Hello! I use command docker run -d -p 8080:8080 puckel/docker-airflow and localhost:8080 is not avaliable. Containers list shows:  Docker containers are not automatically removed when you stop them unless you start the container using the --rm flag. Remove one or more containers To remove one or more Docker images use the docker container rm command followed by the ID of the containers you want to remove.

Comments
  • "docker run hello-world" <== works perfectly, but if I run "docker run -d hello-world", I still cannot get a running container.
  • I had a similar issue but I got it working by using docker run -it -d <image> /bin/bash this starts a bash shell interactively and doesn't close the container because the shell process is active.
  • Sorry, one more question, I have a script need to run at startup, looks like /etc/rc.d/rc.local no longer works(my mindset still treating docker like OS), I assume docker file is better option in this case?
  • @GuruprasadGV That is expected. Instead of using docker attach, use docker exec -it <yourContainer> bash.
  • This also works if you add the tail command at the end of an entrypoint file.
  • I used sshd as my last (long running) command. You can then ssh on too your container as you would on a VM (once you set .ssh/authorized_keys etc)... You can also go on to configure the container using ansible.
  • I ended up doing this to execute my command plus the older fix listed above: docker run image_name /bin/bash -c "my/command && tail -f /dev/null"
  • Nice. Seems less hacky than appending tail -f /dev/null
  • Thanks! On the slim chance this helps someone, this doesn't behave nicely in Emacs Eshell.
  • docker run -t -d --name mysql -p 3306:3306 mysql - doesn't work for me (ubuntu 14.04.5): STATUS=Exited (1) 4 seconds ago
  • I found that you don't need <command> here, unless you want. Strangely, it also works to replace -t with -i (interactive). The doc does mention using -t and -i combined will behave like a shell.
  • Note that this doesn't work on alpine as BusyBox's sleep doesn't accept infinity.