Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Docker is an amazing containerization tool, allowing us developers to bundle applications and their dependencies into a single package that we can then run consistently across various environments, without worrying about compatibility. It is amazing because of the flexilibilyt that it provides. We can start, stop, and restart a container, have multiple containers running in parallel, or have a single container running in the background while you focus on your work at hand. However, there is one impediment to this, which is if you happen to close your terminals as soon as you are done running your last command, (which I’m guilty of doing a lot), you’ll have to provide your password to invoke the ‘docker’ command again. This is kind of an annoyance, while it’s highly recommended that you only allow docker to run with ‘sudo’ in server environments but its also an annoyance during the development phase when you frequently interact with Docker, let’s be honest. Fortunately, there’s a straightforward way to set up Docker so that you don’t need to enter sudo
each time.
In this blog post, I’ll walk you through the steps to make Docker rootless, enabling you to use Docker commands without needing sudo
on your Ubuntu system.
Docker requires elevated privileges to access resources like the network, storage, and devices. And because these are sensitive system resources so prepending sudo
to your Docker commands ensures these resources are accessed only by authorized users. However, we can configure Docker to be run by non-root users by adding our user to the docker
group. To learn more about how to add a user to a group or an application to a group you may check this blog.
Install Docker (If Not Already Installed): If you haven’t installed Docker yet, you can visit the official docker website here at www.docker.com
Create a Docker group if not already created: Use the following command to check if a docker group is already present on the system. If you have recently installed docker then this should be already created.
mohit@hyperfoss:~$ grep docker /etc/group
If the docker
group does not exist, then we can create it by running the following command:
mohit@hyperfoss:~$ sudo groupadd docker
Add your user to the docker
group by running the following command:
mohit@hyperfoss:~$ sudo usermod -aG docker $USER
Note: We need to replace $USER
with the correct username if we are adding a different user than the one currently logged in. We can use echo $USER
to print the current user.
Log out of your Ubuntu session and log back in. This step is necessary for the group changes to take effect.
After logging back in, open a terminal and check if your user is added to the docker
group by running the following command:
mohit@hyperfoss:~$ groups
And that’s all folks :-D, that is all that is needed to run docker
commands without using sudo
. Test it by running:
mohit@hyperfoss:~$ docker run hello-world
If the Docker installation is working properly, you should see the “Hello from Docker!” message.
As explained above it should be noted that giving regular users access to Docker can have security problems. If you share the system with other users then ensure you trust those users before adding them to docker
group.
So this was a small guide about how to set up Docker to run without sudo
on our Ubuntu machine. I do it to streamline my development workflow, as I have to worry about fewer password prompts. But keep in mind that we need to be a little more cautious about which users we add to the docker
group.
Finally please feel free to share what you feel about this setup and what are your thoughts or you may also ask any questions in the comments below. Happy containerizing!