This error occurs when Docker's PID file exists but the daemon isn't properly running, typically after an unclean shutdown, system crash, or when attempting to start Docker while it's already running. The fix involves removing the stale PID file and properly restarting the Docker service.
When the Docker daemon starts, it creates a PID (Process ID) file at `/var/run/docker.pid` (or `/var/snap/docker/<version>/run/docker.pid` for snap installations). This file contains the process ID of the running Docker daemon and serves as a lock to prevent multiple daemon instances from starting simultaneously. The "pid file found, ensure docker is not running" error occurs when Docker attempts to start but finds an existing PID file. This typically happens in these scenarios: - **Unclean shutdown**: The system crashed or lost power while Docker was running, leaving a stale PID file behind - **Docker is already running**: You're trying to start Docker manually (via `dockerd`) while it's already managed by systemd or another init system - **Process terminated abnormally**: The Docker daemon crashed or was killed without proper cleanup Docker refuses to start because it interprets the existing PID file as a sign that another instance might be running. This safety mechanism prevents potential conflicts and data corruption that could occur if multiple daemon instances accessed the same containers and images.
Before deleting the PID file, verify whether Docker is truly running or if it's a stale file:
Check for running Docker processes:
ps aux | grep -E 'dockerd|containerd'Check systemd service status:
sudo systemctl status docker
sudo systemctl status containerdCheck the PID in the file:
cat /var/run/docker.pid
# Then check if that PID is actually running
ps -p $(cat /var/run/docker.pid) 2>/dev/null && echo "Process is running" || echo "Process is NOT running (stale PID)"If the process is NOT running but the PID file exists, it's safe to remove it. If Docker IS running, you should stop it properly instead of deleting the file.
If Docker processes are still running, stop them properly before proceeding:
For systemd-managed Docker:
sudo systemctl stop docker
sudo systemctl stop containerdFor Docker installed via snap:
sudo snap stop dockerIf systemctl commands hang or fail, the daemon might be unresponsive. In that case, proceed to the next step to kill processes manually.
If Docker processes are still running and won't stop gracefully, kill them manually:
Find and kill Docker processes:
# Find all Docker-related processes
ps -ef | grep -E 'dockerd|containerd' | grep -v grep
# Kill dockerd process
sudo pkill -9 dockerd
# Kill containerd if necessary
sudo pkill -9 containerd
# Alternatively, kill by PID from the file
sudo kill -9 $(cat /var/run/docker.pid 2>/dev/null) 2>/dev/nullVerify processes are stopped:
ps aux | grep -E 'dockerd|containerd' | grep -v grepIf no processes are listed, you can proceed to clean up the PID file.
Once you've confirmed Docker isn't actually running, delete the PID file:
For standard Docker installation:
sudo rm -f /var/run/docker.pidFor snap-installed Docker:
# Find the correct path (version number varies)
ls /var/snap/docker/*/run/docker.pid
# Remove it
sudo rm -f /var/snap/docker/*/run/docker.pidAlso check for containerd PID file:
sudo rm -f /var/run/containerd/containerd.pidNote: The PID file location may vary by distribution. Common locations:
- /var/run/docker.pid (most Linux distributions)
- /run/docker.pid (same as above, /var/run is typically symlinked to /run)
- /var/snap/docker/<version>/run/docker.pid (snap installations)
Now restart the Docker service:
For systemd-managed Docker:
sudo systemctl daemon-reload
sudo systemctl start dockerFor snap-installed Docker:
sudo snap start dockerVerify Docker is running:
sudo systemctl status docker
docker ps
docker infoYou should see Docker running with no errors.
To minimize the chance of this happening again:
Enable live-restore (recommended):
Add this to /etc/docker/daemon.json:
{
"live-restore": true
}This allows containers to keep running even when the daemon stops, and helps with cleaner restarts.
Ensure proper shutdown sequence:
Always stop Docker gracefully before system shutdown:
sudo systemctl stop dockerFor servers, configure proper shutdown order:
# Docker should stop before network services
sudo systemctl add-wants shutdown.target docker.serviceMonitor Docker health:
Set up monitoring to detect daemon crashes:
# Create a simple watchdog script or use systemd's built-in restart
sudo systemctl edit docker.service
# Add: Restart=on-failure### Docker-in-Docker (DinD) Considerations
When running Docker inside Docker containers (common in CI/CD pipelines), the PID file issue is particularly common. The inner Docker daemon may not have a chance to clean up when the container is stopped.
Solution for DinD:
Modify your Docker entrypoint script to clean up the PID file before starting:
#!/bin/sh
# run-docker.sh for DinD
rm -f /var/run/docker.pid
exec dockerd "$@"In GitLab CI, use the official dind service image which handles this automatically:
services:
- docker:dind
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"### Understanding the PID File Lock Mechanism
Docker uses the PID file as a form of inter-process communication lock:
1. On startup, dockerd checks if /var/run/docker.pid exists
2. If it exists, Docker reads the PID and checks if that process is running
3. If the process IS running, Docker refuses to start (prevents double-daemon)
4. If the process is NOT running, Docker should remove the stale file and start
However, in some edge cases (particularly after crashes), this cleanup doesn't happen properly, hence the need for manual intervention.
### Why Docker Uses PID Files
PID files serve several purposes:
- Prevent multiple instances: Only one Docker daemon should manage the container runtime
- Graceful shutdown: Init systems can read the PID to send stop signals
- Health checks: Monitoring systems can verify the daemon is running
### Snap-Specific Behavior
Docker installed via snap has a different file layout and may require additional steps:
# Full cleanup for snap Docker
sudo snap stop docker
sudo rm -f /var/snap/docker/*/run/docker.pid
sudo rm -f /var/snap/docker/*/run/containerd/containerd.pid
sudo snap start dockerCheck snap logs for additional errors:
sudo snap logs docker -n 50### Filesystem and Mount Issues
In some cases, the PID file cannot be deleted because:
- The filesystem is mounted read-only
- The /var/run directory is on a full disk
- SELinux or AppArmor is blocking the operation
Check filesystem status:
mount | grep "run"
df -h /var/runIf read-only, remount as read-write:
sudo mount -o remount,rw /var/run### Alternative: Using Systemd Drop-in
If you frequently encounter this issue, create a systemd drop-in that cleans up before starting:
sudo mkdir -p /etc/systemd/system/docker.service.d
cat << 'EOF' | sudo tee /etc/systemd/system/docker.service.d/cleanup.conf
[Service]
ExecStartPre=-/bin/rm -f /var/run/docker.pid
EOF
sudo systemctl daemon-reloadNote: The - before /bin/rm means the service will start even if the rm command fails (e.g., if the file doesn't exist).
image operating system "linux" cannot be used on this platform
How to fix 'image operating system linux cannot be used on this platform' in Docker
manifest unknown: manifest unknown
How to fix 'manifest unknown' in Docker
cannot open '/etc/passwd': Permission denied
How to fix 'cannot open: Permission denied' in Docker
Error response from daemon: failed to create the ipvlan port
How to fix 'failed to create the ipvlan port' in Docker
toomanyrequests: Rate exceeded for anonymous users
How to fix 'Rate exceeded for anonymous users' in Docker Hub