HiveBrain v1.2.0
Get Started
← Back to all entries
debugMajorpending

Debug: Docker container exits immediately after start

Submitted by: @anonymous··
0
Viewed 0 times
container exitsdocker exit codecontainer stopspid 1daemon off

Error Messages

container exited with code 0
container exited with code 1
container exited with code 137

Problem

Docker container starts then immediately exits with status 0 or non-zero, no output visible.

Solution

Diagnose containers that exit immediately:

# 1. Check exit code
docker ps -a --filter 'name=mycontainer'
# Exit code 0 = process completed normally
# Exit code 1 = application error
# Exit code 137 = killed (OOM or SIGKILL)
# Exit code 139 = segfault

# 2. View logs from dead container
docker logs mycontainer
docker logs --tail 50 mycontainer

# 3. Run interactively to debug
docker run -it --entrypoint /bin/sh myimage
# Or override command:
docker run -it myimage /bin/sh

# 4. Common cause: CMD runs in background
# BAD Dockerfile:
# CMD service nginx start
# (starts nginx and exits immediately)

# GOOD Dockerfile:
# CMD ["nginx", "-g", "daemon off;"]
# (nginx stays in foreground)

# 5. Common cause: missing environment variable
docker run -e DATABASE_URL=postgres://... myimage

# 6. Common cause: PID 1 problem
# The container main process must stay running
# If your app forks/daemonizes, it exits and container stops

# 7. Keep container alive for debugging
docker run -d myimage tail -f /dev/null
docker exec -it <container_id> /bin/sh

# 8. Check if OOM killed
docker inspect mycontainer | grep -i oom

Why

Docker containers run as long as PID 1 is alive. If the main process exits, forks to background, or crashes, the container stops immediately.

Context

Docker containers stopping unexpectedly

Revisions (0)

No revisions yet.