Tools: Docker Container Keeps Restarting: Diagnosis and Fix Guide (2026)

Tools: Docker Container Keeps Restarting: Diagnosis and Fix Guide (2026)

Docker Container Keeps Restarting: Diagnosis and Fix Guide

Step 1: Read the Crash Logs First

Step 2: Check the Exit Code

Step 3: Stop the Restart Loop

Step 4: Fix by Exit Code

Exit Code 137 (OOM Killed)

Exit Code 1 (Application Error)

Container Exits With Code 0 (Clean Exit)

Step 5: Debug Interactively

Step 6: Check Docker Compose Dependencies

Full Diagnostic Checklist If docker ps shows your container in a perpetual restart loop, here's the systematic way to diagnose and fix it. This is where 80% of your answers are. Don't skip this. Now you can investigate without the container fighting you. Common causes and fixes: Your process ran and finished. Docker restarts it because of your restart policy. Or if running a script: Without depends_on with health checks, your app starts before the DB is ready → connection refused → crash → restart loop. Restart loops at 2AM are brutal. Step2Dev — paste your docker logs and get the exact fix for your container setup in 60 seconds. Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

$ -weight: 500;">docker logs my-container --tail 100 -weight: 500;">docker logs my-container --tail 100 2>&1 | grep -i "error\|fatal\|exit\|killed" -weight: 500;">docker logs my-container --tail 100 -weight: 500;">docker logs my-container --tail 100 2>&1 | grep -i "error\|fatal\|exit\|killed" -weight: 500;">docker logs my-container --tail 100 -weight: 500;">docker logs my-container --tail 100 2>&1 | grep -i "error\|fatal\|exit\|killed" -weight: 500;">docker inspect my-container --format='{{.State.ExitCode}}' -weight: 500;">docker inspect my-container --format='{{.State.ExitCode}}' -weight: 500;">docker inspect my-container --format='{{.State.ExitCode}}' -weight: 500;">docker -weight: 500;">update ---weight: 500;">restart=no my-container -weight: 500;">docker -weight: 500;">update ---weight: 500;">restart=no my-container -weight: 500;">docker -weight: 500;">update ---weight: 500;">restart=no my-container # Check current memory usage -weight: 500;">docker stats my-container --no-stream # Increase memory limit -weight: 500;">docker run --memory=512m --memory-swap=512m myimage # In -weight: 500;">docker-compose.yml services: myapp: deploy: resources: limits: memory: 512M # Check current memory usage -weight: 500;">docker stats my-container --no-stream # Increase memory limit -weight: 500;">docker run --memory=512m --memory-swap=512m myimage # In -weight: 500;">docker-compose.yml services: myapp: deploy: resources: limits: memory: 512M # Check current memory usage -weight: 500;">docker stats my-container --no-stream # Increase memory limit -weight: 500;">docker run --memory=512m --memory-swap=512m myimage # In -weight: 500;">docker-compose.yml services: myapp: deploy: resources: limits: memory: 512M # 1. Missing environment variable -weight: 500;">docker run -e DATABASE_URL=postgres://... myimage # 2. Can't connect to database (wrong hostname) # In -weight: 500;">docker-compose, use -weight: 500;">service name, not localhost DATABASE_URL=postgres://db:5432/mydb # 'db' = -weight: 500;">service name # 3. Port already in use lsof -i :3000 # Find what's using the port -weight: 500;">docker run -p 3001:3000 myimage # Map to different host port # 1. Missing environment variable -weight: 500;">docker run -e DATABASE_URL=postgres://... myimage # 2. Can't connect to database (wrong hostname) # In -weight: 500;">docker-compose, use -weight: 500;">service name, not localhost DATABASE_URL=postgres://db:5432/mydb # 'db' = -weight: 500;">service name # 3. Port already in use lsof -i :3000 # Find what's using the port -weight: 500;">docker run -p 3001:3000 myimage # Map to different host port # 1. Missing environment variable -weight: 500;">docker run -e DATABASE_URL=postgres://... myimage # 2. Can't connect to database (wrong hostname) # In -weight: 500;">docker-compose, use -weight: 500;">service name, not localhost DATABASE_URL=postgres://db:5432/mydb # 'db' = -weight: 500;">service name # 3. Port already in use lsof -i :3000 # Find what's using the port -weight: 500;">docker run -p 3001:3000 myimage # Map to different host port # BAD — this runs and exits CMD ["node", "migrate.js"] # GOOD — this keeps running CMD ["node", "server.js"] # BAD — this runs and exits CMD ["node", "migrate.js"] # GOOD — this keeps running CMD ["node", "server.js"] # BAD — this runs and exits CMD ["node", "migrate.js"] # GOOD — this keeps running CMD ["node", "server.js"] CMD ["sh", "-c", "node migrate.js && node server.js"] CMD ["sh", "-c", "node migrate.js && node server.js"] CMD ["sh", "-c", "node migrate.js && node server.js"] # Override entrypoint to get a shell -weight: 500;">docker run -it --entrypoint /bin/sh myimage # Or exec into a running container -weight: 500;">docker exec -it my-container /bin/bash # Inside container — test connectivity -weight: 500;">curl -I http://db:5432 # Can it reach the DB? env | grep DATABASE # Are env vars set? # Override entrypoint to get a shell -weight: 500;">docker run -it --entrypoint /bin/sh myimage # Or exec into a running container -weight: 500;">docker exec -it my-container /bin/bash # Inside container — test connectivity -weight: 500;">curl -I http://db:5432 # Can it reach the DB? env | grep DATABASE # Are env vars set? # Override entrypoint to get a shell -weight: 500;">docker run -it --entrypoint /bin/sh myimage # Or exec into a running container -weight: 500;">docker exec -it my-container /bin/bash # Inside container — test connectivity -weight: 500;">curl -I http://db:5432 # Can it reach the DB? env | grep DATABASE # Are env vars set? services: app: image: myapp depends_on: db: condition: service_healthy # Wait for DB to be ready db: image: postgres:15 healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 5 services: app: image: myapp depends_on: db: condition: service_healthy # Wait for DB to be ready db: image: postgres:15 healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 5 services: app: image: myapp depends_on: db: condition: service_healthy # Wait for DB to be ready db: image: postgres:15 healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 5 # 1. Get recent logs -weight: 500;">docker logs my-container --tail 50 2>&1 # 2. Get exit code -weight: 500;">docker inspect my-container | grep '"ExitCode"' # 3. Check resource usage -weight: 500;">docker stats --no-stream # 4. Inspect full config -weight: 500;">docker inspect my-container | python3 -m json.tool # 5. Check events -weight: 500;">docker events --filter container=my-container --since 10m # 1. Get recent logs -weight: 500;">docker logs my-container --tail 50 2>&1 # 2. Get exit code -weight: 500;">docker inspect my-container | grep '"ExitCode"' # 3. Check resource usage -weight: 500;">docker stats --no-stream # 4. Inspect full config -weight: 500;">docker inspect my-container | python3 -m json.tool # 5. Check events -weight: 500;">docker events --filter container=my-container --since 10m # 1. Get recent logs -weight: 500;">docker logs my-container --tail 50 2>&1 # 2. Get exit code -weight: 500;">docker inspect my-container | grep '"ExitCode"' # 3. Check resource usage -weight: 500;">docker stats --no-stream # 4. Inspect full config -weight: 500;">docker inspect my-container | python3 -m json.tool # 5. Check events -weight: 500;">docker events --filter container=my-container --since 10m