如何在Azure管道中运行Docker容器? [英] How to run a docker container in Azure pipeline?

查看:64
本文介绍了如何在Azure管道中运行Docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Azure文档(https://docs.microsoft.com/zh-cn/azure/devops/pipelines/tasks/build/docker?view=azure-devops )未指定如何在Azure管道中运行Docker容器.我们可以使用Docker @ 2任务来构建/推送docker镜像,但是它没有运行容器的命令.通过查看较旧版本的Docker任务的源代码,我可以看到有一个run命令,但现在已弃用了这些命令,并且找不到文档.

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.

推荐答案

显然,问题中提到的配置将提取图像并运行步骤(在本例中为脚本中的 printenv 命令)容器.临时工作目录将自动挂载,并将在该目录中运行.但是,这不会运行容器本身.(将不会执行Dockerfile中定义的 CMD 命令)

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)

为了运行容器本身,我们必须使用 Docker @ 2 内置任务登录Docker注册表,然后手动执行 docker run 作为脚本.这是一个例子,

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 

这篇关于如何在Azure管道中运行Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆