How to Fix Docker Connection Refused on Ubuntu 22.04
The Root Cause
This error on Ubuntu 22.04 often indicates that the Docker daemon is either not running or the current user lacks the necessary permissions to access the Docker Unix socket /var/run/docker.sock. By default, only the root user and members of the docker group have read/write access to this socket, leading to “permission denied” or “connection refused” for other users.
Quick Fix (CLI)
# 1. Verify Docker daemon status
sudo systemctl status docker
# If the status is 'inactive (dead)', start the daemon
sudo systemctl start docker
# Enable Docker to start automatically on system boot
sudo systemctl enable docker
# 2. Add your current user to the 'docker' group
sudo usermod -aG docker $USER
# 3. Apply the new group membership (log out and back in for full effect, or use newgrp)
newgrp docker
Configuration Check
The primary configuration file for the Docker daemon is /etc/docker/daemon.json. While this file primarily controls daemon behavior, a misconfiguration or absence can prevent the daemon from starting, indirectly leading to “connection refused.”
File: /etc/docker/daemon.json (create if it does not exist)
Ensure the file contains valid JSON and appropriate settings. A basic, valid configuration often sufficient for troubleshooting (if you suspect a malformed existing file) is:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
After modifying or creating daemon.json, restart the Docker daemon:
sudo systemctl restart docker
Verification
To verify the fix, execute a Docker command that interacts with the daemon, ensuring it runs without sudo and without errors.
docker ps
docker run hello-world