How to Resolve the "Cannot connect to the Docker daemon" Error

November 23, 2023

Introduction

Docker daemon is a background process used by the Docker engine to manage containers, images, and other aspects of Docker via API requests. The "Cannot connect to the Docker daemon" error appears if the engine has a problem communicating with the daemon.

This tutorial covers the possible causes of the "Cannot connect to the Docker daemon" error and how to solve it.

How to resolve the "cannot connect to Docker daemon" error

Prerequisites

Resolving the "Cannot connect to the Docker daemon" Error

The "Cannot connect to the Docker daemon" error occurs when the user attempts to issue a command using the Docker CLI. Below is an example of the error shown when you list running containers:

An example of the Cannot connect to Docker daemon error.

The sections below provide methods to troubleshoot and fix the error. If one solution does not work, move on to the next until you resolve the issue.

Method 1: Check User Privileges

Insufficient privileges can sometimes be the reason for the "Cannot connect to the Docker daemon" error. To ensure this is not the cause of the issue, try adding sudo to the docker command:

sudo docker [subcommand] [options]

If sudo solves the problem, follow the steps below to access Docker CLI without the sudo command.

1. Create the docker group if it does not already exist:

sudo groupadd -f docker

2. Add the current user to the docker group via the usermod command:

sudo usermod -aG docker $USER

3. Log out, then log back in.

4. Start the Docker service again with:

sudo service docker start

Method 2: Check Docker Service

If the Docker service is not running, commands issued from the CLI cannot access the daemon. The steps below explain how to troubleshoot this issue.

1. View the status of the Docker service:

sudo service docker status

The Active section should report the service as active (running). Any other message means that the service is experiencing a problem.

Checking the Docker service status to resolve the "cannot connect to the docker daemon" error.

2. If the Docker service is inactive, start it with the following command:

sudo service docker start

Now, you can attempt to run the command that reported the error.

Method 3: Start Docker Daemon Manually

Starting the Docker service also starts the Docker Daemon. However, if you receive the "Cannot connect to the Docker daemon" error, the daemon may have failed to initiate.

Use the following command to run the daemon manually:

sudo dockerd

As the daemon initiates, the startup process information is displayed in the output.

Manually running the Docker daemon.

If the daemon starts successfully, you can run Docker commands in another terminal window. If an error occurs, the output provides a specific reason for the error.

Method 4: Assign Ownership to Docker Unix Socket

The "Cannot connect to the Docker daemon" error also occurs if the Unix socket file for Docker does not have the correct ownership assigned.

1. Check ownership for the Docker Unix socket:

sudo ls -l /var/run/docker.sock

The expected owner of the socket is root, as shown in the example below:

Checking the ownership for the Docker Unix socket to resolve "cannot connect to the docker daemon" error.

2. If the owner is a user not currently logged into the system, change the ownership to root by typing:

sudo chown root:docker /var/run/docker.sock

Alternatively, grant the ownership to the current user:

sudo chown $USER:docker /var/run/docker.sock

Method 5: Switch to Default Docker Context

In some cases, installing Docker Desktop may cause the CLI commands to be forwarded to Docker Desktop instead of the Docker engine. Changing back to the default context may resolve the issue.

1. Check the current context of the Docker deployment:

docker context ls
Checking Docker context.

2. If the value in the name column is anything other than default, switch back to the default context by typing:

docker context use default

Run a Docker command to see if the error has been fixed by setting the default context.

Method 6: Check File Ownership

Ownership issues can also extend to files used by your Docker build. If Docker needs to use a file it cannot access, the "Cannot connect to the Docker daemon" error may appear.

Follow the steps below to troubleshoot this issue:

1. Run the docker build command for each container. It gives you a detailed output that points out any potential errors.

Using the docker build command to get a more detailed output to resolve "cannot connect to the docker daemon" error.

2. Check the output for each container, looking for a "Cannot connect to the Docker daemon" error report. If there is a problem with the file ownership, the error report lists the files the docker build command cannot access.

3. There are several ways to resolve the issue of ownership of used files:

  • Remove the files in question by deleting them. However, this deletion affects other builds using the files.
  • Another method is to add the .dockerignore file to your current build, thus excluding the files your build cannot access.
  • Finally, you can change the file ownership with:
sudo chown $USER:docker [path-to-file]

Conclusion

After following this tutorial, you should know the potential causes of the "Cannot connect to the Docker daemon" error and how to address each by following one of the methods.

Next, learn Docker container management best practices for an efficient and safe Docker environment.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
10 Docker Security Best Practices
September 14, 2020

This article provides 10 container security tips that can help you prevent attacks and privilege breaches...
Read more
How To Install and Use Docker on Ubuntu 20.04
April 6, 2023

Install Docker on Ubuntu 20.04 using the default repository or from the official Docker repository with the...
Read more
How to Update Docker Image and Container to the Latest Version
July 11, 2024

To avoid running containers with outdated Docker images, update the image and run the container with the...
Read more
Docker Volumes: How to Create & Get Started
July 27, 2020

Persist data in Docker containers by mounting Docker volumes to containers. You can use an existing host...
Read more