DEV Community

Cover image for Are you Facing ‘Docker Permission Denied’ error! Let’s fix it and get you started with your shipping! 😵😕🔍
Khushal Sarode
Khushal Sarode

Posted on

Are you Facing ‘Docker Permission Denied’ error! Let’s fix it and get you started with your shipping! 😵😕🔍

Stop typing 'sudo' before every single Docker command. Learn how to instantly and securely fix the docker.sock permission denied error.

How to Fix the Docker Permission Denied Error (docker.sock)

Have you ever run a docker ps or docker build only to be met with this frustrating message?

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

This is a common issue when setting up a new Linux machine or cloud VM. Let’s explore why it happens and how to fix it for good.


The Issue

When you run Docker commands, your terminal shows a permission denied error. This forces you to add sudo to every command, which disrupts automated scripts and becomes a hassle to type.


Why This Happens

Docker uses a Unix socket file located at /var/run/docker.sock to connect the CLI tool you use with the background service that runs the containers.

What exactly is /var/run/docker.sock?

The /var/run/docker.sock file is a Unix domain socket that enables communication between the Docker CLI and the daemon. It typically needs root permissions.

By default, root owns this socket file. For security reasons, regular Linux user accounts are not allowed to access it.


Two Ways to Fix It

1. The Proper Way (Recommended)

This method gives your specific user account permanent and secure access by adding it to a specific security group.

  • Step 1: Create the Docker system group (it usually exists, but just in case):
  sudo groupadd docker
Enter fullscreen mode Exit fullscreen mode
  • Step 2: Add your current user account to that group:
  sudo usermod -aG docker \$USER
Enter fullscreen mode Exit fullscreen mode
  • Step 3: Activate the group changes right away without logging out:
  newgrp docker
Enter fullscreen mode Exit fullscreen mode

Test it by running docker run hello-world without sudo!

2. The Quick Way

If you are in a hurry, working on a temporary VM, and just need it to work right now, you can take a shortcut.

  • The Command:
  sudo chmod 666 /var/run/docker.sock
Enter fullscreen mode Exit fullscreen mode
  • The Catch: This permission change goes away when you reboot the machine. Additionally, it lets any user or process on that machine read and write to Docker, which is a significant security risk in production.

Conclusion

The quick way is fine for a brief local test, but the proper way is the best practice. It persists through reboots and keeps your environment safe. Take the extra 5 seconds to set up the user group— Enjoy 🐬📦🏗!
Happy Learning!!😊👨‍💻📌

Top comments (0)