Running Docker commands without sudo - NidhiSingh0068/Docker-Content Wiki

By default, we can only use the docker commands with root privileges. Ubuntu requires the use of the sudo prefix. For example, if you try to run a hello-world container, the output displays permission was denied.

image

To bypass typing sudo every time. Adding the user to the docker group grants privileges equivalent to root.

  1. Create the docker group with command.

sudo groupadd docker

  1. Now, type the following command (making sure to replace [user] with your username):

sudo usermod -aG docker [user]

  1. Enable the new settings with:

su - [user]

  1. Lastly, check to confirm the user is now a part of the docker group by running:

id -nG

  1. To apply these changes, need to reload the shell in order to make the changes take effect. Often need to reboot shell.

sudo reboot

Now, setup is done to run docker commands without doing sudo everytime, i.e user is now added to the docker group.

Let's test the docker command without sudo.

image