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?
Hi this issue is because docker containers exit if there is no running application in the container.
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
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
Argument order matters
Jersey Beans answer (all 3 examples) worked for me. After quite a bit of trial and error I realized that the order of the arguments matter.
Keeps the container running in the background:
docker run -t -d <image-name>
Keeps the container running in the foreground:
docker run <image-name> -t -d
It wasn't obvious to me coming from a Powershell background.
According to this answer, adding the
-t
flag will prevent the container from exiting when running in the background. You can then usedocker exec -i -t <image> /bin/bash
to get into a shell prompt.It seems that the -t option isn't documented very well, though the help says that it "allocates a pseudo-TTY."
I have explained it in the following post that has the same question.
How to retain docker alpine container after "exit" is used?