简体   繁体   中英

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.

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 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM