How to Debug Docker Container Exiting with Code 0 Immediately
In this tutorial, you'll learn about How to Debug Docker Container Exiting with Code 0 Immediately. We cover key concepts, practical examples, and best practices.
The Problem
A Docker container starts and exits immediately with exit code 0, without any error message, because the main process finishes instantly (e.g., a script that runs and returns, or a background daemon that forks away).
Quick Fix
Run in Interactive Mode with -it
docker run -it --rm ubuntu:22.04 bash
# root@abc1234:/# echo 'Container stays open'
# Container stays open
# root@abc1234:/# exit
Adding -it and a long-running command like bash keeps the container alive. Without -it, bash exits immediately because there is no terminal attached.
Use tail -f or sleep infinity as the Entrypoint
docker run -d --name debug ubuntu:22.04 sleep infinity
# abc1234def5678
docker exec debug echo 'Container still running'
# Container still running
docker kill debug
Override the command with sleep infinity to keep the container alive for debugging. This is useful for inspecting a container based on an image that normally runs a short-lived process.
Check the Container Logs
docker logs <container-id>
# (empty output — process exited silently)
docker inspect <container-id> --format '{{.State.ExitCode}}'
# 0
A container that exits with code 0 without logs means the main command finished successfully but was not designed to run as a foreground process. Check the Dockerfile's CMD or ENTRYPOINT.
Fix the Dockerfile to Keep the Process in Foreground
node -e "
const dockerfile = \`
FROM node:18
COPY server.js .
# ❌ Wrong: runs as background daemon
# CMD node server.js &
# ✅ Correct: runs in foreground
CMD node server.js
\`;
console.log('Use CMD without & to run process in foreground');
"
# Use CMD without & to run process in foreground
Many applications (e.g., web servers, databases) provide a foreground mode flag. Check the documentation and ensure the container does not daemonize.
Override the Entrypoint for Debugging
docker run -it --entrypoint sh myimage
# / # ls
# bin dev etc home ...
Use --entrypoint to override the container's entrypoint with a shell. This lets you inspect the filesystem, check environment variables, and manually run the application to see why it exits.
Inspect Container Configuration
docker inspect <container-id> --format '{{json .Config}}' | python3 -m json.tool
# {
# "Hostname": "abc123",
# "Env": ["PATH=/usr/local/bin:..."],
# "Cmd": ["node", "app.js"]
# }
Use docker inspect to examine the full configuration of a container. This reveals misconfigurations in environment variables, command arguments, and network settings that may not appear in logs.
Prevention
- Always run the main application process in the foreground inside a container
- Use
execin wrapper scripts to replace the shell with the application process - Start with
docker run -it <image> shto inspect the image before deploying with-d - Add
CMD ["sleep", "infinity"]temporarily for debugging containers that exit too early
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro