Docker container will automatically stop after “docker run -d”

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.

  • Prepare Docker container for worst-case rescue via shell or exec
  • copy file from docker to host system using python script
  • Docker: HTTP code 403 while uploading metadata: “invalid character '<' looking for beginning of value”
  • Mac Docker Machine ZSH shell set env variable not applying
  • ECS Service other than HTTP keeps restarting
  • Host port with DB to Docker container
  • 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?

  • Docker restart not showing the desired effect
  • docker version fails on OS X
  • How to create a docker image for php and node?
  • Permission Issue in Docker container for Symfony2
  • Calling mongodump wrapped into docker
  • docker rabbitmq hostname issue
  • 6 Solutions collect form web for “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.

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

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

    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

    Docker requires your command to keep running in the foreground. Otherwise, it thinks that your applications stops and shutdown the container.

    So if your start script have contained a background process like following:

    /usr/local/bin/confd -interval=30 -backend etcd -node $CONFIG_CENTER &
    

    The ‘&’ makes the container stop and exit if there are no other foreground process triggered later. This behavior confused me a long time and I got my answer here! many thanks to VonC

    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

    ...
    RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' && systemctl enable sshd
    ...
    CMD /etc/init.d/oracle-xe start && /sbin/sshd && tail -f /dev/null
    

    Then adding -d -t and -i to run

    docker run --shm-size=2g --name oracle-db -d -t -i -p 5022:22 -p 5080:8080 -p 1521:1521 centos-oracle:7.3.1611 
    

    Finally after hours of bashing my head against the wall

    ssh -v root@127.0.0.1 -p 5022
    ...
    root@127.0.0.1's password: 
    debug1: Authentication succeeded (password).
    

    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.

    execute command as follows :

    docker run -t -d <image-name>
    

    if you want to specify port then command as below:

    docker run -t -d -p <port-no> <image-name>
    

    verify the running container using following command:

    docker ps
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.