DEV Community

Dev Learning Tools
Dev Learning Tools

Posted on Originally published at devlearningtools.com

My Docker Container Keeps Exiting. Here's the Checklist I Use

You ran docker run, the container appeared for a second, and then it was gone. Or you ran docker ps and nothing was there.

This happens to almost everyone who starts with Docker. The good news is that the cause is almost always one of a few things, and you can find it in about two minutes with the same steps every time.

Step 1: Check the status

docker ps -a
Enter fullscreen mode Exit fullscreen mode

docker ps only shows running containers. docker ps -a shows stopped ones too. Look at the STATUS column. If it says Exited (1) or another non-zero number, the program inside crashed. If it says Exited (0), the program finished normally, which often means it wasn't meant to exit at all.

Step 2: Read the logs

docker logs <container-name>
Enter fullscreen mode Exit fullscreen mode

This shows what the program printed before it stopped. The last few lines usually name the problem: a missing file, a wrong password, or a missing environment variable.

Step 3: Inspect the configuration

docker inspect <container-name>
Enter fullscreen mode Exit fullscreen mode

This shows the command the container actually ran, its environment variables, and its mounts. Many problems come from a setting that didn't reach the container the way you expected.

Step 4: Fix one thing and try again

Change one thing in your command or Dockerfile, remove the old container, and run it again:

docker rm <container-name>
docker run -d --name myapp -p 8000:8000 my-python-app
Enter fullscreen mode Exit fullscreen mode

Changing one thing at a time makes it obvious which change fixed it.

The usual suspects

Symptom Likely cause What to try
Exits immediately The main program finished or crashed Check the logs, and make sure the program runs in the foreground
"Port already in use" Another program uses the same port Use a different host port, like -p 8081:8000
Data disappears Data was written inside the container Mount a volume with -v
Can't reach another container by name Both are on the default network Create a network with docker network create and attach both
Code changes don't show up Old image or old container Rebuild with docker build, then run a new container

One warning before you clean up

When the disk fills up, it's tempting to run:

docker system prune -a
Enter fullscreen mode Exit fullscreen mode

This removes every unused image, not only the broken ones. Run docker system df first to see what's using space, and read the prompt before you confirm.

Going deeper

I covered this in more detail in a free beginner course on Docker, with a troubleshooting lesson and the commands in one place: https://www.devlearningtools.com/learn/docker.

What error did you hit the first time your container exited? Share it in the comments and I'll help you read the logs.

Top comments (0)