Docker Add User to Docker Group

In this section we will see how you can add a user to the Docker Group. Here we will use the docker command to add an user to the Docker Group.

Docker Add User to Docker Group

Docker Non-root user access - Docker Add User to Docker Group

If you want to enable a non-root user to access the Docker service for running images on the Docker service then you have to add the user to the Docker Group. For example you have installed Docker using the sudo command and you are not able to access docker with your user then you have to follow the steps given here.

About Docker

Docker is a very popular service for running applications in containers on the host of the operating systems. Docker is an open-source software which can be used to develop, manage and run applications on the host machine without running a Virtual Image on the host machine. Docker uses the host operating system OS to run the container applications. Docker is available for Windows, Linux and Mac operating systems. But mostly used in the Linux and Mac operating system. Learn more at What is Docker?

After this you will be able to use docker without sudo. Is it possible to use docker without sudo? Yes, you will be able to use Docker with some other user (non-root) user. This is possible by adding a user to the Docker Group.

If a non-root runs any docker command then following error is displayed:

docker: Got permission denied while trying to connect to the 
Docker daemon socket at unix:///var/run/docker.sock: 

How to Add User to Docker Group?

Here are the steps to add a user into the Docker Group.

Step 1: Check if Docker group exists? If not then create

During the installation of Docker the docker group is created automatically by the docker installer. If it is not created then you have to create the docker group.

Run the following command to see if docker group exists:

grep docker /etc/group

If docker group is already added then it will show following display:

Docker group

If there is not docker group then run the following command to add docker group to your system:

sudo groupadd docker

Above command will add docker group to the system.

Step 2: Restart docker service

Now the next step after adding the docker group by running the following command:

sudo systemctl restart docker

Above command will restart the docker service on your Ubuntu desktop.

Step 3: Add user to the docker group

Now you should add your user to the docker group. To add current user to the docker group run following command:

sudo usermod -aG docker $USER

If you have to add other user to the docker group then replace $USER with the given user name.

Step 4: Verify

Now you can verify if the user is added to the docker group by running the following command:

grep docker /etc/group

Above command will list download the names which is in the docker group. Now you can logoff and login again after this you will be able to use the docker service with your username. Once you login again try running following command:

docker images

Above command should not give permission denied issue and it should list down the docker images on your system. If you have installed docker first time then you won't see any image list.

Step 5: Running docker hello-world image

The final step is to run the docker "hello-world" image, which simply downloads the sample docker "hello-world" image and runs on your docker installation. Open terminal on your Ubuntu desktop and run the following command:

docker run hello-world

Here is the output of the above command:

Hello from Docker! 
This message shows that your installation appears to be working correctly. 

To generate this message, Docker took the following steps: 
1. The Docker client contacted the Docker daemon. 
2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 
(amd64) 
3. The Docker daemon created a new container from that image which runs the 
executable that produces the output you are currently reading. 
4. The Docker daemon streamed that output to the Docker client, which sent it 
to your terminal. 

To try something more ambitious, you can run an Ubuntu container with: 
$ docker run -it ubuntu bash 

Share images, automate workflows, and more with a free Docker ID: 
https://hub.docker.com/ 

For more examples and ideas, visit: 
https://docs.docker.com/get-started/ 

Here is the screenshot of the process:

Docker hello world example

After running the docker images command you will be able to see docker images downloaded to your system. In my case there are two images:

  • gcr.io/k8s-minikube/kicbase
  • hello-world

So, we learned how to add an user to the docker group and then run a docker image after getting the privileges.

How do I add a user to a docker group?

You have to login to your Ubuntu/Linux system and the a user to a docker group by running the command:

sudo usermod -aG docker $USER

You can replace $USER with the name of user you want to add. For example if you have to add user john to the docker group then you can run following command:

sudo usermod -aG docker john

Should you add user to docker group? 0

If you want to run docker as not root user then you have to add user to the docker group. I have already explained the command for adding the user to the docker group.

What is the best reason for adding the Linux user to a docker group?

The main reason for adding the Linux user to the docker group is to be able to access docker service for that user. If a user is not in the docker group that user won't be able access docker service. In such cases you have to switch to the root user for running docker, which is not a good practice. If many users are accessing the docker service then it's much easier to add the users user to the docker group, which gives docker access to these users. 1

How to add users to Docker container?

As explained above, users can add users to the docker group with the help of usermod command. After running the command your user will be added to the docker group. After the next login user will be able to use the docker service from their login.

How to add user and a group in Docker Container running? 2

You can add a user to the Linux system with the help of useradd command. To add a user to the Linux you can run following command:

sudo useradd john

Above command will add user john to the Linux operating system. After adding this you can run following command to add it to the docker group:

sudo usermod -aG docker john

Above command will add user john to the docker group, after next login user john will be able to use docker service. 3

run docker-compose without sudo

For running docker-compose without sudo, you have to add your user to the docker group. I have already explained the command above in the tutorial.

add user to docker group linux 4

As explained above here is the command to add users to the docker group in Linux and Ubuntu operating system.

sudo usermod -aG docker john

cannot connect to the docker daemon at unix:///var/run/docker.sock. is the docker daemon running?

This error comes when your user is not added to the docker gropu. So you have to first add the user to the docker group and then above error will go away. 5

docker without sudo

If you want to run docker without sudo then contact your admin and ask your user to the docker group. If you have root access to your system/server then you can use the command explained in this tutorial for adding a given user to the docker group.

How to add user to docker group in Ubuntu? 6

It is very easy to add a user to the docker group in the Ubuntu and all other Linux operating system. As explained in this tutorial you can use following command:

sudo usermod -aG docker $USER

Above command is easy and you can use for adding the user to the docker group.

docker permission denied 7

If you are getting this error then it means that you don't have permission to access the docker daemon. You have to use the following command to add user to the group:

sudo usermod -aG docker <your_user_name>

You should replace the <your_user_name> with the actual user name. In this tutorial we have learned the steps to adding user to docker group and then very by running hello-world docker image on the Ubuntu operating system.

Checkout our Docker tutorials: 8