How to run a docker container in Azure pipeline?

老子叫甜甜 提交于 2021-02-08 06:15:31

问题


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.


回答1:


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 



回答2:


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.



来源:https://stackoverflow.com/questions/63583510/how-to-run-a-docker-container-in-azure-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!