How to Fix a Docker Container That Keeps Restarting
A container that exits immediately after start indicates a startup or runtime failure rather than a slow shutdown.
Problem
Containers restart continuously when their main process exits, the entrypoint fails, or the container crashes during initialization.
Symptoms
docker psshowsRestarting (1) ...- Logs show the process exiting with a non-zero code.
- No service responds inside the container.
Investigation
Check logs with docker logs <container>. Look at the exit code. Inspect the entrypoint script. Verify that required environment variables and volumes are mounted correctly.
Working approach
- Inspect logs (
docker logs --tail 50). - Check the container health (
docker inspect --format='{{.State}}'). - Verify the image builds without errors.
- Confirm the entrypoint runs correctly in an interactive session (
docker run --entrypoint sh ...). - Apply the fix and restart.
Verification
Run docker ps after the change. Confirm the container has been running for more than a minute without restarts.