Docker is a popular way to build and run software containers. You can use it to work with containers on a single computer or across multiple computers using Swarm mode.
Docker uses a special kind of architecture called “daemon-based”. This means that the program that actually creates and starts containers is separate from the program that accepts your commands. So if you try to run commands without an active connection to the daemon, you might see some errors in the program that accepts your commands. In this article, we’ll show you how to deal with these annoying error messages.
To use the Docker CLI, you need to have a connection to the daemon. The CLI talks to the daemon by making API calls. If the daemon is not available, you might see error messages like the following when you try to run commands like “docker ps”, “docker run”, or “docker build”:
This means that the Docker CLI attempted to connect to the Docker daemon using the Unix socket located at /var/run/docker.sock, but the socket was closed, which resulted in the connection failing.
Here are some tips that can help you solve the error: Cannot Connect to the Docker Daemon.
Normally, the Docker daemon is controlled by a systemd service that starts Docker automatically whenever your computer restarts. To start investigating the issue, you should verify whether this service is currently running:
sudo systemctl status docker
You can check our example here, how the output will look like.
To start Docker, run the command:
sudo systemctl start docker
You should now be able to execute Docker CLI commands without any issues.
Sometimes after a machine reboot, Docker may remain in the stopped state. To resolve this, you can enable the service and allow systemd to automatically start it. Here is how to do it :
sudo systemctl enable docker
sudo systemctl daemon-reload
Difficulties may arise when attempting to establish a connection between the CLI and a remote Docker daemon instance. This is typically the case if the error message displays a TCP address.
Cannot connect to the Docker daemon at tcp://localhost:2375/
The problem in this situation is that the docker CLI is attempting to communicate with the Docker daemon at tcp://localhost:2375/ using TCP, rather than through the local Unix Docker socket. This will not work if the Docker daemon’s TCP functionality is disabled, or if the given host cannot be reached on the network.
To fix this issue, you can usually switch to the correct Docker CLI context for the desired daemon connection. This can be done by using:
docker context use default
Another reason for daemon connection issues is when the user permissions for Docker’s socket are incorrect. If this is the case, the error message displayed may be slightly different:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
When you don’t have the appropriate permissions to interact with the Docker API’s socket, it means that your Unix user account lacks the required access. To fix this issue, it is best practice to add your account to the docker group:
sudo usermod -aG docker $USER
After making the change by adding yourself to the docker group, you’ll need to open a fresh shell window or log out and then log back in to ensure the changes take effect. Once you do that, you should be able to execute docker commands smoothly without encountering any permission-related issues.
If you are working on a system that does not have the Docker service installed, it is possible to initiate the Docker daemon manually by executing the “dockerd” command. Typically, you would need root privileges to perform this action.
As long as the command is running, you’ll be able to access Docker. To stop the daemon, use Ctrl+C.
If you see the error message “Cannot connect to the Docker daemon,” it means that the Docker CLI is having trouble communicating with the Docker daemon using your current settings. This could be due to the Docker daemon service being turned off or disabled, or you could be trying to connect to a remote Docker host that’s no longer available. Now that you know the possible reasons for this issue and the typical ways to address it, you can troubleshoot the error.
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.