The error “Cannot connect to the Docker daemon at unix:///var/run/docker.sock” means the Docker client cannot reach the Docker daemon process that actually builds, runs, and manages containers. This usually happens because the daemon isn’t running, the current user lacks permission to access the socket, or the Docker service is misconfigured. Below are the causes and step-by-step fixes.
What the Docker Socket Is and How It Works
Docker uses a client-server architecture. The docker command you type is a client that sends requests to the dockerd daemon over a socket. By default, that socket is a Unix domain socket located at /var/run/docker.sock (which is a symlink to /run/docker.sock on most systems). The daemon listens on this socket for API calls, and the client must have read/write permission to it.
If the daemon is not running, the socket file doesn’t exist, and the client reports the connection error. If the daemon is running but the user isn’t in the docker group, the client gets a permission denied error, often phrased as “permission denied while trying to connect to the Docker daemon socket.”
Common Causes of the Error
- Docker daemon not started: On Linux systems using systemd, the service may be stopped or disabled.
- User not in the docker group: By default, only root and members of the
dockergroup can access the socket. - Docker not installed correctly: The daemon binary may be missing or the service unit file may be broken.
- Environment variable overrides:
DOCKER_HOSTcan point the client to a different socket or remote host, causing the local socket to be ignored. - WSL2 or virtualization issue: On Windows with WSL2, the Docker daemon may be running in the Windows side but not accessible from the Linux side without proper integration.
- Socket file permissions: Even if the daemon runs, the socket file may have restrictive permissions.
How to Fix the Error on Linux (systemd)
Most Linux distributions use systemd to manage services. The typical fix is to start the Docker service and enable it to start on boot:
- Open a terminal and run
sudo systemctl start docker. - To make it start automatically, run
sudo systemctl enable docker. - Check the status with
sudo systemctl status docker– you should see “active (running)”. - If it fails to start, view the logs with
journalctl -u dockerto see the exact error.
If the service starts but you still get permission errors, add your user to the docker group:
sudo usermod -aG docker $USER
Then log out and back in (or run newgrp docker) for the group change to take effect.
Fixing the Error on macOS and Windows
On macOS and Windows, Docker typically runs inside a virtual machine (Docker Desktop). The error usually appears when Docker Desktop is not running. Open Docker Desktop from the Applications folder (macOS) or the system tray (Windows) and wait for the whale icon to become steady. If it doesn’t start, check the Docker Desktop logs or restart the application.
On Windows with WSL2, ensure that Docker Desktop is set to use the WSL2 backend and that your Linux distribution has WSL integration enabled in Docker Desktop’s settings. If you’re using a Linux terminal inside WSL, you should not need to set DOCKER_HOST manually.
Checking Environment Variables and Socket Existence
Sometimes the client is pointed to the wrong place. Run the following to inspect your environment:
echo $DOCKER_HOST
If it returns a non-empty value, it’s overriding the default socket. Unset it with unset DOCKER_HOST or remove it from your shell profile. Also check if the socket file exists:
ls -l /var/run/docker.sock
If the file is missing, the daemon is not running. If it exists but the group is not docker, or the permissions are srw-rw----, you may need to adjust them. However, avoid changing permissions manually; instead, ensure the daemon is started with the correct flags.
When the Daemon Won’t Start
If systemctl start docker fails, there may be a configuration problem. Common issues include:
- A corrupted
/etc/docker/daemon.jsonfile – check for syntax errors. - Conflicts with iptables or firewalld – Docker needs to manage network rules.
- Storage driver issues – especially on older kernels.
Run sudo dockerd --debug to see the daemon’s output directly. This will print detailed logs that usually indicate the root cause. For example, if it says “failed to start containerd”, you may need to reinstall Docker.
Frequently Asked Questions
Why do I get “permission denied” even after starting Docker?
Your user account doesn’t have access to the socket. Add your user to the docker group as shown above, then log out and back in. If you’re in a container or a CI environment, you may need to run the command with sudo.
What does “Cannot connect to the Docker daemon at unix:///var/run/docker.sock” mean exactly?
It means the Docker client tried to open the Unix socket file but couldn’t. Either the file doesn’t exist (daemon not running) or the client doesn’t have permission to read/write it.
Can I use a different socket?
Yes, you can start the daemon with dockerd -H unix:///var/run/another.sock and point the client to it with DOCKER_HOST=unix:///var/run/another.sock. But this is rarely needed and can cause confusion.
Does this error occur on Windows or macOS?
Yes, but the cause is usually that Docker Desktop isn’t running. The socket path is the same because Docker Desktop creates a Linux VM that exposes the socket.
How do I fix this in a CI/CD pipeline?
In CI environments, the Docker daemon is often run as a separate service. Ensure the service is started before running Docker commands. If you’re using Docker-in-Docker, you may need to set DOCKER_HOST to the inner daemon’s socket.
Alternatives and Related Tools
If you’re using Podman, the equivalent command is podman, and it doesn’t require a daemon by default. For rootless Docker, you can run dockerd-rootless.sh to run the daemon without root privileges, which changes the socket path to something like /run/user/1000/docker.sock. This is useful for security reasons, but it requires additional setup.
If you’re working in a Kubernetes environment, you might be using containerd directly, which has its own socket. The error message would be different in that case.
Preventing the Error in the Future
To avoid this error, always ensure the Docker service is running before using Docker commands. On Linux, you can enable the service to start at boot with sudo systemctl enable docker. On macOS and Windows, set Docker Desktop to start when you log in. Also, add your user to the docker group to avoid permission issues.
If you frequently switch between Docker contexts, use docker context ls and docker context use to manage them. This prevents the DOCKER_HOST variable from pointing to the wrong daemon.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Access the DOM in Angular Safely
- Websites Create QR Code? How It Works
- Evaluate Private Cloud AI Security Operations Platforms
- Tongue Escape Events Schedule: Full Guide
- Space Engineers Server Name & Description Setup
- f/2.8 Telephoto Zoom: What to Consider