How to Fix Docker: Permission Denied

When working with Docker, you might come across this frustrating error:

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

This error usually means your user doesn’t have the right permissions to interact with the Docker daemon. The good news? It’s a common issue and easy to fix.

In this blog post, you’ll learn why this error occurs, how to fix it, and how to avoid it in the future.


🔍 Why You See “Permission Denied” in Docker

Docker commands like docker run, docker ps, or docker build require access to the Docker daemon, which runs as a root-level process.

By default:

  • Only users with root privileges or who belong to the docker group can interact with the daemon.
  • If you’re not in the right group, you’ll get a permission denied error.

✅ Quick Fix: Use sudo

If you just need a quick workaround:

sudo docker ps

You can prepend sudo to any Docker command.

But this isn’t ideal long-term. Instead, let’s fix the root issue.


✅ Permanent Fix: Add Your User to the Docker Group

Step 1: Add your user to the Docker group

sudo usermod -aG docker $USER
  • -aG: appends the user to the group without removing other group memberships.
  • $USER: automatically references your current username.

Step 2: Log out and log back in

This step is crucial. Group membership changes won’t take effect until your session is restarted.

You can:

  • Log out and back in
  • Or reboot your machine
  • Or use newgrp docker to refresh the group membership without logout

Step 3: Verify the fix

Run:

docker ps

If you see a list of containers (or an empty list), you’re good to go!


🧠 Check if You’re in the Docker Group

Use this command:

groups

If docker isn’t listed, repeat the steps above.


🧪 Bonus: Check Docker Socket Permissions

The Docker daemon listens on a UNIX socket at /var/run/docker.sock. You can inspect its permissions:

ls -l /var/run/docker.sock

Expected output:

srw-rw---- 1 root docker 0 Jun 30 12:00 /var/run/docker.sock
  • The socket should be owned by the docker group.
  • If it’s not, reinstalling Docker or restarting the daemon may help.

❗ Common Mistakes to Avoid

MistakeFix
Forgetting to log out after usermodLog out and back in or reboot
Not installing Docker correctlyFollow the official Docker installation guide
Using sudo with scripts and aliasesCheck PATH and permissions carefully

✅ Summary

ProblemSolution
Permission denied errorAdd your user to the Docker group
Want to test quicklyUse sudo with Docker commands
Need changes to take effectLog out and back in or restart your session
Not sure what’s wrongCheck Docker socket permissions

🔐 Final Thoughts

Docker permission errors are common, especially right after a fresh install. By properly setting group permissions, you can ensure smoother, safer workflows without needing to prefix everything with sudo.

💡 Pro tip: Avoid running Docker as root unless absolutely necessary—stick to using the Docker group for better security.

Sharing Is Caring:

Leave a Comment