TL;DR
Docker 卷挂载在 Azure DevOps Pipelines 中工作(至少在 Microsoft 托管代理上)。
如下所示,OP 中描述的两种方法都适用于ubuntu-latest
代理池。如果使用自托管代理my-Linux-pool
问题可能出在他们身上,而不是原帖中共享的 Dockerfile 或管道配置。
请参阅完整的工作演示git repo and Pipeline
EDIT:如果自托管管道代理作为 docker 容器运行,则问题可能来自于内部容器引用仅存在于外部容器中而不存在于主机上的路径。有关从另一个 docker 容器启动 docker 容器时如何挂载卷的更多详细信息,请参阅在 Docker 容器中使用 Docker 挂载卷来自 Azure Pipeline 文档,或者这个答案 from 在 Azure Pipeline 作业中安装 docker run
测试设置
我已经修改了azure-pipelines.yaml
创建一个完全独立的示例:
对以下内容进行了更改azure-pipelines.yml
在OP中
- 将代理池设置为
ubuntu-latest
而不是my-Linux-agents
- 改变
Docker login
一步到一个docker build
实际上将图像构建为管道的一部分的步骤。 (不是必需的,但它使管道独立于自定义图像注册表)
- 添加一个步骤,递归列出存储库中的所有文件及其权限(这样我们就可以轻松验证存储库中的所有文件)
/test
文件夹所有人都可读)
- 将运行两种方法中的容器的步骤添加到同一管道中,以便在同一管道中演示这两种方法
The azure-pipelines.yml现在看起来像这样:
trigger:
- none
jobs:
- job: test
pool:
vmImage: 'ubuntu-latest'
displayName: Run tests
steps:
- task: Docker@2
displayName: Build Docker Image
inputs:
repository: sample/test-app
command: build
Dockerfile: image/Dockerfile
tags: '1.1'
- bash: |
echo $(Build.SourcesDirectory)
ls -lrtR
displayName: List files
- bash: |
echo "Executing docker run command"
docker run --name testApp -i -v $(Build.SourcesDirectory)/test:/test -w /test sample/test-app:1.1
displayName: Run docker inline in pipeline
- task: Bash@3
displayName: Run test.sh
inputs:
targetType: filePath
filePath: 'test/test.sh'
arguments: '$PWD'
workingDirectory: test
其余文件看起来相同,可以找到完整的测试设置here
检测结果
运行管道时,会获得以下输出(可以在此处找到完整的管道运行here
列出文件
/home/vsts/work/1/s
.:
total 16
drwxr-xr-x 2 vsts docker 4096 Sep 18 16:51 test
drwxr-xr-x 2 vsts docker 4096 Sep 18 16:51 image
-rw-r--r-- 1 vsts docker 849 Sep 18 16:51 azure-pipelines.yml
-rw-r--r-- 1 vsts docker 198 Sep 18 16:51 README.md
./test:
total 8
-rw-r--r-- 1 vsts docker 0 Sep 18 16:51 test.txt
-rwxr-xr-x 1 vsts docker 72 Sep 18 16:51 test.sh
-rwxr-xr-x 1 vsts docker 258 Sep 18 16:51 run.sh
./image:
total 8
-rwxr-xr-x 1 vsts docker 67 Sep 18 16:51 entrypoint.sh
-rwxr-xr-x 1 vsts docker 87 Sep 18 16:51 Dockerfile
在管道中内联运行 docker
Executing docker run command
START Running Docker
Listing Files
total 16
drwxr-xr-x 2 1001 121 4096 Sep 18 16:51 .
drwxr-xr-x 1 root root 4096 Sep 18 16:51 ..
-rwxr-xr-x 1 1001 121 258 Sep 18 16:51 run.sh
-rwxr-xr-x 1 1001 121 72 Sep 18 16:51 test.sh
-rw-r--r-- 1 1001 121 0 Sep 18 16:51 test.txt
运行测试.sh
Root path: /home/vsts/work/1/s/test
starting run script
/home/vsts/work/1/s/test
START Running Docker
Listing Files
total 16
drwxr-xr-x 2 1001 121 4096 Sep 18 16:51 .
drwxr-xr-x 1 root root 4096 Sep 18 16:51 ..
-rwxr-xr-x 1 1001 121 258 Sep 18 16:51 run.sh
-rwxr-xr-x 1 1001 121 72 Sep 18 16:51 test.sh
-rw-r--r-- 1 1001 121 0 Sep 18 16:51 test.txt