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.
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.
This command lists the images available locally on your virtual machine.
Can you see the permission denied error below?
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.
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.
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.
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.