Why Does My Docker Container Exit Immediately? (And How to Fix It)

One of the most common frustrations for developers new to Docker is this scenario:

You start a Docker container, and it exits right away with no clear error.

Don’t worry—you’re not alone! This behavior usually indicates that the container ran successfully and then finished doing what it was told to do.

In this blog post, you’ll learn:

  • ✅ Common reasons why a Docker container exits immediately
  • 🧪 How to debug and keep containers running
  • 🔧 Real-world fixes with examples

🧠 TL;DR — The container exits because the main process finishes.

Docker containers are designed to run a single main process (PID 1). When that process ends, the container stops—even if the image built successfully.


✅ 1. The Main Command Exited

Example:

docker run ubuntu

✅ This runs the Ubuntu container… but it exits immediately!

Why?
You didn’t tell it to run anything, so it launched, found nothing to do, and exited.

🔧 Fix:

Use an interactive shell:

docker run -it ubuntu /bin/bash

This keeps the container running until you exit the shell.


✅ 2. CMD or ENTRYPOINT Executes a Script That Finishes

If your Dockerfile looks like this:

CMD ["echo", "Hello from Docker!"]

It will print the message and exit. That’s expected behavior.

🔧 Fix:

If you want to keep the container running (e.g., for testing), replace with a long-running process:

CMD ["tail", "-f", "/dev/null"]

Or run your actual application (like a server, database, etc.).


✅ 3. You’re Using an Interactive Shell in Detached Mode

Running this:

docker run -d ubuntu /bin/bash

Will exit immediately because bash has no interactive terminal to stay alive.

🔧 Fix:

Use -it instead of -d, or run a command that persists:

docker run -dit ubuntu

or:

docker run -d ubuntu tail -f /dev/null

✅ 4. The Application Crashed or Exited

Your container might exit because:

  • The application ran into an error
  • It finished a task and exited normally

🔧 Fix:

Inspect logs:

docker logs <container-id>

You’ll often find the error message or completion notice there.


✅ 5. docker-compose Uses a One-Off Command

If your docker-compose.yml runs a one-time script (like a migration or build step), the container exits after the task is done.

Example:

command: python manage.py migrate

This finishes and exits normally. Not an error—just behavior.


🧪 How to Keep a Container Running (Temporarily)

Use a dummy process like this:

CMD ["tail", "-f", "/dev/null"]

Or run your app in the foreground (not as a daemon).


🧠 Summary

ReasonWhy It HappensHow to Fix
No command providedNothing to executeUse -it and run /bin/bash
Script or app finishesContainer did its job and exitedAdd persistent app or dummy task
Detached shellNo interactive shell sessionUse -it instead of -d
App crashedError in command/scriptCheck docker logs
One-time task in ComposeTask completedAdd a long-running service

🏁 Final Thoughts

A container that exits immediately isn’t always a problem—it might mean everything ran exactly as expected. The key is understanding what your container is supposed to do, and ensuring the main process runs as needed.

Sharing Is Caring:

Leave a Comment