Docker cannot restart a stopped container because the restart command only works on running containers. Use docker start instead, or check why the container exited and address the underlying issue.
This error occurs when you attempt to use `docker restart` on a container that has already exited or was never started. The Docker restart command is designed to stop a running container and then start it again, but it cannot operate on containers that are not currently running. When Docker reports "Cannot restart container: Container is not running," it means the container you're targeting is in an exited state. This can happen for various reasons: the container's main process completed successfully, the process crashed, or the container was explicitly stopped and never started again. The key distinction here is between `docker restart` and `docker start`. The restart command performs a stop-then-start sequence, which requires the container to be running first. The start command, on the other hand, can bring up a stopped container from its exited state.
First, verify the container's state to understand why the restart failed:
docker ps -a | grep <container_name_or_id>Look at the STATUS column. If it shows "Exited (0)" the container completed normally. If it shows "Exited (1)" or another non-zero code, the container crashed. The "Exited" status confirms why docker restart won't work.
For containers in an exited state, use docker start to bring them back up:
docker start <container_name_or_id>This will start the container using the same configuration it was created with. If you need to see the output:
docker start -a <container_name_or_id>The -a flag attaches STDOUT/STDERR so you can see what's happening.
If the container keeps exiting, check the logs to understand why:
docker logs <container_name_or_id>For more detailed output with timestamps:
docker logs --tail 50 --timestamps <container_name_or_id>The logs will show you what the application inside the container printed before it exited, which often reveals the root cause.
Get comprehensive information about the container's state:
docker inspect <container_name_or_id> --format='{{.State.Status}} - ExitCode: {{.State.ExitCode}}'Or view the full state section:
docker inspect <container_name_or_id> | grep -A 20 '"State"'This shows the exact exit code, when it stopped, and why. Common exit codes: 0 (success), 1 (general error), 137 (killed by SIGKILL), 143 (killed by SIGTERM).
To prevent this issue in the future, set a restart policy when running containers:
# Always restart unless explicitly stopped
docker run --restart=unless-stopped <image>
# Restart only on failure (non-zero exit code)
docker run --restart=on-failure <image>
# Always restart, even after Docker daemon restarts
docker run --restart=always <image>For existing containers, update the restart policy:
docker update --restart=unless-stopped <container_name_or_id>Note: The restart policy only takes effect after a successful start. You'll still need to use docker start first.
If you need to modify the container's configuration or it won't start properly, recreate it:
# Remove the old container
docker rm <container_name_or_id>
# Create and run a new one with proper settings
docker run -d --name <container_name> --restart=unless-stopped <image>For docker-compose projects:
docker-compose up -d --force-recreate <service_name>This ensures you have a fresh container with the correct configuration.
Understanding restart vs start: The docker restart command is equivalent to running docker stop followed by docker start. Since docker stop requires a running container, the entire restart operation fails if the container is already stopped. This is by design - restart is meant for gracefully cycling running services.
Restart policies explained:
- no: Never automatically restart (default)
- on-failure[:max-retries]: Restart only when the container exits with a non-zero code
- always: Always restart regardless of exit code; also starts on daemon startup
- unless-stopped: Like always, but won't start if it was manually stopped before daemon restart
Docker Compose considerations: In docker-compose.yml, use the restart key:
services:
web:
image: nginx
restart: unless-stoppedThe docker-compose restart command works differently - it sends the restart signal to all services, but individual containers still need to be running.
Systemd integration: If Docker itself isn't starting on boot, containers won't auto-start even with restart policies:
# Enable Docker to start on boot
sudo systemctl enable docker.service
# Check if Docker socket is the issue
sudo systemctl enable docker.socketExit code analysis: When troubleshooting repeated exits:
- Exit code 0: Process completed normally (may indicate CMD finished rather than crashed)
- Exit code 1: General application error
- Exit code 125: Docker daemon error
- Exit code 126: Permission problem or command not executable
- Exit code 127: Command not found
- Exit code 137: Container received SIGKILL (OOM killer or manual kill)
- Exit code 143: Container received SIGTERM (graceful shutdown)
- Exit code 255: Exit status out of range
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec