Docker Permission Denied (docker.sock) Error: The Safe Fix

Short answer
What is the docker permission denied error?
The docker permission denied error appears when your current user lacks read and write access to the Unix socket that the Docker client uses to talk to the Docker daemon, usually /var/run/docker.sock. In other words, Docker is not broken; the user who typed the command simply has no right to reach the service.
You run a Docker command and get "permission denied while trying to connect to the Docker daemon socket." The same command works with sudo and fails without it. This docker permission denied error means your user cannot access the socket file that leads to the Docker service. In this guide we cover three safe ways to fix it, the security cost of each one, and the shortcuts you should never take.
First, Docker has two parts. A background service (the daemon) manages containers, while the docker command you type is only a client that sends requests to that service. By default, the two talk over a Unix socket. The Docker security documentation explains that Docker chose this socket on purpose and that you should limit access to it with traditional Unix permission checks.
So the fix is not "open up the permission somehow." The fix is a deliberate decision about who may control the service, and that decision directly affects the security of your server.
What exactly does the error message tell you?
The message usually contains a few parts: permission denied while connecting to the daemon socket, then the socket path and the API address the client tried to reach. As a result, those details matter for diagnosis.
- Path is /var/run/docker.sock: The client tries to reach the system-wide daemon that runs as root.
- Path sits under your user's runtime directory: The client tries to reach a rootless daemon, and that daemon most likely is not running.
- Message says "connection refused" or "no such file" instead: The problem is not permission at all; the service is not running.
Also note which command triggered it. If even a simple docker ps fails, the problem is general access. If only one compose project fails, check whether that project uses a different context or environment variable. Docker lets you define several contexts, and the active one may point at another daemon.
This distinction matters, because many fixes you find online loosen socket permissions when the real issue is a stopped service. Check that the service runs first, then check your user's permissions. For example, on a systemd distribution you can query the service status through the service manager.
How do you check your user and the socket owner?
Before you change anything, collect two facts: who runs the command, and who owns the socket? Those two answers point you to the right row in the diagnosis table.
- Run
idto see your user name and your groups. - Run
ls -l /var/run/docker.sockto read the socket's owner, group and permissions. On a standard package install, the group is usually docker. - If the socket's group is docker and your group list lacks docker, you have found the cause.
- A missing socket file means the problem is not permission; the service is down, or it opened its socket somewhere else.
- If the DOCKER_HOST environment variable exists, the client goes to that address instead of the default socket. Check its value.
These checks only read; they write nothing and change no setting. Therefore, you can run them safely even on a production server. In addition, when you share the output with a teammate, you show evidence instead of describing the problem.
Also, if you installed Docker through something other than your package manager, such as snap, the socket path and group may differ. In that case, check the official documentation for your install method.
Why does the docker permission denied error happen?
On Linux, the daemon that Docker packages install runs with root privileges, and root and the docker group own the socket file. If your normal user does not belong to that group, it cannot write to the socket, and you see the docker permission denied error.
The most common causes look like this:
- Nobody ever added your user to the docker group.
- You added the user to the group but did not refresh the session; group membership takes effect in a new login session.
- A CI tool, a cron job or a service account runs the command, and that user is not in the group.
- You installed rootless Docker, but the client still tries the system-wide socket.
- You ran Docker with sudo earlier, so root now owns the Docker config folder in your home directory.
- A container tries to reach the host socket, and the container user lacks permission.
Each cause therefore needs a different fix. Find your case with the diagnosis table below.
Which fix fits which situation? A diagnosis and decision table
The table below links each symptom to its cause and a safe fix. The last column shows the security cost of the path you pick.
| Symptom | Likely cause | Safe fix | Security impact |
|---|---|---|---|
| Works with sudo, fails without | User not in the docker group | docker group or rootless Docker | Group membership grants root-level privileges |
| Added to the group, still fails | Session not refreshed | Log out and back in | No extra impact |
| Permission warning for the config file | Root owns the .docker folder in your home | Restore folder ownership | No extra impact |
| Fails in CI or cron | The job user lacks permission | Separate build machine or rootless setup | Giving a CI user root equivalence is risky |
| Socket access from inside a container | Socket mounted, permission missing | Question whether the socket is truly needed | A container with the socket can control the host |
| Fails after a rootless install | Client points at the wrong socket | Set DOCKER_HOST to the right socket | Rootless is the lowest-risk path |
However, one row is missing on purpose: opening the socket to everyone. That does not solve the problem; it turns the problem into handing root to every user on the system.
Why is chmod 777 on docker.sock never the fix?
The shortcut people suggest most often on forums is to make the socket readable and writable by everyone. The error disappears instantly; however, the price is very high.
In fact, the Docker documentation states plainly that whoever controls the Docker daemon effectively controls the host. According to the docs, Docker lets you share host directories with containers without limiting access rights; for example, a container that mounts the host's root directory can alter that filesystem without restriction. Anyone who can write to the socket can start such a container.
- Every user, every web application process and every compromised account on the server ends up one command away from root.
- When Docker restarts, it recreates the socket, so the permission change often vanishes and the error returns. So it is both unsafe and temporary.
- Security audits usually flag this setting as a critical finding.
In short, never use this route in any environment. By the same logic, changing the socket owner to a normal user carries the same risk. For a basic hardening baseline, the steps in our Linux swap file and SSH hardening guide are a good start.
How do you add your user to the docker group?
The official Docker Linux post-installation steps give this order for using Docker without sudo:
- Create the docker group:
sudo groupadd docker(the package install may have created it already). - Add your user to it:
sudo usermod -aG docker $USER - Log out and back in so the membership takes effect, or run
newgrp docker. - Test the setup:
docker run hello-worldshould now run without sudo.
Also, the -a option in that command appends the new group without removing your existing groups. If you forget it, the user may drop out of other groups, so type the command exactly.
However, read the security section below before you take this step. The documentation itself places an important warning right next to it.
Why is the docker group equivalent to root?
The Docker documentation is direct about it: the docker group grants root-level privileges to the user. The reason is that the group grants access to the socket, and anyone with socket access can ask the root daemon to start any container they like.
Therefore, treat docker group membership like sudo rights. Do not add anyone to the group before you answer these questions:
- Would I give this person or account sudo on this machine?
- Does an internet-facing application use this account?
- Is this account sharing its password or SSH key with other machines?
- Does the machine hold other people's data or customer data?
On your personal development machine, a single-user virtual machine or a server where you already have sudo, group membership is often an acceptable trade-off. On a shared server, a CI runner or the account of a web application, it is a serious risk. Also remember that anyone with SSH access to that account inherits this power; we cover SSH key problems in our guide to the SSH permission denied (publickey) error.
What is rootless Docker, and when should you choose it?
Put simply, rootless mode runs the Docker daemon and its containers as a non-root user. The Docker rootless documentation says this reduces the risk from potential vulnerabilities in the daemon and the container runtime. According to the docs, unlike user namespace remapping, where the daemon still runs as root, rootless mode runs both the daemon and the containers without root privileges.
Consider rootless mode first in these cases:
- Several people use the server and you do not want to hand everyone root equivalence.
- Systems run code from outside, such as CI runners and build machines.
- You want a separate, isolated Docker environment per user.
- Your security policy restricts services that run as root.
On the other hand, rootless mode may not behave identically for every workload; some networking and storage features can differ. So test your own containers before production and read the troubleshooting page that the documentation links to.
How do you install rootless Docker step by step?
According to the Docker documentation, the installation needs no root privileges once you meet the prerequisites. The main steps look like this:
- Install prerequisites: You need newuidmap and newgidmap; most distributions ship them in the uidmap package.
- Check the subordinate ID range: Your user needs at least 65,536 subordinate UIDs and GIDs in /etc/subuid and /etc/subgid.
- Stop the system-wide daemon: The docs advise disabling it if it runs; otherwise the setup script asks for a force option, and you keep using rootful Docker until you shut it down.
- Run the setup script: With a package-based install, run
dockerd-rootless-setuptool.sh installas your normal user. If the script is missing, install the docker-ce-rootless-extras package. - Point the client: Some applications need DOCKER_HOST set to the socket in your user runtime directory. The documented example looks like /run/user/1000/docker.sock, where the number is your user ID.
- Verify: The output of
docker infoshould show the rootless context and security option. - Start at boot: The docs show enabling lingering for the user so the rootless daemon starts when the system boots.
These steps follow the order in the official documentation. Open the current version of that page before you install, because details can change between releases.
I added myself to the group, so why does docker permission denied persist?
This is the most common situation, and it is almost always about the session because of how login works. Linux reads group membership when you log in, so your current terminal keeps using the old group list.
Check in this order:
- Run
groups. If docker is missing from the list, your current session does not see the new membership. - Log out completely and log back in. If you use SSH, close the connection and open a new one.
- On a desktop, the graphical session can hold the old group list; if logging out does not help, try restarting the machine.
- Make sure you typed the right user name; $USER shows the expected user when you run the command yourself, but scripts may differ.
- Confirm that the service runs and the socket exists.
Also, an editor such as VS Code can keep an old session alive through its remote connection process. In that case, restart the editor's server-side process too. If you develop inside containers, our article on the VS Code Dev Container explains that connection model.
How do you fix the permission warning for the .docker folder?
If you ran Docker commands with sudo before, root may now own the .docker folder in your home directory. Then, even after you join the group, you see a similar permission warning about the config file.
In this case, the official documentation offers two fixes:
- Delete the folder: Docker recreates it automatically, but you lose custom settings such as registry entries.
- Restore ownership and permissions: You give the folder back to your user and add group read, write and execute permission. The docs show sudo with chown and chmod, aimed only at this folder.
Note that this fix applies only to the config folder in your own home directory. Applying the same logic to the socket file or to system directories creates the security risk we described earlier.
How do you fix the error for CI, cron and service accounts?
In automation, then, the docker permission denied error comes from the fact that the job does not run as you. Jenkins, GitLab Runner and cron jobs each run under their own account.
The easy way out is to add that account to the docker group. However, that means giving every piece of incoming build code root equivalence on the host. Anyone who can change a repository could take over the machine through a build script.
- Run builds on separate, single-purpose machines you can throw away.
- Use rootless Docker for the build user where possible.
- Do not build on the production server; pull the finished image and run it.
- Keep the job account's passwords and keys separate from other systems.
That way, a bug or a malicious change in a build script only affects that temporary machine.
How safe is mounting docker.sock into a container?
For example, some tools ask you to mount the host socket into a container so they can manage other containers. Management dashboards, reverse proxies and auto-update tools are typical examples. These tools can also show a similar permission error.
However, first accept this fact: giving a container the socket gives it root equivalence on the host. If an attacker compromises the app in that container, they can control the host. Therefore:
- Question whether the tool truly needs the socket.
- Do not expose the tool's admin interface to the internet; use strong authentication.
- Use only trusted images that receive regular updates.
- Fix the permission error the way the tool's documentation describes, not by granting the container user broad socket rights.
We cover setup and risk for tools like these in our guides to installing Portainer and what Coolify is.
Does exposing the Docker API over the network fix the error?
Next, some sources suggest exposing the Docker API on a TCP port instead of the socket. That can make the permission error go away, but it moves the risk from your local machine to the whole network.
The Docker documentation uses firm language here. If you expose the API over HTTP, securing the endpoints with HTTPS and certificates is mandatory. In addition, according to the docs, exposing the daemon API over HTTP without TLS is not permitted, and the daemon fails to start in that configuration. The docs also mention SSH-based access as an alternative for remote use.
So if you need to manage Docker on a remote machine, connect over SSH instead of opening a port. That method reuses your existing SSH authentication and opens no new attack surface. To limit brute-force attempts against the server, apply the steps in our Fail2ban setup guide.
How does the error show up in Docker Compose and scripts?
Docker Compose uses the same client logic; in other words, it talks to the daemon over the same socket. That is why compose commands show the same docker permission denied message, and the fix is the same.
Scripts, however, are a bit trickier. For example, when you run a script with sudo, the docker commands inside it run as root and the error stays hidden. When cron or CI runs the same script, a different user takes over and the error appears. Also, a script that runs with sudo can leave root-owned files in a home directory, which leads to the config folder warning we covered above.
- Log the running user at the top of the script.
- Instead of sudo inside the script, choose a deliberate permission model for the user who runs it.
- Document whether the script depends on environment variables such as DOCKER_HOST.
That way, you can separate "works by hand, fails in automation" problems within minutes.
How do you audit Docker access over time?
However, fixing the error does not end the security work. Because the docker group equals root, you need to review its members regularly.
- List group members at set intervals and remove accounts that no longer need access.
- Close the accounts and SSH keys of people who leave the team on the same day.
- Tie group membership to the same approval process as sudo rights.
- Document which directories your containers mount and which ones can reach the socket.
This small habit means you still know, a year later, who holds root-equivalent access and why. On servers that several people share, the access list is the first document anyone checks during a security incident.
How do you compare the three safe paths?
In practice, the path you pick depends on who owns the machine and what it runs.
| Path | Ease | Security | Best fit |
|---|---|---|---|
| Run commands with sudo | Works right away | Each command asks for privilege on purpose and leaves a log entry | An admin who uses Docker now and then |
| Join the docker group | Very easy | User becomes root equivalent | Personal dev machine, single-user server |
| Rootless Docker | Needs setup | Daemon and containers run without root | Shared server, CI, multi-user environments |
Remember that sudo also grants full power; the difference is that you request it deliberately for each command. If containers are new to you, start with our what is Docker guide to settle the basics.
A realistic example: first setup on a new server
Example scenario: a small software team installs Docker on a new Ubuntu server and wants to run a database and a web app in containers. One developer connects over SSH, types docker ps and gets permission denied.
The first suggestion is the socket permission command from a forum. The team lead stops it, because three people have accounts on the server and the web app will run under a separate user. Instead, the team decides to add the two admins to the docker group, because they already have sudo. The third person only updates application code, so they stay out of the group; an admin-approved script handles deploys.
The error persists for the two admins until they refresh their sessions; after they log out and back in, it disappears. A few months later, the team adds a CI runner. This time, instead of adding the build user to the group, they run builds on a separate virtual machine with rootless Docker, and the production server only pulls finished images. That closes the path for anyone with repository access to gain root-equivalent access to production. This is not a client case; it is an example scenario built from components we see often. We walk through the first configuration of a new server in our Ubuntu Server initial setup guide.
What are the most common mistakes with the docker permission denied error?
Specifically, we see the same mistakes across different teams:
- Opening the socket to everyone: Unsafe, and it vanishes when the daemon restarts.
- Calling it broken without refreshing the session: Group membership applies only to new sessions.
- Adding the web app's user to the group: A bug in the app turns straight into root access.
- Forgetting -a in usermod: The user may drop out of other groups.
- Tweaking permissions while the daemon is down: Confirm that the service runs first.
- Exposing the API without TLS: According to the docs, Docker does not even permit that configuration.
- Using the old socket after a rootless install: Without DOCKER_HOST, the client goes to the wrong daemon.
You can use this list as a short checklist for new server setups and whenever someone new joins the team.
Is it different on Windows, macOS and WSL2?
Yes, because the architecture differs. On Windows and macOS machines with Docker Desktop, the daemon runs inside a virtual environment that the app manages, and Docker Desktop sets up the socket connection. In those environments, a similar connection error usually means Docker Desktop is not running or is still starting.
If you use a Linux distribution inside WSL2, there are two possibilities. If you installed Docker directly inside the WSL distribution, the Linux steps in this guide apply. With Docker Desktop's WSL integration, check in Docker Desktop's settings that integration is on for that distribution.
In both cases, first find out where the daemon actually runs, because what looks like a permission problem may be a connection problem. We cover setting up and managing WSL2 in what is WSL2.
A short checklist for the docker permission denied error
When the error comes back, work in this order:
- Does the message say "permission denied," or does it say the connection was refused?
- Does the Docker service run, and does the socket file exist?
- Which user runs the command? Does the groups output include docker?
- If you just joined the group, did you refresh the session?
- Does your user own the .docker folder in your home directory?
- With a rootless setup, does DOCKER_HOST point at the right socket?
- On this machine, is it acceptable to give this user root equivalence?
The last question is a security question, not a technical one, and its answer differs by environment. If you run databases in containers, our guide to PostgreSQL and MySQL in Docker helps with your next step.



