Running Docker can sometimes result in a frustrating error:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
This typically occurs when Docker commands are executed without the necessary permissions, especially on Linux systems.
In this blog, we’ll explore why this happens, and how to fix it safely and correctly.
🚫 Why You’re Getting “Permission Denied”
The error means your user is not part of the docker group, which is required to access the Docker daemon (/var/run/docker.sock).
By default, only the root user (or users with root privileges) can interact with the Docker daemon.
✅ Solutions to Fix “Permission Denied”
🔧 1. Use sudo with Docker commands
The simplest fix is to prepend sudo to your Docker commands:
sudo docker ps
sudo docker run hello-world
But using sudo every time is cumbersome. Let’s fix it permanently.
👤 2. Add Your User to the Docker Group
This allows your user to run Docker commands without sudo.
Step-by-step:
# Create the docker group (if it doesn't exist)
sudo groupadd docker
# Add your user to the docker group
sudo usermod -aG docker $USER
After running the command, log out and log back in (or reboot) to apply the group changes.
Verify:
docker run hello-world
If it runs without an error, you’re all set!
🧼 3. Check Docker Daemon is Running
Sometimes, the error may also happen if the Docker service isn’t running.
Start or restart the Docker service:
sudo systemctl start docker
# or
sudo systemctl restart docker
Then re-run your Docker commands.
🔒 4. Check Socket File Permissions
Ensure the Docker socket is accessible by the docker group:
ls -l /var/run/docker.sock
Expected output:
srw-rw---- 1 root docker 0 Jul 15 10:00 /var/run/docker.sock
If the group is incorrect, fix with:
sudo chown root:docker /var/run/docker.sock
🛡️ Warning: Do Not Use chmod 777
While it may fix the error, giving full permissions (777) to /var/run/docker.sock is a major security risk. Avoid this unless you’re in a secure dev environment and understand the implications.
📌 Summary
| Fix | Use Case |
|---|---|
sudo docker | Quick fix |
Add user to docker group | Permanent fix |
| Start Docker daemon | If not running |
| Check socket permissions | Advanced fix |
🎯 Final Note
Once you’ve added yourself to the Docker group, log out and log back in to apply changes. If you’re using WSL, restart your terminal or WSL session.
By applying the correct fix, you’ll ensure Docker works smoothly and securely on your system.