You run a Docker command and get this:
This happens because Docker runs as root by default, and your user doesn’t have permission to access the Docker socket. Here’s how to fix it properly.
The Quick Fix (Not Recommended for Production)
You could just use sudo for everything:
| |
This works but gets tedious fast. You’ll need sudo for every Docker command, and it breaks tools that expect Docker access without elevation.
The Right Fix: Add Your User to the Docker Group
Docker creates a docker group during installation. Members of this group can access the Docker socket without sudo.
Step 1: Create the Docker Group (If It Doesn’t Exist)
| |
If you get “group ‘docker’ already exists,” that’s fine — move to step 2.
Step 2: Add Your User to the Group
| |
The -aG flags mean “append to group” — this adds Docker to your existing groups without removing any.
Step 3: Apply the Group Change
Here’s where most tutorials fail you. The group change doesn’t take effect until you log out and back in. You have three options:
Option A: Log out and back in
Close your terminal, log out of your desktop session, log back in.
Option B: Use newgrp (temporary)
| |
This starts a new shell with the docker group active. Works for testing but doesn’t persist.
Option C: Reboot
| |
Nuclear option, but guaranteed to work.
Step 4: Verify It Worked
| |
If you see “Hello from Docker!” without needing sudo, you’re done.
Why This Happens
The Docker socket (/var/run/docker.sock) has these permissions:
| |
- Owner:
root(can read/write) - Group:
docker(can read/write) - Others: no access
When your user isn’t in the docker group, you’re “others” — no access.
Common Gotchas
“I added myself to the group but it still doesn’t work”
You didn’t re-login. The groups command shows your current session’s groups:
| |
If docker isn’t listed, your session hasn’t picked up the change. Log out and back in.
“It worked yesterday but not today”
The Docker daemon might have restarted and recreated the socket with different permissions. Check if Docker is running:
| |
Restart it if needed:
| |
“I’m in the docker group but /var/run/docker.sock doesn’t exist”
Docker daemon isn’t running:
| |
“Permission denied inside a container trying to use Docker”
You’re trying to run Docker-in-Docker or access the host’s Docker from a container. You need to mount the socket:
| |
Security warning: This gives the container full control over your host’s Docker daemon. Only do this with containers you trust completely.
Security Implications
Adding a user to the docker group is essentially giving them root access. Anyone who can run Docker containers can:
| |
This is equivalent to root. Only add trusted users to the docker group.
For multi-user systems or production, consider:
- Rootless Docker: Runs the daemon as your user, no privilege escalation
- Podman: Daemonless container runtime, rootless by default
- Restricted socket access: Use Docker contexts with TLS authentication
TL;DR
| |
That’s it. The permission error happens because your user can’t access the Docker socket. Adding yourself to the docker group fixes it, but remember to re-login for the change to take effect.