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 -
- docker Got permission denied while trying to connect to the Docker daemon socket at unix
- 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
- Approach 1 - Run docker command as sudo
- Approach 2 - Add your user to the Docker group (recommended)
- Approach 3 - Restart your docker engine service
- Approach 4 - Check the permission of docker.sock file
- Approach 5 - Check the docker build of each docker container
- 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.
2. Add your user to the Docker group (recommended)
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.
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 -
- First and far most I uninstalled the docker and docker engine from my Mac OS
- I did the fresh re-installation of Docker
- After the installation start the docker-machine -
1docker-machine start default
- Generate the TLS connection certificates -
1docker-machine regenerate-certs default
- Set the environment variable for the docker machine
1eval "$(docker-machine env default)"
- 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 -
- Setup kubernetes on Ubuntu
- Setup Kubernetes on CentOs
- Setup HA Kubernetes Cluster with Kubespray
- Setup HA Kubernetes with Minikube
- Setup Kubernetes Dashboard for local kubernetes cluster
- Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
- How to use Persistent Volume and Persistent Volume Claims in Kubernetes
- Deploy Spring Boot Microservice on local Kubernetes cluster
- Deploy Spring Boot Microservice on Cloud Platform(GCP)
- Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
- How to setup kubernetes jenkins pipeline on AWS?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- How to fix kubernetes pods getting recreated?
- How to delete all kubernetes PODS?
- How to use Kubernetes secrets?
- Share kubernetes secrets between namespaces?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Delete Kubernetes POD stuck in terminating state?
Posts in this Series
- (docker run -d) Why Does a Docker Container Stop Automatically?
- Attach and detach from Docker's process?
- How I Change Name of My Docker Repository and Rename Images?
- How to set-up Cron Jobs in Docker Containers?
- 4 Ways to copy file from localhost to docker container
- Multiple commands execution in Docker Compose?
- How to push Docker Images to AWS ECR(Elastic Container registry)?
- How to Copy Docker images from one host to another host?
- What is persistent storage in docker and how to manage it
- Docker Installation on MacOS, Linux and Windows
- Docker - ADD, Update, Export Environment variable
- How to fix-Docker docker failed to compute cache key not found
- How to fix docker driver failed programming external connectivity on endpoint webserver?
- How to fix docker error executable file not found in $PATH?
- How to expose port on live containers?
- How to expose multiple ports with Docker?
- How to restart single docker container within multiple docker container?
- How to edit file within Docker container or edit a file after I shell into a Docker container?
- How to fix Error starting docker service Unit not found?
- How to remove old, unused images of Docker?
- How to fix docker error invalid reference format error?
- How to fix requested access to the resource is denied?
- How to fix Docker error cannot delete docker container conflict unable to remove repository reference?
- How to fix docker error no space left on device?
- How to connect localhost from docker container?
- Docker COPY vs Docker ADD?
- 6 Ways to fix - Got permission denied while trying to connect to the Docker daemon socket?
- 6 Ways to fix – Docker COPY failed: stat no source files were specified