How to Resolve "Cannot connect to the Docker daemon at unix:///var/run/docker.sock"
Quick answer
The error "Cannot connect to the Docker daemon at unix:///var/run/docker.sock" indicates that the Docker client is unable to establish communication with the...
The error "Cannot connect to the Docker daemon at unix:///var/run/docker.sock" indicates that the Docker client is unable to establish communication with the background Docker engine daemon service. This happens either because the Docker service is completely stopped, the system service failed to initialize on boot, or your environment variables are misdirecting API calls to a non-existent socket location. Restarting the daemon or reconfiguring startup services solves the issue.
The Problem
Executing any Docker CLI command returns a communication failure indicating the IPC Unix domain socket is unresponsive:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
In CI/CD environments or script pipelines, you may observe related variations of this socket connectivity error:
Error response from daemon: Bad response from Docker engine
dial unix /var/run/docker.sock: connect: no such file or directory
Why It Happens
The Docker architecture follows a client-server pattern. The docker executable is purely a CLI client that communicates with dockerd via the Unix socket file at /var/run/docker.sock. Common causes for connectivity dropouts include:
- Stopped Docker Service: The
dockersystemd service or Docker Desktop application is not actively running. - Crashing Daemon: Modern configuration errors in
/etc/docker/daemon.jsonpreventdockerdfrom booting cleanly. - DOCKER_HOST Environment Variable Override: An environment variable points the client to an invalid remote socket or custom socket path.
- WSL2 Integration Inactivity: On Windows Subsystem for Linux (WSL2), Docker Desktop integration with your Linux distribution is disabled or disconnected.
The Fix
Start the daemon engine and verify socket status following these OS-specific steps.
Step 1: Check Docker service status on Linux
Run systemctl to determine if the Docker service is active:
sudo systemctl status docker
If the service is stopped or inactive, start it and enable auto-boot on system startup:
sudo systemctl start docker
sudo systemctl enable docker
Step 2: Clear invalid DOCKER_HOST environment variables
Check if your current terminal session has overridden the standard Docker host location:
echo $DOCKER_HOST
If this returns a value (e.g., tcp://127.0.0.1:2375 or a stale custom socket), unset it to force Docker to default back to unix:///var/run/docker.sock:
unset DOCKER_HOST
Remove any permanent export DOCKER_HOST definitions from your ~/.bashrc or ~/.zshrc files.
Step 3: Enable Docker integration in WSL2 (Windows users)
If using Windows Subsystem for Linux (WSL2):
- Open Docker Desktop on Windows.
- Navigate to Settings > Resources > WSL Integration.
- Enable Use the WSL 2 based engine.
- Toggle the switch for your specific installed Linux distribution (e.g.,
Ubuntu-22.04) and click Apply & restart.
Still Not Working?
If starting systemd fails, invalid JSON or unrecognized options in /etc/docker/daemon.json are likely preventing the daemon process from starting.
Test starting the Docker daemon manually in foreground mode to view real-time startup error logs:
sudo dockerd --debug
If output flags syntax errors inside /etc/docker/daemon.json, validate your JSON formatting or temporarily rename the configuration file to restore default settings:
sudo mv /etc/docker/daemon.json /etc/docker/daemon.json.bak
sudo systemctl restart docker