6 Ways to fix - Got permission denied while trying to connect to the Docker daemon socket?

I faced this issue couple of times and both times I had the same error but with different error messages text. The first error message which I got was -

  1. docker Got permission denied while trying to connect to the Docker daemon socket at unix
  2. the second time I got the error message - Can not connect to docker daemon. Is docker -d running on this host?.

But in the end, both are the same issues, in this article I have written down the different approaches which I have taken to troubleshoot this issue. It is a possibility that some of the approaches described in the article might not work for you. So I would highly encourage you to read all the approaches and see which one works for you.

Here is a little brief about the docker.sock file and why we face this issue?

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 the 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.

Table of Content

  1. Approach 1 - Run docker command as sudo
  2. Approach 2 - Add your user to the Docker group (recommended)
  3. Approach 3 - Restart your docker engine service
  4. Approach 4 - Check the permission of docker.sock file
  5. Approach 5 - Check the docker build of each docker container
  6. Approach 6 - Mac OS X docker permission denied issue after every reboot/restart


1. Run docker command as sudo

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

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

In my case, I was getting an 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 a user to the Docker group would be the most suitable approach for fixing this issue.

1$ sudo usermod -aG docker $USER

After running the above command either you can log out or log back-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 development practices.

Ideally, you should have segregation 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.

2.1 Fixing WARNING: Error loading config file: /home/user/.docker/config.json - stat /home/user/.docker/config.json: permission denied

After adding the current user to the docker group, if you still face the following issue then the current user doest not have permission to directory .docker

1WARNING: Error loading config file: /home/rwagh/.docker/config.json -
2stat /home/rwagh/.docker/config.json: permission denied 

How to Fix?

Change the owner of .docker directory to current user -

1sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
2
3sudo chmod g+rwx "$HOME/.docker" -R



3. Restart your docker engine service

The third approach would be to check your docker engine service using the following command

1sudo service docker status

Once you run the above command and you see the Active: inactive(dead) which means your docker engine services are not running.

permission denied while trying to connect to the Docker daemon socket?

To fix this issue you must start your docker engine services -

1sudo service docker start 

or you can simply restart the docker engine services -

1sudo service docker restart


4. Check the permission of docker.sock file

There is a high possibility that you do not have the correct permission set on /var/run/docker.sock file and that might be the reason you are facing the issue - docker: Got permission denied while trying to connect to the Docker daemon socket at

Use the following command for assigning the correct permission -

1sudo ls -la /var/run/docker.sock

What is /var/run/docker.sock ?

It is a Unix Domain Socket or sometimes it is called 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 a guide published by docker which can also be referred - https://docs.docker.com/engine/install/linux-postinstall/


5. Check the docker build of each docker container

While building each docker container carefully look at the docker build logs of each container and spot if there is any issue related to file permission or if by any chance your docker container is not able to access the file which is needed for building the docker container.

If you see any error message like permission denied then check the file permission on which the error has happened and try to change the permission using the following command so that the docker service can access the file.

1 sudo chown [username]:docker /your/file/location

The other way around would be to add the file to .dockerignore, if you do not need that file and do not want it to be included in the docker build.

Here is the docker build logs of the successful docker build command

1$ docker build -t gcr.io/jhooq-sprinboot-k8s-demo/jhooq-springboot:v1 .
 1Sending build context to Docker daemon  28.31MBB
 2Step 1/4 : FROM openjdk:8-jdk-alpine
 3 ---> a3562aa0b991
 4Step 2/4 : ARG JAR_FILE=build/libs/*.jar
 5 ---> Using cache
 6 ---> ab3e5f96d4dc
 7Step 3/4 : COPY ${JAR_FILE} app.jar
 8 ---> Using cache
 9 ---> ede5735c296c
10Step 4/4 : ENTRYPOINT ["java","-jar","/app.jar"]
11 ---> Using cache
12 ---> adaf5b0a60a2
13Successfully built adaf5b0a60a2
14Successfully tagged gcr.io/jhooq-springboot-k8s-demo/jhooq-springboot:v1


6. Mac OS X docker permission denied issue after every reboot/restart

The sixth approach is special and is only applicable for Mac OS. This was very trivial because I always had an error message docker Got permission denied while trying to connect to the Docker daemon socket whenever I rebooted/restarted my Mac OS.

Here is the solution which worked for me -

  1. First and far most I uninstalled the docker and docker engine from my Mac OS
  2. I did the fresh re-installation of Docker
  3. After the installation start the docker-machine -
1docker-machine start default
  1. Generate the TLS connection certificates -
1docker-machine regenerate-certs default
  1. Set the environment variable for the docker machine
1eval "$(docker-machine env default)"
  1. You can achieve all of the above in one single command -
1docker-machine start default; docker-machine regenerate-certs default; eval "$(docker-machine env default)"

In case if you still face the Error getting SSH command: Something went wrong running an SSH command! then simply re-run the above commands once again.

If you are interested in learning more about docker please refer to - Docker


Learn more On Kubernetes -

  1. Setup kubernetes on Ubuntu
  2. Setup Kubernetes on CentOs
  3. Setup HA Kubernetes Cluster with Kubespray
  4. Setup HA Kubernetes with Minikube
  5. Setup Kubernetes Dashboard for local kubernetes cluster
  6. Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
  13. How to setup kubernetes jenkins pipeline on AWS?
  14. Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
  15. How to fix kubernetes pods getting recreated?
  16. How to delete all kubernetes PODS?
  17. How to use Kubernetes secrets?
  18. Share kubernetes secrets between namespaces?
  19. How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
  20. Delete Kubernetes POD stuck in terminating state?

Posts in this Series