You are running docker container through VSTS build pipeline, while running the below commands in Bash script task-

docker build -t $(dockerId).azurecr.io/$(imageName) .
docker login -u $(dockerId) -p $(dockerPassword) $(dockerId).azurecr.io
docker push $(dockerId).azurecr.io/$(imageName)

You receive the below error message when the Linux agent runs the task-

 

2018-10-10T23:01:26.1028602Z Warning: failed to get default registry endpoint from daemon (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.38/info: dial unix /var/run/docker.sock: connect: permission denied). Using system default: https://index.docker.io/v1/
2018-10-10T23:01:26.1048887Z Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/auth: dial unix /var/run/docker.sock: connect: permission denied
2018-10-10T23:01:27.6360125Z Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/images/*****.azurecr.io/containerbuilds.70/push?tag=: dial unix /var/run/docker.sock: connect: permission denied
2018-10-10T23:01:27.6506375Z ##[error]Bash exited with code ‘1’.
2018-10-10T23:01:27.6579092Z ##[section]Finishing: Bash Script

 

Solution:

 

The error indicates that the user doesn’t have access to docker. To fix it, login to the Linux agent server and run the below command to allow access to the user-

 

sudo usermod -aG docker $USER

 

Logout of the session and login back. It is required for the group change to take effect.
To confirm that the access issue is fixed,  run this command-

 

docker run hello-world

 

If you get the below response then it confirms it is fixed-
—————————————————————————

Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aac357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

————————————————————————–

 

Now run the VSTS build pipeline again. The BASH script task should run fine. In case the error is still observed then you have to reboot the Linux agent server using below command.

 

sudo reboot

 

The problem should be fixed after rebooting.