How to Fix Docker Connection Refused on Debian 11


The Root Cause

On Debian 11, “Docker Connection Refused” often signifies that the Docker daemon is either not actively running, or the current user lacks the necessary permissions to access the Docker Unix socket located at /var/run/docker.sock. By default, this socket requires either root privileges or membership in the docker group to establish a connection, leading to refusal if these conditions are not met.

Quick Fix (CLI)

# 1. Check if the Docker daemon is running
sudo systemctl status docker

# If Docker is not running (state will not be 'active (running)'), start it:
sudo systemctl start docker
sudo systemctl enable docker # Ensure it starts on boot

# 2. Add your current user to the 'docker' group to gain socket access
# Replace $USER with your actual username, or use the variable directly
sudo usermod -aG docker $USER

# 3. Apply the new group membership. Either log out and log back in,
# or run the following command (note: this only affects the current terminal session)
newgrp docker

Configuration Check

The core configuration issue resides with user group membership, stored within the system’s /etc/group file. While direct manual editing of /etc/group is not recommended, the sudo usermod -aG docker $USER command modifies this file to append your user to the docker group.

To verify the user’s groups configuration after the fix (before relogging or using newgrp), you can inspect the /etc/group file for the docker entry:

grep docker /etc/group

You should see an entry like docker:x:999:yourusername (where 999 is an example GID).

Verification

After performing the quick fix steps and ensuring your group membership is active (by logging out/in or using newgrp), run a simple Docker command:

docker run hello-world

A successful output will indicate that Docker is accessible and functioning correctly, displaying a message about the “Hello from Docker!” container. If this command runs without “Connection Refused” or permission errors, the issue is resolved.