[FIX] Cannot Connect to the Docker Daemon at ‘unix:///var/run/docker.sock’

Docker is becoming more and more popular with its ability to create, deploy, and run applications using containers easily. Containers allow developers to package an application with all its libraries and dependencies before deploying it as one full package. Installing Docker on Linux is relatively easy. All you need to do is run a couple of commands in the Terminal, and you are good to go.

Docker connection error
Docker connection error

However, this might not be the same for all users, even after a successful installation. One of the most common arising error is: Cannot connect to the Docker daemon at (unix:///var/run/docker.sock. Is the docker daemon running?) This error can arise when running various docker commands like docker run, docker info, docker pull, docker stop, etc.

What causes the Error: Cannot Connect to the Docker Daemon?

After receiving numerous developers’ complaints, we decided to dive in and come up with effective solutions. Some of the reasons that trigger this error include:

  • The Docker daemon is not running.
  • Docker doesn’t shutdown cleanly.
  • Lack of root privileges to start the docker service.

Now that you have a basic understanding of the problem cause, let’s look at the various solutions available for you.

Solution 1: Start the Docker service with systemctl

If you have just completed a Docker’s fresh installation on Ubuntu or rebooted your PC, there is a high probability chance the Docker service is not running. Docker daemon (dockerd) is the system service for docker. This service handles various Docker objects like images, containers, networks, and volumes and listens to the Docker API requests.

The Systemctl command comes to replace the old SysV init system, and it manages systemd services running on Linux systems. If you don’t have systemctl in your system, don’t worry; use the service command as described below.

Note: This method only works for users who installed Docker with the APT package manager. If you installed Docker via SNAP, refer to Solution 5 below.

  1. Open the Terminal and execute the first command – unmask docker.
sudo systemctl unmask docker

If we try to start docker service when docker is masked, we might face the error ‘Failed to start docker.service: Unit is masked.’ Mask can be considered a more robust version of disabling. When a unit file is masked, the unit is linked to ‘dev/null.’ You can list the state of all unit files with the command – ‘$ systemctl list-unit-files

2. Once the docker unit is unmasked, we can start the docker daemon with the systemctl command. The docker daemon manages docker objects like Images, Containers, and Docker API requests. Execute the command below on the command-line.

systemctl start docker
Start Docker Service
Start Docker Service

3. To verify whether the docker service is active and running. We will use the systemctl status command, which shows the current status of the particular service. Execute the command below on your Terminal.

systemctl status docker
Docker service status
Docker service status

From the above image, we can see that the docker is active and running.

Solution 2: Clean a ‘Failed Docker Pull’ and Start Docker service

There are cases where you might unexpectedly close Docker while pulling a container. Such situations will mask the docker.service and docker .socket files. Docker.socket is a file located at ‘/var/run/docker.sock’ and is used to communicate with the Docker daemon. We will need to unmask the two-unit files – docker .service and docker.daemon before proceeding to start docker.

  1. Launch the Terminal and execute the commands below:
systemctl unmask docker.service
systemctl unmask docker.socket
systemctl start docker.service
Start Docker Service
Start Docker Service

If you are still experiencing the error even after executing the commands below, we will need to delete the files in the Containerd directory before starting Docker again. Containerd was a feature introduced in Docker 1.11 and is used to manage Docker images life-cycle.

2. Open Terminal and execute the commands below. Ensure you know the root password since we will need elevated privileges to execute the commands.

sudo su
service docker stop
cd /var/run/docker/libcontainerd
rm -rf containerd/*
rm -f docker-containerd.pid
service docker start
Restart docker service
Restart docker service

Solution 3: Start Dockerd (Docker Daemon) Service

Dockerd is the Docker daemon which listens to Docker APIs and manages the various Docker objects. Dockerd can be used as an alternative to the command ‘$ systemctl start docker‘ which is also used to start the Docker daemon.

  1. Open Terminal and start dockerd by executing the command below:
sudo dockerd
Start dockerd
Start dockerd

Solution 4: Start Docker with the Service command

If you are using the SysV init system, then the systemctl command will not work for you. We will need to use the service command to start docker daemon.

  1. launch the Terminal and execute the commands below:
sudo service --status-all
sudo service docker start
Start Docker service
Start Docker service

Solution 5: Start the Docker Service with Snap

If you installed Docker with the Snap package manager, you would need to use the snap command to manage the docker daemon.

Generally, Snap manage their services automatically. However, in situations such as this error, it will require manual intervention. Some of the arguments you can use with the snap command include stop, start, and restart. In our case, we will use the start parameter.

  1. Open Terminal and execute the command below to start Docker.
sudo snap start docker
Start Docker
Start Docker

2. Execute the command below to verify whether the Docker service was started.

sudo snap services

That will list all running snap services.

Snap Services
Snap Services

If the above commands don’t work for you, try connecting the docker:home plug since it’s not auto-connected by default. Once done, start the Docker service.

3. Launch the Terminal and run the commands below:

sudo snap connect docker:home :home
sudo snap start docker
Start Docker
Start Docker

Solution 6: Start Docker for users without Root Privileges

The error might also arise due to lack of elevated privileges and the user doesn’t have access to ‘unix:///var/run/docker.sock.’ Luckily there is a workaround. We will export the Docker Host variable to the localhost via port 2375.

  1. Open the Terminal and run the command below:
export DOCKER_HOST=tcp://localhost:2375
Export DockerHost
Export Docker Host

Solution 7: Reinstall Docker

If the above solutions don’t solve the error, there is a high probability chance that you might have installation errors. To correctly install Docker in your Linux system, follow the steps from the Docker official website.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.