How to Add User to Docker Group

How to Add User to Docker Group

By default, Docker Daemon is a root user, and it owns the Unix socket. So, in Linux, unprivileged users can not use Docker directly without sudo user rights and get the “docker: Permission Denied” error. To execute the “docker” command directly without sudo user rights, the user first needs to add the user to the Docker group:

 docker: Permission Denied error

How to Add User to Docker Group?

Docker holds the root privileges, therefore any user that does not have root rights is unable to run the “docker” commands directly. They have to use “sudo” in front of every Docker command. To avoid using “sudo” to run Docker, the user first needs to check if Docker is added to the root group. Then, the user needs to add the unprivileged user to the Docker group. Follow the below demonstration to add the user to the Docker group.

Step 1: Check Docker Permission

Launch the Linux terminal through the “CTRL+ALT+T” shortcut key. Then, use the below command to check if Docker exists in the root group or not. During installation, Docker is automatically added to the root group. But if in case if Docker is not added to the root group, the user needs to add it manually:

grep docker /etc/group

Here, no output shows that Docker is not automatically added to the root group:

grep docker /etc/group

Step 2: Add Docker to the Root User Group

To add the Docker to the root user group, execute the “groupadd docker” command with “sudo” rights:

sudo groupadd docker
sudo groupadd docker

Step 3: View Docker Permissions

Now, again check if Docker is added to the root group and get root permissions:

grep docker /etc/group

Here, now docker is added as a root user, but the current user “hands-on” is still not added to the Docker group:

grep docker /etc/group

Step 4: Add User to Docker Group

To add the non-root user to the Docker group to use the “docker” command without sudo, utilize the “usermod -aG docker <user-name>/$USER” command with “sudo” rights:

sudo usermod -aG docker $USER
sudo usermod -aG docker $USER

Step 5: Verification

After adding the user to the Docker group, again check Docker privileges:

grep docker /etc/group

The output should be “docker:x:<any reference-no>:<user-name>” as shown below:

grep docker /etc/group

Step 6: Make Changes Effective

Now, evaluate the Linux group membership. For this purpose, either close the terminal and relaunch it or the user may need to run the “newgrp docker” command:

newgrp docker
newgrp docker

This operation may require rebooting the system. For this purpose, simply run the “reboot” command:

reboot

Step 7: Run Docker Command

After restarting the system, directly execute the “docker” command without the “sudo” keyword and check if the command is executed or not:

docker run hello-world

For demonstration, we have executed the “docker run” command to execute the demo container. We have effectively executed the “docker” command without the “sudo” user which means we have successfully added the non-root user to the Docker group:

docker run hello-world

Note: Sometimes, while running the “docker” command directly, the user may get a warning “error loading config file:/home/user/.docker/config.json”. This is due to the user executing the “docker” with the “sudo” keyword initially. After adding the user to Docker group, the changes may not be updated in the “~/.docker/” directory. To fix the stated warning, follow the below section.

Bonus Tip: How to Resolve “Error loading config file: /home/user/.docker/config.json –

stat /home/user/.docker/config.json: permission denied” Warning in Docker

To fix the stated warning message in Docker, the user may need to remove the “~/.docker/” directory. On system or docker restart, this directory will be automatically recreated with updated information, or the user needs to update the “~/.docker/” manually. Follow any of the below-given solutions to fix the given issue.

Solution 1: Remove “.docker” Directory

To fix the “Warning: error loading config file….” warning in Docker, remove the “~/.docker/” hidden directory using the “rm -rf” command. This will recursively remove the specified directory:

sudo rm -rf ~/.docker/
sudo rm -rf ~/.docker/

After that, either restart the system or restart the Docker Daemon using the “systemctl” command line utility:

sudo systemctl restart docker
sudo systemctl restart docker

This will fix the stated warning, but the user may also lose some customized settings and need to log in to the Docker registry using the “docker login” command:

docker login

This will ask you to type the login credentials. Type in the credentials and press Enter:

docker login

Solution 2: Change “.docker” Directory Ownership and Permissions

Another possible solution is to manually add the current or not-root user to the “.docker” directory using the below command:

sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R

Now, allocate the read and write permissions to the group owner for the specified directory “$HOME/.docker” through the given command:

sudo chmod g+rwx "$HOME/.docker" -R
sudo chmod g+rwx "$HOME/.docker" -R

We have covered the approach to add the non-root user to the Docker group and fix the warning that may occur after adding the user to Docker.

Conclusion

To add the user to the Docker group, first, check if Docker is already added to the root user group. After that, if docker is not added then add it using the “sudo groupadd docker” command. After that, add the non-root user to the Docker group to run the “docker” commands without the “sudo” keyword using the “sudo usermod -aG docker $USER/<user-name>” command. We have also fixed the warning that may occur after adding the user. This write-up has discussed the method to add the user to the Docker group.

Similar Posts