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

Share on:

As the problem says permission denied which means you do not have permission on docker.sock file.

And due to the missing permission you see following error message -

1$ docker-compose up
1docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
2See docker run --help.

There are two ways to fix the problem -

1. Run docker command as sudo

The easiest way I can suggest to you is run the docker command with sudo.

Because running the command with sudo allows you to run any docker command as adminstrator. And since the administrator has all the rights, so you will not face this issue again.

In my case I was getting error with the following command, so I added sudo before running the command.

1$ sudo docker-compose up

But it can be any command in your case, so I would highly recommend you to add sudo before running any command.

In my opinion adding user to the Docker group would be the mostly suitable approach for fixing this issue.

1$ sudo usermod -aG docker $USER

After running the above command either you can logout or logback-in.

Or you can restart your machine so that group policies are applied correctly.

Why do I recommend this approach? - Because in the production environment it is not advisable to run any command with sudo privileges. If you have are running any command with sudo privileges then I would doubt your devlopment practices.

Ideally you should have seggregation of rules and duties in production and due which you should add the user to the Docker group so that you do not face this issue again.

What is /var/run/docker.sock ?

It is an Unix Domain Socket or sometimes it is called as IPC(Inter Process communication), which is used for exchanging the data between the processes.

So in the nutshell, any user who belongs to the Docker group should have access to /var/run/docker.sock for sharing the data between the processes.

Here are guide published by docker which can also be referred - https://docs.docker.com/engine/install/linux-postinstall/

If you are interested in learning more about docker please refer to - https://jhooq.com/category/docker/