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. 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. 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. 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. 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.
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.
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.
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. 2. Execute the command below to verify whether the Docker service was started. That will list all running 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:
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.
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.
Fix: Cannot Connect to the Docker DaemonFix: Could not open lock file /var/lib/dpkg/lockFix: Could not open file /var/lib/dpkg/statusWhat is Microsoft AU Daemon?