How to fix Container exited with code 0 immediately after starting in Docker

DockerBEGINNERLOWUpdated Dec 6, 2025· 355 views

A Docker container that exits with code 0 immediately after starting has successfully completed its main process. This typically occurs when the container's entrypoint or command is a short-lived task rather than a long-running service, or when running interactive shells without proper terminal attachment.

Quick fix

Fixes Container exited with code 0 immediately after starting

# View the Dockerfile
cat Dockerfile

# Or inspect a running/stopped container
docker inspect <container_name> | grep -A 10 "Cmd"
  1. Check what command the container is running
  2. For interactive shells, add -it flags
  3. Add tty: true in docker-compose.yml
  4. Use a long-running foreground process
docker — error
# View the Dockerfile
cat Dockerfile
# Or inspect a running/stopped container
docker inspect <container_name> | grep -A 10 "Cmd"
Container exited with code 0 immediately after starting

What this error means

Exit code 0 in Docker indicates that the container's main process completed successfully—it's not actually an error. Containers are designed to run a single process, and when that process exits, the container stops. Unlike traditional servers that run continuously, Docker containers have a lifecycle tied to their primary process. When you see "exited with code 0," Docker is telling you that your application ran and finished without errors. This behavior is intentional and by design. However, if you expected the container to stay running (like a web server or database), it means your entrypoint/CMD is either completing too quickly or requires an interactive terminal that wasn't provided. The root cause is that Docker needs a foreground process that keeps running. If the main process exits or runs in the background without any foreground process to attach to, the container will shut down immediately.

Typical symptoms

  • Container starts and immediately shows 'Exited (0)' status
  • Running 'docker ps' shows no running container, but 'docker ps -a' shows it exited
  • Container works with 'docker run -it' but exits with 'docker run -d'
  • Shell containers (bash, sh) exit instantly when run in detached mode
  • After running 'docker-compose up', services exit with code 0
  • Container logs show no errors but container is not running

Common causes

  1. Container's CMD or ENTRYPOINT is a short-lived command that completes quickly (like mkdir, echo, or a script that exits)
  2. Running an interactive shell (bash, sh) without the -it flags to attach a terminal
  3. Main process runs in background (with &) and no foreground process remains
  4. Application completes its task successfully and exits (batch jobs, one-time scripts)
  5. Docker Desktop behavior change - newer versions don't automatically add -it flags
  6. Service starts as a background daemon but container has no foreground process to keep it alive

How to fix "Container exited with code 0 immediately after starting"

1Check what command the container is running

First, inspect the Dockerfile or docker-compose.yml to see what CMD or ENTRYPOINT is configured:

bash
# View the Dockerfile
cat Dockerfile

# Or inspect a running/stopped container
docker inspect <container_name> | grep -A 10 "Cmd"

If the command is something like bash, mkdir, echo, or a script that exits quickly, that's your issue.

2For interactive shells, add -it flags

If you're trying to run a shell container, you need to attach an interactive terminal:

bash
# Instead of:
docker run -d ubuntu:latest

# Use:
docker run -it ubuntu:latest

# Or for detached mode with terminal:
docker run -dit ubuntu:latest

The -i (interactive) keeps STDIN open, and -t allocates a pseudo-TTY, which prevents the shell from exiting.

3Add tty: true in docker-compose.yml

If using Docker Compose, add the tty and stdin_open options:

yaml
services:
  myapp:
    image: ubuntu:latest
    tty: true
    stdin_open: true
    # Or specify a long-running command
    command: tail -f /dev/null

This keeps the container running by maintaining an attached terminal.

4Use a long-running foreground process

Replace short-lived commands with processes that run continuously:

dockerfile
# Instead of:
CMD ["echo", "Hello World"]

# Use a web server:
CMD ["nginx", "-g", "daemon off;"]

# Or keep container alive for debugging:
CMD ["tail", "-f", "/dev/null"]

The key is ensuring the process runs in the foreground and doesn't exit. For nginx, daemon off; is critical.

5Fix background processes with foreground wrapper

If your main process runs in background (with &), ensure there's a foreground process afterward:

bash
#!/bin/bash
# Bad - container will exit
./my-background-service &

# Good - start background service then keep container alive
./my-background-service &
tail -f /dev/null

Or better yet, run your service in foreground mode if it supports it.

6Debug by overriding the entrypoint

To investigate what's happening, start the container with a shell and run commands manually:

bash
# Override entrypoint to get a shell
docker run -it --entrypoint /bin/bash <image_name>

# Now manually run your original CMD to see what happens
./your-original-command.sh

This helps you understand why the process is exiting.

Advanced notes

Exit Code 0 vs Other Exit Codes: Remember that exit code 0 specifically means "success." If you see other exit codes (like 1, 137, 143), those indicate errors or signals. Code 0 is unique because it means your application is working correctly—it's just not designed to run continuously.

Production Considerations: Using tail -f /dev/null is fine for debugging but not ideal for production. For production containers, ensure your application itself runs as a foreground process. Most production-ready applications (web servers, databases, message queues) have a foreground or non-daemon mode for exactly this reason.

Docker Compose Dependencies: If you're using depends_on in docker-compose and a dependency container exits with 0, it can cause issues. Consider using health checks instead of depends_on for services that need to stay running.

Batch Jobs vs Services: This behavior is perfectly normal for batch jobs or CI/CD containers that perform a task and exit. Not every container needs to stay running forever. If your use case is a one-time task, exit code 0 is exactly what you want.

Frequently asked questions

What does "Container exited with code 0 immediately after starting" mean?
Exit code 0 in Docker indicates that the container's main process completed successfully—it's not actually an error. Containers are designed to run a single process, and when that process exits, the container stops. Unlike traditional servers that run continuously, Docker containers have a lifecycle tied to their primary process.
How do I fix "Container exited with code 0 immediately after starting"?
A Docker container that exits with code 0 immediately after starting has successfully completed its main process. This typically occurs when the container's entrypoint or command is a short-lived task rather than a long-running service, or when running interactive shells without proper terminal attachment.
What causes "Container exited with code 0 immediately after starting"?
Container's CMD or ENTRYPOINT is a short-lived command that completes quickly (like mkdir, echo, or a script that exits) Running an interactive shell (bash, sh) without the -it flags to attach a terminal Main process runs in background (with &) and no foreground process remains
Is the "Container exited with code 0 immediately after starting" error serious?
It is a low-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading