I need to run docker commands such as "docker build" and "docker push" in azure devops build pipelines. I know there are tasks available to do these things such as imagebuildingInfo@1 etc. But issue is I need to do this for a new ACR for each pipeline run, and imagebuildingInfo@1 task expects the service connection of type docker registry not azure rm. So I have to create a new service principal of every run of the pipeline which I can’t do.
Is there a way to run docker commands in a script in pipeline? I am trying this
- task: AzureCLI@2
# displayName: Registry Login
# name: acrLogin
# env:
# ADME_SUBSCRIPTION: 'id'
# REGISTRY: 'acr name'
# inputs:
# azureSubscription: 'azure rm service principal'
# scriptType: 'bash'
# scriptLocation: 'inlineScript'
# inlineScript: |
# az acr login -n ${REGISTRY}
But while running this I am getting the error:
WARNING: You may want to use 'az acr login -n <acr_name> --expose-token' to get an access token, which does not require Docker to be installed.
ERROR: 2024-03-20 10:22:28.619871 An error occurred: DOCKER_COMMAND_ERROR
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Which means docker deamon is not running on the agent. We have to use a template which might be causing this not to run. Is there a way to run the docker Deamon explicitly in pipeline?
2
Answers
Self-hosted agentpool
If you are using a self-hosted agentpool where you install the agents yourself, and need to run the
docker
commands directly in other tasks, these are the commands on how I solved that for a self-hosted agentpool and the agent user on the VM:Installing it in this manner should solve the error:
Once installed you should be i.e. be able to do the following:
I hope this helps, good luck!
Based on your description, I could reproduce the issue with a pipeline running on a Linux self-hosted agent machine, where docker and the pipeline agent service were newly installed.
This is because the user that the pipeline agent service is configured to run as, is not granted permission to connect to the Docker daemon. When installing agent service with
sudo ./svc.sh install [username]
, ifusername
parameter is not specified then the username is taken from the $SUDO_USER environment variable which is set by sudo command. This variable is always equal to the name of the user who invoked thesudo
command.Here are my steps to fix the error.
Alvin
in my case) to run pipeline agent service indocker
admin group;Rerun failed jobs
without changing anything in the pipeline;