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.
Prerequisites
- Access to the command line or terminal.
- A working Docker installation.
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:
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.
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.
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:
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
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.
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.