free webpage hit counter

How to troubleshoot “Can’t connect to docker daemon” errors

A graphic showing the Docker logo

Docker is one of the leading platforms for building and running software containers. It comes with everything you need to use containers on a single host or multiple distributed nodes in swarm mode.

Docker has a daemon-based architecture. The software responsible for creating and starting containers is independent of the CLI process that receives your commands. This means that if you try to run commands without an active daemon connection you will see errors in the CLI. In this article, we share some methods to overcome these frustrating messages.

Symptoms of the problem

The Docker CLI depends on an available daemon connection. It communicates with the daemon using API calls. When a configured daemon is inaccessible, docker Commands like docker ps, docker runAnd docker build shows an error message similar to:

$ docker run hello-world:latest
Cannot connect to the Docker daemon at unix:///var/run/docker.sock
Is the docker daemon running?

This reveals that it tried to communicate with the Docker daemon using the CLI /var/run/docker.sock Unix socket. Socket not open so connection failed.

1. Check if Docker daemon service is running

The Docker daemon is usually managed by the systemd service which automatically starts Docker after your host reboots. You can start troubleshooting by checking if this service is running:

$ sudo systemctl status docker
docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: inactive (dead)

Service should be reported Active: active (running) If the daemon is running. As the example above shows inactive (dead) That means the daemon has stopped.

Start Docker using the following command:

$ sudo systemctl start docker

Now you should be able to run docker The CLI commands successfully.

After you reboot your machine you will find that Docker remains in a stopped state. You can solve this by enabling the service, allowing systemd to start automatically:

$ sudo systemctl enable docker
$ sudo systemctl daemon-reload

The daemon-reload The command instructs systemd to reload its configuration to apply the change.

2. Start the daemon manually

You may sometimes use a system that does not have the Docker service installed. You can manually start using the Docker daemon dockerd command It should be run normally root.

$ sudo dockerd
INFO[2022-06-29T15:12:49.303428726+01:00] Starting up

Docker is accessible as long as the command is running. Use Ctrl+C to stop the daemon.

3. Verifying the CLI targets the correct daemon

Problems may occur when the CLI tries to connect to a remote Docker daemon instance. This is usually caused when the error message shows the TCP address:

$ docker run hello-world:latest
Cannot connect to the Docker daemon at tcp:///0.0.0.0:2375

In this example, the docker Trying to connect to Docker daemon in CLI 0.0.0.0:2375 Use TCP instead of native Unix Docker socket. This will fail if the Docker daemon’s TCP support is disabled or if the specified host is not accessible on the network.

You can usually solve this by switching to the correct Docker CLI context for the daemon connection you want to use:

$ docker context use default

You can list all available instances and the daemon endpoints they connect to context ls command:

$ docker context ls
NAME        DESCRIPTION                               DOCKER ENDPOINT             
default *   Current DOCKER_HOST based configuration   unix:///var/run/docker.sock

The currently selected context is highlighted with an asterisk.

Unexpected values ​​in DOCKER ENDPOINT A column usually arises DOCKER_HOST Setting an environment variable. In this case you will see a warning:

$ export DOCKER_HOST=1.2.3.4
$ docker context ls
NAME        DESCRIPTION                               DOCKER ENDPOINT
default *   Current DOCKER_HOST based configuration   tcp://1.2.3.4:2375
Warning: DOCKER_HOST environment variable overrides the active context. To use a context, either set the global --context flag, or unset DOCKER_HOST environment variable.

the presence DOCKER_HOST An environment variable in your shell overrides the endpoint defined by your selected context. In this example, docker Commands always target a daemon instance tcp://1.2.3.4:2375.

This problem can be solved by clearing DOCKER_HOST Variable:

$ export DOCKER_HOST=

Docker will now use the endpoint configured by your active context. This will be the default local Unix socket /var/run/docker.sock Unless you manually set a custom context.

$ docker context ls
NAME        DESCRIPTION                               DOCKER ENDPOINT             
default *   Current DOCKER_HOST based configuration   unix:///var/run/docker.sock

4. Permissions Issues

Another common cause of daemon connection problems is incorrect user permissions on Docker’s socket. This type of problem usually shows a slightly different error message:

$ docker run hello-world:latest
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

This happens when your Unix user account does not have permission to communicate with the socket that exposes the Docker API. Adding yourself to docker A group is a good practice way to solve this problem:

$ sudo usermod -aG docker $USER

You must open a new shell window or logout and back in for this change to take effect. You should be able to run now docker Commands without running into permissions issues.

Summary

“Unable to connect to Docker daemon” appears when the Docker CLI is unable to communicate with the Docker daemon instance using your current configuration. This is most likely because the Docker daemon service has been stopped or disabled. You may be trying to connect to a remote Docker host that is offline.

You should now be aware of the possible causes of this problem and common ways to resolve it. Resolve the error by checking your Docker daemon settings, restarting the Docker service, and making sure your user account has permission to communicate with Docker’s socket.

Leave a Reply

Your email address will not be published.

Previous post How to integrate Google Drive into Slack
Next post How to make a fire safety plan for your home