Running Docker Commands Without SUDO

Executing docker commands without sudo access

Famidha Thurab
Make Android
2 min readJan 8, 2024

--

Photo by Taneli Lahtinen on Unsplash

Docker daemon runs with root privileges so when the container is launched, it bestows the root permission from the docker. To run docker as a non-root user, you must add the user to the docker group.

This article will explore how to run docker without sudo!!

By default, the docker daemon connects with the Unix Socket which is owned by the root user and not any other port like tcp. When a non-root user becomes a member of the docker group can access the Unix socket making the docker run as a non-root user.

When docker is installed, we will have to access docker as the root user as the docker daemon by default runs as a root user, now by adding the non-root user to the docker group, we will be changing the docker daemon to run on non-root user.

Install docker referring to the below link

To run the docker command as below, we are using sudo access as the docker daemon is running as a root user.

$ sudo docker run -it ubuntu bash

To run docker with/as a non-root user, will proceed with the below steps

1. Check if the docker group is available or not

$ cat /etc/group | grep docker

2. If not available create a docker group

$ sudo groupadd docker

4. Add the user to the docker group

$ sudo usermod -aG docker ${USER}

5. Activate changes with the below command

$ newgrp docker

6. Now, try to run some docker commands to verify docker runs with non-root

$ docker run hello-world

Below is the output of the above command:

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
....

7. Change the permissions of the ~/.docker

$ chown "$USER":"domain users" $HOME/.docker -R
$ chmod g+wx "$HOME/.docker" -R

Take away!

Execute docker commands without sudo access which enhances the security by adding an extra protective layer.

Thanks for Reading!!

If you find the article helpful please hit the clap and follow me on medium.

--

--

Famidha Thurab
Make Android

DevOps Engineer| Editor Make Android | Writes on AIOps | Android IVI | AOSP | Docker | Kubernetes | AWS | Jenkins | Python | Shell | Git