Fix Docker Permission Denied Error

While I have tried this on ubuntu-server, the steps should work for all Linux versions

I installed Docker on Ubuntu. It was super easy. But when I tried to run a docker command, it threw this error at me:


Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied        

It’s not that I am trying to run something special. It happens for basic docker commands like docker ps as well.

The error is basically for the user not having the required permission to access the docker.sock file while running docker. The fix is pretty simple.

Fix 1: Run all the docker commands with sudo

If you have sudo access on your system, you may run each docker command with sudo and you won’t see this ‘Got permission denied while trying to connect to the Docker daemon socket’ anymore.

$ sudo docker ps -
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
13dc0f4226dc        ubuntu              "bash"              17 hours ago        Exited (0) 16 hours ago                       container-2
2d9a8c190e6c        ubuntu              "/bin/bash"         17 hours ago        Created                                       container-1a        

But running each and every docker command with sudo is super inconvenient. You miss adding sudo to the beginning and you’ll get a ‘permission denied’ error again.

Fix 2: Running docker commands without sudo

To run the docker commands without sudo, you can add your user account (or the account you are trying to fix this problem for) to the docker group.

First, create the docker group using groupadd command. The group may already exist but running the group creation command won’t hurt.

$ sudo groupadd docker        

Now that you have the docker group, add your user to this group with the usermod command. I am assuming that you are trying to do it for your own user account and in that case, you can use the $USER variable.

$ sudo usermod -aG docker $USER        

Verify that your user has been added to docker group by listing the users of the group. You probably have to log out and log in back again.

$ group
maino adm cdrom sudo dip plugdev dockers        

If you check your groups and docker groups are not listed even after logging out, you may have to restart Ubuntu. To avoid that, you can use the newgrp command like this:


$ newgrp docker        

Now if you try running the docker commands without sudo, it should work just fine.

Troubleshooting

In some cases, you may need to add additional permissions to some files especially if you have run the docker commands with sudo in the past.

You may try changing the group ownership of the /var/run/docker.sock file.

$ sudo chown root:docker /var/run/docker.sock        

You may also try changing the group ownership of the ~/.docker directory.

$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
$ sudo chmod g+rwx "$HOME/.docker" -R        
Tam Nguyen

Networking Software Engineer

1y

Thank you for this it save my day : newgrp docker

Like
Reply

To view or add a comment, sign in

More articles by Om Prakash Singh

Insights from the community

Others also viewed

Explore topics