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

When the error “Cannot connect to the Docker daemon at ‘unix:///var/run/docker.sock‘” appears, commands like `docker run` or `docker ps` won’t work, preventing the user from working with Docker containers. This error means that the Docker client cannot talk to the Docker daemon, which handles Docker operations.

Docker connection error
Docker connection error

The most common reason for this problem is that the Docker service is not running on the host system. Other reasons include the user not having enough permissions for the Docker socket, a misconfigured Docker installation, or issues with the docker.sock file.

Now that you know the causes, let’s discuss the solutions.

1. Start the Docker Service with systemctl

Ensuring Docker is running correctly is essential, and starting the Docker service with systemctl helps with this. If the service isn’t active, the Docker client can’t communicate with the main process, causing the error. Using the systemctl start docker command starts the service, allowing Docker to manage containers and work properly again.

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

    If we try to start the Docker service when Docker is masked, we might face the error ‘Failed to start docker.service: Unit is masked.’ A 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

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

    Docker service status

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

2. Clean a ‘Failed Docker Pull’ and Start Docker Service

When a Docker pull fails, it can leave incomplete or broken parts that mess up the daemon’s operations. Removing these leftovers ensures Docker is in a clean state. Restarting the Docker service restarts the daemon, fixing problems like stuck processes or incorrect startup settings. This approach helps restore proper function and fixes communication errors effectively.

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

    Start Docker ServiceStart Docker ServiceIf 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

3. Start Dockerd (Docker Daemon) Service

Dockerd is the Docker daemon that listens to Docker APIs and manages 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

4. Start Docker with the Service Command

Manually starting Docker makes sure the service is up and ready to handle requests. Using the `sudo service docker start` command starts the Docker daemon, solving problems that occur when it’s not running. This step is especially helpful after restarting your computer or when the daemon doesn’t start automatically.

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

5. Start the Docker Service with Snap

If Docker was installed using Snap and the service isn’t running, manually starting it can help fix the issue. Snap packages often need you to manage services yourself. Running `sudo snap start docker` makes sure the Docker daemon is on and ready to handle client requests.

  1. Open Terminal and execute the command below to start Docker.sudo snap start dockerStart Docker
  2. Start DockerExecute the command below to verify whether the Docker service was started.
    sudo snap services
  3. This will list all running snap services.Snap Services
  4. Snap ServicesIf 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.
  5. Launch the Terminal and run the commands below:
    sudo snap connect docker:home :home sudo snap start docker

    Start Docker

6. Start Docker for Users without Root Privileges

The error might also arise due to a lack of elevated privileges when 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

7. Reinstall Docker

Sometimes, problems with the Docker installation, like corrupted files or incorrect settings, can block communication with the Docker daemon. Reinstalling Docker resets everything to its original state, fixing any errors caused by incomplete setups or conflicting settings.

  1. Manually starting Docker makes sure the service is up and ready to handle requests. Use the sudo service docker start command to start the Docker daemon, fixing problems if it isn’t active. This step is especially helpful after restarting the system or if the daemon doesn’t start on its own.
  2. Ensuring the Docker daemon is running is important because it serves as the core of Docker, managing containers. Starting the dockerd service re-establishes the connection between the Docker client and daemon, fixing issues caused by the service being inactive or stopping unexpectedly.

By adding your user to the Docker group, you get direct access to the Docker service, fixing errors caused by restricted access to the docker.sock file. This method not only solves the problem but also makes things easier by preventing the need for repeated permission requests.

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.