Got permission denied while trying to connect to the Docker daemon socket
If you are just getting started with Docker there is a good chance you have come across this error message. “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.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied” It basically means that you don’t have the required permissions to connect to the docker daemon. You will need the correct permissions to run any docker commands. It does confirm that the Docker daemon is running because if it wasn’t it wouldn’t be able to deny you access. It would just say that you can’t connect.
This issue will usually pop up as soon as you try to run any Docker related commands such as docker ps.
There are a few different ways you could go about fixing this.
- Run each command with sudo
- Run all of your docker commands as root
- Grant permissions to a non-root user
- Run the Docker daemon in Rootless mode (as a non-root user)
Option 1: Sudo
You can run Docker commands with sudo like this:
sudo docker ps
Option 2: Become Root
If you have the rights to run “sudo su -“ you can run Docker commands as root like this:
sudo su - # sudo to root docker ps # directly run command
If you have the root password, you can run Docker commands as root like this:
su - # change to root user docker ps # directly run command
Option 3: How To Grant Permissions To A Non-root User
Run these two commands to create a docker group and add your current user to it.
sudo groupadd docker sudo usermod -aG docker $USER
- logout and back in
- restart the Docker daemon
If you had already run some docker commands as root you will likely see additional error after adding the docker group. You can fix these by adjusting the permissions on the “.docker” directory recursively.
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R sudo chmod g+rwx "$HOME/.docker" -R
Verify that it works:
docker run hello-world