This error occurs when attempting to execute commands on a stopped Docker container. The container exists but is not in a running state, preventing operations like exec, logs, or attach from working.
The "Container is not running" error from the Docker daemon indicates that you're attempting to perform an operation on a container that exists but is currently stopped. Unlike traditional virtual machines, Docker containers are designed to run a single process or application. When that process exits or is stopped, the container stops as well. This error commonly appears when using commands like `docker exec`, `docker attach`, or `docker logs` on containers that have exited. The container's state is preserved on disk, but it's not actively executing any processes. Understanding container lifecycle is key: containers transition between states (Created, Running, Paused, Stopped, Exited) based on the process they're running. A stopped container can be restarted, but operations requiring an active process will fail with this error.
First, list all containers including stopped ones to identify your container:
docker ps -aLook for the STATUS column. If it shows "Exited (0)", the process completed normally. A non-zero exit code indicates an error. Note the container ID or name.
Check the logs to understand why the container stopped:
docker logs <container_id>Look for error messages, stack traces, or application crashes that caused the exit. This often reveals missing environment variables, connection failures, or startup errors.
If the container simply needs to be restarted:
docker start <container_id>Then verify it's running:
docker ps | grep <container_id>If it starts successfully, you can now run exec commands or access logs.
If the container exits immediately, run it interactively to debug:
docker run -it <image_name> /bin/bashOr override the entrypoint to get a shell:
docker run -it --entrypoint /bin/sh <image_name>This allows you to explore the container environment and test commands manually.
Docker containers need a foreground process to stay running. If your CMD or ENTRYPOINT exits, the container stops. Common fixes:
Bad (exits immediately):
CMD echo "Hello"Good (runs indefinitely):
CMD ["nginx", "-g", "daemon off;"]
# or
CMD ["node", "server.js"]
# or for debugging
CMD ["tail", "-f", "/dev/null"]Ensure your Dockerfile's CMD or ENTRYPOINT starts a long-running process.
To automatically restart containers after host reboots or crashes:
docker run -d --restart unless-stopped <image_name>Or update an existing container:
docker update --restart unless-stopped <container_id>Restart policies: no (default), on-failure, always, unless-stopped.
Container Lifecycle Management:
Containers are not VMs. They run a single process tree defined by CMD/ENTRYPOINT. When that process exits (PID 1), the container stops. Use docker inspect to see detailed state information and exit codes.
Debugging Exited Containers:
Use docker inspect <container_id> | jq '.[0].State' to see exit code, error message, and timestamps. Exit codes follow Unix conventions (0 = success, 1 = general error, 126 = command cannot execute, 127 = command not found, 137 = SIGKILL, 143 = SIGTERM).
Docker Compose Considerations:
In Docker Compose, use restart: unless-stopped or restart: always in service definitions. Combine with health checks to ensure containers are not just running, but functional.
Common Pitfall with Shell Scripts:
If your entrypoint is a shell script, ensure it doesn't exit prematurely. Use exec to replace the shell process with your application: exec node server.js instead of node server.js, so signals reach your app directly.
CI/CD Best Practices:
In CI environments (GitHub Actions, GitLab CI), containers may exit if the build platform doesn't match. Use docker buildx with --platform linux/amd64 for cross-platform builds.
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