Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container exits as soon as I start it

When I run or start a Docker container, it will not stay running. Docker start will just return the name of whatever container I gave it, but wont actually do anything.

Docker run (ex $ docker run -p 8080:80 --name hello -d hello-world) will create it but it will exit immediately.

If I run docker ps after one of these, it will show nothing listed as currently running. If I run docker ps -a, it will show all of my containers and show the one that I just attempted to run having exited a few seconds ago.

Is this common and how do I get my containers to stay running? I am trying to learn how to use Docker and it has been one of the worst experiences. Thank you for any help or suggestions.

like image 586
farmer1010 Avatar asked Oct 24 '25 16:10

farmer1010


1 Answers

A docker container exits when its main process finishes. The hello-world main process just prints some text and exits, so container exits too.

You can run this command straightly to see it's text:

docker run hello-world

If you want a running container, maybe you can try a nginx demo:

docker run --name nginx-demo -p 8080:80 -d nginx

then you can visit http://localhost:8080 using your web browser.

like image 182
Gavin Avatar answered Oct 26 '25 06:10

Gavin