I recently installed docker on one machine with Ubuntu 18.04.4. While trying to work with docker command, i faced unable to connect with docker socket. In this post we will see how we can connect to docker socket as a normal user.

SetUP

Machine and Docker details:-

ssirohi@srv2:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.4 LTS
Release:	18.04
Codename:	bionic

ssirohi@srv2:~$ docker --version
Docker version 19.03.6, build 369ce74a3c

But whenever I was trying to connect docker through any docker command, i am not able to do so , like below.

ssirohi@srv2:~$ docker ps -a
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied

ssirohi@srv2:~$ docker search centos
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/images/search?limit=25&term=centos: dial unix /var/run/docker.sock: connect: permission 
denied

In above commands it says permission denied while trying to connect to Docker daemon socket.

Now we need to know, How we can resolve this thing. So whenever we installed docker on any machine, it installed with one docker group. We just need to be part of this Group, like below

ssirohi@srv2:~$ grep  docker: /etc/group
docker:x:132:
ssirohi@srv2:~$ sudo usermod  -aG  docker ssirohi
ssirohi@srv2:~$ grep  docker: /etc/group
docker:x:132:ssirohi

Once current users is member of docker group, you need to logoff once and then login again, Now you can connect to docker socket, like below.

ssirohi@srv2:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ssirohi@srv2:~$ 

So now we know how we as normal user could work with docker commands.