How Can I Use Docker Without sudo?

Docker is a free and open-source tool used by developers to automate the deployment, scaling, and management of applications inside containers (isolated environments). It enables the user to combine the source code of the application with the required libraries and dependencies into a single unit that can run anywhere. By default, only the root user (or the user having sudo permission) is allowed to interact with docker’s daemon.

This article will demonstrate how the user can use docker without sudo.

Default Working of Docker Without sudo

Running the Docker command requires sudo privileges, it confirms that the particular command is executed in a secure manner or by the user (member of Docker’s group). If we run the docker command without sudo, it will print the error message as seen below:

$ docker run hello-world

How to Use Docker Without sudo? 

By default, the docker commands cannot be run without sudo until the respective user is added to the docker group. To run the docker command without sudo, go through the following steps.

Step 1: Create the Docker Group

The first step is to create the docker’s group if it does not exist, run the following command in the terminal:

$ sudo groupadd docker

Step 2: Add User to the Docker Group

The next step is to add the user to the docker’s group, use the following command for adding the current:

$ sudo usermod -aG docker $USER

You can add any user to the docker’s group using the below command syntax:

$ sudo usermod -aG docker <username>

Step 3: Restart the System

Now, restart the system to see the applied changes:

$ sudo reboot

Verify the Results

Once the system restarts, the user will be able to run the docker command without sudo. Let’s check it by running the command in the terminal:

$ docker run hello-world

The docker’s command is working without sudo.

Conclusion

To run docker without sudo, create the docker group and add the particular user to the docker’s group. The members of the docker group are allowed to use the Unix socket (which is bound with the Docker daemon) without sudo. This post has demonstrated all the steps to use Docker without sudo.