29

Azure documentation (https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/build/docker?view=azure-devops) does not specify how to run a docker container in Azure pipeline. We can use the Docker@2 task to build / push docker images but it does not have a command to run a container. By looking at source code of older versions of Docker task I can see there has been a run command, but those are now deprecated and there is no documentation to be found.

I also followed the doc: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/container-phases?view=azure-devops With following yaml I was able to pull a docker image which was previously pushed to ACR. (my-acr is a service connection I added via project settings)

pool:
  vmImage: 'ubuntu-16.04'

container:
  image: somerepo/rnd-hello:latest
  endpoint: my-acr

steps:
- script: printenv

But I cannot get the container to run.

2
  • How about using Docker@1 task? Commented Aug 26, 2020 at 9:56
  • Is the following reply helpful? Is your issue solved? Commented Aug 28, 2020 at 3:24

2 Answers 2

35

Apparently the configuration mentioned in the question will pull the image and run the step (in this case printenv command in the script) inside the container. A temporary working directory will be mounted automatically and it will run inside that dir. However this will not run the container itself. (CMD command defined in the Dockerfile will not be executed)

In order to run the container itself we have to login to docker registry with Docker@2 inbuilt task and then manually execute the docker run as a script. Here is an example,

trigger: none

jobs:
- job: RunTest
  workspace:
    clean: all
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - task: Docker@2
    displayName: Login to ACR
    inputs:
      command: login
      containerRegistry: my-acr
  - script: |
      docker run my-registry.azurecr.io/somerepo/rnd-hello:latest 
Sign up to request clarification or add additional context in comments.

Comments

3

If you want, you can simply use a shell command to execute docker run and simply rely on that for all the further steps in your pipeline. You don't need to use Docker tasks in Pipelines to be able to communicate with the daemon.

Another solution would be using Azure Container Registry for running a container, but that seems like the last resort in case something went wrong with Pipelines.

4 Comments

I tried adding docker run somerepo/rnd-hello:latest to the script section. But it does not recognise the docker command.
@tharinduwijewardane Have you tried adding Docker CLI installer step before?
No, I added that step, thanks. I suppose I have to do a docker login too. I ran a ls and did not see the image pulled before. Is there a way to use the server connection? or do I have to login providing credentials?
Images are available only per agent run - they are ephemeral so images fetched will not be persisted. Yes, you probably need to run docker login depending on the source of your images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.