Troubleshoot Docker Containers That Exit or Keep Restarting
Before you begin
- Applies to:Docker Engine on Linux with Compose v2. The steps assume one replica per service.
- Where to start:Open a terminal in the deployed project directory. Use the same Docker context, project name, and Compose files as the deployment.
- What you need:Use an account authorized to run Docker commands. Have the affected service name ready and replace the example name app. Keep the same shell for commands that share variables.
Guide tools
A restart policy can keep a failed process coming back. Capture the last exit state and logs before recreating the container. A one-shot job exiting successfully may have completed normally.
Troubleshooting Steps
Identify which service is failing
InventoryThe —all option includes stopped containers. Record the service and container ID, then read the latest 80 log lines with timestamps. If logs are empty, inspect the engine error field and check whether the app logs to files or a different sink.
docker compose ps --alldocker compose logs --tail 80 --timestamps appDistinguish an application exit from a runtime failure
StateCopy the affected container ID from the first step into the variable. Check OOMKilled and the memory limit. Exit code 137 indicates SIGKILL in typical cases and can also follow a forced stop. OOMKilled is stronger evidence. A nonempty Error field can identify a runtime startup failure before the app writes logs.
What you should see · The container state and logs tell you whether Docker could start the container and whether the app then exited.
If you see something else · Start with the runtime error if the Error field is populated. If Docker reports OOMKilled, compare the configured limit and measured demand. If the app logged an error, take that error into Step 3 before recreating the container.
container='example-app-1'docker inspect --type container --format '{{json .State}}' "$container"docker inspect --type container --format 'restarts={{.RestartCount}} restart={{.HostConfig.RestartPolicy.Name}} memory_bytes={{.HostConfig.Memory}}' "$container"Correct the confirmed cause
DiagnosisFor a file or permission error compare the container mount with the intended host path and runtime user. For a missing setting restore it through the configured secret or environment source. For a confirmed OOM review observed memory demand and host capacity before changing the limit. Validate Compose syntax without printing the rendered secrets.
docker compose config --quietRoll out the single service and verify
RecoveryFor a confirmed service-only configuration or image fix, use the approved deployment procedure. The example assumes dependencies are already healthy and may recreate the app container. Confirm it stays running or completes as expected, check its application health, and compare the restart count over time. If it regresses, restore the known-good Compose settings and image reference, then redeploy through the same process.
docker compose up -d --no-deps appdocker compose ps --all appdocker compose logs --tail 40 --timestamps appReferences
Your feedback keeps these production procedures accurate and battle-tested.