Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a docker container in Azure pipeline?

Azure documentation (https://docs.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://docs.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.

like image 850
tharinduwijewardane Avatar asked Aug 25 '20 16:08

tharinduwijewardane


People also ask

How do I use Azure DevOps pipeline Docker?

Sign in to your Azure DevOps organization and navigate to your project. Select Pipelines, and then select New Pipeline to create a new pipeline. Select GitHub YAML, and then select Authorize Azure Pipelines to provide the appropriate permissions to access your repository. You might be asked to sign in to GitHub.

Does Azure support Docker containers?

With Docker deployment on Azure, you are able to run modern and traditional Linux or Windows apps with enterprise-grade security, support and scale.


2 Answers

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 
like image 176
tharinduwijewardane Avatar answered Sep 27 '22 22:09

tharinduwijewardane


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.

like image 38
kamil-mrzyglod Avatar answered Sep 27 '22 21:09

kamil-mrzyglod