Docker Without sudo

Share on:

Assumptions/ Prerequisites

  1. You have installed docker cli and runtime on you ubuntu machine.
  2. You have root access to the above mentioned ubuntu machine.

Run docker command without sudo on Linux

Usually when you install docker on Linux machines like Ubuntu for example, you cannot run docker commands without sudo or root access. This is because Docker daemon binds to a unix socket and by default that Unix socket is owned by "root" user. To avoid prefixing sudo each time you run a docker command you can follow the below steps

  1. Create a docker usergroup
1sudo group add docker
  1. Add your current user to docker group.
1sudo usermod -aG docker $USER.

Here -a flag adds or appends the user to the group docker provided by -G flag. Catch is your user will be removed from other groups.

  1. To make the changes effective run the following command, or simply logout and re login.
1newgrp docker 

Here you can find details about newgrp command

Also you can do this in another way, using ACL (Access control list)

1sudo setfacl -m user:<youruser>:rw /var/run/docker.sock

Here you are modifying the ACL of file /var/run/docker.sock by using setfacl command. The -m flag means modify.