Running docker exec -it my_container bash only to encounter Error response from daemon: Container <ID> is not running means your Docker container crashed immediately after starting or failed its health check process. Docker containers require a foreground process (PID 1) to stay alive.

Step 1: Inspect Container Exit Code & Error Logs

Use docker ps -a to view the exit code of the stopped container, then read stdout/stderr logs:

Docker Terminal Commandsbash
# 1. Display stopped containers and exit status
docker ps -a --filter "name=my_container"
# Example Output: Exited (1) 12 seconds ago
 
# 2. View error logs preceding the container crash
docker logs --tail 50 my_container

Common Causes & Resolutions

  • No Foreground Process (`PID 1`): If a Dockerfile specifies CMD ["service", "nginx", "start"], Nginx forks into the background, causing PID 1 to exit. Fix: Use CMD ["nginx", "-g", "daemon off;"].

  • Missing Environment Variables: Application entrypoint crashes due to missing DB passwords or secret keys. Fix: Pass variables via docker run -e KEY=VAL or .env files.

  • Permission Denied on Volumes: Mounted host directories lack read/write permissions for the container user ID.