Docker

How to Use Docker Without sudo?

In Linux, sudo is a command that permits users to run commands with administrative rights. When users install Docker on Linux, the Docker daemon executes with root rights. They need to use the sudo command to run Docker commands on Linux. However, when users try to run Docker commands without sudo, they will encounter an error message because the user account is currently logged in. It is not authorized to interact with the Docker daemon.

This write-up will demonstrate the method to use Docker without sudo.

How to Utilize Docker Without sudo?

To use Docker with sudo, try out the below-mentioned steps:

Step 1: Create/Make a Docker Group

First, make a Docker group by executing the below-listed command in the Ubuntu terminal:

sudo groupadd docker

The above output shows that there is already a Docker group.

Step 2: Add New User

Then, utilize the given-provided command to add the new user to the Docker group:

sudo usermod -aG docker $USER

Here:

  • The “usermod” command modifies an account of user on the system.
  • The “-aG docker” option adds the new user to the Docker group. The “-a” flag determines that the user should be added to the group, the “-G” flag specifies the group to which user should be added.
  • The “docker” is the group name.
  • $USER” is a variable for the current user’s username:

In this way, the above command has been successfully executed.

Step 3: Verification

Finally, write out the following Docker command in the terminal without using the “sudo” command for the verification:

docker run hello-world

As users can see, we have successfully used the Docker command without the sudo command.

Conclusion

To use Docker without sudo, first, make a Docker group if it does not already occur. Next, add the new user to the Docker group via the “sudo usermod -aG docker $USER” command. Lastly, verify the changes by executing the Docker command without the sudo command. This write-up has demonstrated the method to use Docker without sudo.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.