S O F T W A R E S P I C E

Loading

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

That would be the first-ever error you may encounter while running Docker on the Ubuntu VM. The error states “Got permission denied while trying to connect to the Docker daemon socket“.  While you can use sudo and give the sudo password, but it’s annoying to put the password again and again. You will not get this error if you are working on Ubuntu as a root user. In short, we want to manager Docker as a non-root user and we’ll find a solution for that.

Previously, we installed the Docker Engine on Ubuntu 18.04 and on Ubuntu 20.04. The installation process is almost the same. You can do it on the Ubuntu release of your choice.

Let’s fix the permission denied error.

1. Why this Permission Denied Error?

The answer is simple— we are trying to run Docker as a non-root user. I want to run the Docker Engine with my user “faisal” and unfortunately, this user does not have the needed permissions. Therefore, every time running Docker as a non-root user, I have to use sudo and put the sudo password for the user “faisal”.

If you want to replicate this error, simply run any docker command without sudo.

docker images

This command lists the images available locally on your virtual machine.

Can you see the permission denied error below?

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

Now if I run it with sudo, it will ask for the password. The command will show the output afterward. You can see the hello-world Docker image.

put the sudo password for the user

2. Create a Docker Group

In the first step, we need to create a Docker group with groupadd Linux command.

sudo groupadd docker

If it says, the group ‘docker’ already exists means the group has already been added. We need to move to the next step.

3. Add your user to the Docker Group

Now we’ll add our user in to the Docker group. Execute the following command.

sudo usermod -aG docker $USER

Let’s understand the command a bit.

  • The usermod command is used to change the properties of a user in Linux.
  • -a technically means to append the user.
  • -G is the group, we are telling him the “docker” group.
  • $USER is a variable that will get the current username from which you are logged in to the system.

In layman’s terms, -aG means to add the requested user to the Docker group.

Log out from your system and log back in. Now you should be able to run the docker commands without sudo.

Let’s verify that we are able to run docker commands without sudo. 

Search a node image with the docker search command.

docker search node

This command searches and shows us the official and public images for Node.js. It is a JavaScript-based platform for server-side and networking applications.

searching a node image in docker

Again, I am running the following command:

docker run httpd

It pulls the httpd image from the Docker Hub and runs the container for the Apache webserver.

running httpd docker container

Everything looks good and we are able to run the docker commands without sudo. If you are still facing the “Got permission denied while trying to connect to the Docker daemon socket” error, feel free to let me know in the comments.

Happy Docking!!!

Leave a Comment

Recent Posts

how to stop a docker container
How to Run a Docker Container
How to Push a Docker Image to a Private Repository
How to push a docker image
How to Tag a Docker Image
Software Spice
Share via
Copy link
Powered by Social Snap