If you’ve ever encountered the frustrating Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running?
error, you’re not alone.
This is one of the most common issues Docker users face, and it has stumped even experienced developers. As someone who’s battled this error countless times across different environments, I’m sharing my complete troubleshooting guide to help you get your Docker environment back up and running.
Before diving into solutions, let’s understand what this error actually means. When you run a Docker command, your Docker client tries to communicate with the Docker daemon (dockerd) through a Unix socket at /var/run/docker.sock
.
Read: How to install and setup Docker on Ubuntu 22.04
Depending on your operating system, here are the most effective solutions:
Try these commands in order until the issue is resolved:
# Start the Docker service
sudo service docker start
# If that doesn't work, try restarting it
sudo service docker restart
# For systemd-based distributions
sudo systemctl start docker
sudo systemctl enable docker
The most common issue on macOS is forgetting to launch Docker Desktop:
If you’re using Windows Subsystem for Linux:
# Start the Docker service in WSL
sudo /etc/init.d/docker start
# Or for some distributions
sudo service docker start
If the quick solutions didn’t work, let’s go through a more methodical approach:
First, let’s verify if the Docker daemon is actually running:
ps aux | grep docker
You should see output containing dockerd
if the daemon is running. If not, you’ll need to start it.
Depending on your system’s init system, use one of these commands:
For systemd (most modern Linux distributions):
sudo systemctl start docker
sudo systemctl enable docker # Enable on boot
For older init systems:
sudo service docker start
If you need to start the daemon manually:
sudo dockerd
Note: Running sudo dockerd
will start Docker in the foreground, which means you’ll need to keep that terminal window open.
Sometimes the Docker service might be “masked,” which prevents it from being started:
sudo systemctl status docker
If you see “masked” in the output, unmask it:
sudo systemctl unmask docker.service
sudo systemctl unmask docker.socket
sudo systemctl start docker
If you’re getting the error even though the daemon is running, you might not have permission to access the Docker socket:
# Add your user to the docker group
sudo usermod -aG docker $(whoami)
# Apply the new group membership without logging out
newgrp docker
# Verify your user is in the docker group
groups
After adding yourself to the Docker group, try running a Docker command without sudo
.
Read: How to Set Environment Variables in Docker
In newer Docker versions, especially on Ubuntu 22.04+, the issue might be related to Docker contexts:
# List available contexts
docker context ls
# Switch to the default context
docker context use default
If Docker isn’t starting properly, it might be due to a corrupted socket or PID file:
# For standard Docker installations
sudo rm -f /var/run/docker.pid
# For snap installations
sudo rm -f /var/snap/docker/[version]/run/docker.pid
sudo snap stop docker
sudo snap start docker
Docker requires sufficient disk space to operate. Check your available space:
df -h
If your disk is full, clean up some space and try starting Docker again.
Check if your Docker daemon configuration is valid:
cat /etc/docker/daemon.json
Ensure the file contains valid JSON. Common mistakes include missing commas or brackets. If needed, correct the file and restart Docker.
Sometimes Docker has issues with network configurations:
# For Debian/Ubuntu systems using nftables instead of iptables
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
sudo service docker restart
If you’re using Docker Desktop on macOS and still experiencing issues:
If you’re using Docker Desktop with WSL2:
If you’re trying to use Docker inside a Docker container, you’ll need to mount the host’s Docker socket:
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
Then set the correct permissions:
sudo chown $USER:$USER /var/run/docker.sock
sudo service docker stop
sudo systemctl enable docker
sudo apt-get update
sudo apt-get upgrade docker-ce
docker info
When you restart your computer, the Docker daemon doesn’t always start automatically unless you’ve configured it to do so. Use sudo systemctl enable docker
to ensure Docker starts on boot.
Ideally, no. If you add your user to the Docker group (sudo usermod -aG docker $(whoami)
), you should be able to run Docker commands without sudo after logging out and back in.
Unlike Linux where Docker can run natively, Docker on macOS and Windows runs inside a lightweight VM. Docker Desktop manages this VM and provides the necessary integration with your host OS.
Run ps aux | grep docker
to see if the dockerd process is running, or use sudo systemctl status docker
on systemd-based systems.
Yes, if Docker isn’t running. The .pid file just stores the process ID of the running Docker daemon. If Docker crashes, this file might be left behind and prevent Docker from starting again.
While some solutions suggest exposing Docker on TCP port 2375, this is not recommended for production environments as it can pose security risks. Only do this in isolated development environments.
The “Cannot connect to Docker daemon” error can be frustrating, but in most cases, it’s solvable with the right approach. By understanding the underlying causes and following this troubleshooting guide, you should be able to get your Docker environment running smoothly again.
Remember, the most common fixes are simply starting the Docker service, ensuring proper permissions, or launching Docker Desktop on macOS/Windows. For more complex issues, a systematic approach checking the daemon status, socket permissions, and configuration files will usually resolve the problem.
Have you encountered any other Docker daemon issues not covered here? Let me know in the comments below!
This article was last updated on March 9, 2025, and applies to Docker versions up to that date.
The post How to fix “Cannot Connect to the Docker Daemon” Error appeared first on net2.
If you’ve ever tried compiling a Linux kernel from source—whether to add custom system calls,…
Security is paramount, and one of the first lines of defense for any system, whether…
If you’re managing Ubuntu servers or desktops, understanding systemd is absolutely essential. systemd is the init system and…
If you’ve recently upgraded to Ubuntu 22.10 from version 22.04, you might have encountered an…
When developing software, particularly in languages like C and C++, crashes are inevitable. The dreaded…
Have you ever been working in your Ubuntu terminal when suddenly that jarring error sound…