Docker卷装载在Azure DevOps管道中无法正常工作 [英] Docker volume mounts not working in Azure DevOps Pipeline
问题描述
Docker卷装载在Azure DevOps管道中不起作用,请找到下面的代码:
我尝试了两种方法在管道中运行我的停靠容器(请参考下面的内容),这两种方法都返回空的卷-没有发生卷挂载。我不确定我在这里做的是什么错误。如果有人能帮我解决这个问题,我将不胜感激。我要挂载/test下的run.sh、test.sh和test.txt
在entrypoint.sh中-我列出了docker内的所有文件,但返回为空-未挂载所有这些文件run.sh、test.sh和test.txt
过去两天我一直在努力解决此问题,但没有得到任何解决方案-任何帮助都将不胜感激。
这是我的文件夹结构:
my-app/
├─ test/
│ ├─ run.sh
│ ├─ test.sh
│ ├─ test.txt
├─ azure-pipeline.yml
test.sh
#!/bin/bash
rootPath=$1
echo "Root path: $rootPath"
./run.sh $rootPath
run.sh
#!/bin/bash
echo "starting run script"
NAME="testApp"
IMAGE="sample/test-app:1.1"
ROOTPATH=$1
echo "$ROOTPATH"
# Finally run
docker stop $NAME > /dev/null 2>&1
docker rm $NAME > /dev/null 2>&1
docker run --name $NAME -i -v $ROOTPATH:/test -w /test $IMAGE
azure-Pipeline.yml(方法-1)
trigger:
- none
jobs:
- job: test
pool:
name: my-Linux-agents
displayName: Run tests
steps:
- task: Bash@3
displayName: Docker Prune
inputs:
targetType: inline
script: |
docker system prune -f -a
- task: Docker@2
displayName: Docker Login
inputs:
containerRegistry: myRegistry w/ asdf
command: login
- task: Bash@3
displayName: Execute Sample Java
inputs:
targetType: filePath
filePath: 'test/test.sh'
arguments: '$PWD'
workingDirectory: test
azure-Pipeline.yml(方法2)
trigger:
- none
jobs:
- job: test
pool:
name: my-Linux-agents
displayName: Run tests
steps:
- task: Bash@3
displayName: Docker Prune
inputs:
targetType: inline
script: |
docker system prune -f -a
- task: Docker@2
displayName: Docker Login
inputs:
containerRegistry: myRegistry w/ asdf
command: login
- bash: |
echo "Executing docker run command"
echo $(Build.SourcesDirectory)
echo $PWD
docker run --name testApp -i -v $(Build.SourcesDirectory):/test -w /test sample/test-app:1.1
我的Docker图像-文件 Dockerfile
FROM alpine:3.12
COPY entrypoint.sh /
RUN echo "hello"
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
#!/bin/sh
echo "START Running Docker"
echo "Listing Files"
ls -la
推荐答案
TL;DR
Docker卷在Azure DevOps管道中装入工作(至少在Microsoft Hosted Agent上)。
如下所示,OP中描述的两种方法都使用ubuntu-latest
代理池。如果在my-Linux-pool
中使用自托管代理,则问题可能出在它们身上,而不是原始帖子中共享的Dockerfile或管道配置。
edit:如果自托管管道代理作为停靠容器运行,则问题可能来自内部容器引用的路径只存在于外部容器中,而不存在于主机上。有关从另一个停靠容器启动停靠容器时如何装入卷的更多详细信息,请参阅Azure Pipeline文档中的Mounting volumes using Docker within a Docker container部分或Mounting docker run in Azure Pipeline job中的this answer部分
测试设置
我已修改azure-pipelines.yaml
以创建一个完全自包含的示例:
对操作中的azure-pipelines.yml
进行了以下更改
- 将代理池设置为
ubuntu-latest
,而不是my-Linux-agents
- 将
Docker login
步骤更改为实际构建作为管道一部分的映像的docker build
步骤。(不是必需的,但它使管道独立于自定义映像注册表) - 添加一个步骤,以递归方式列出存储库中的所有文件及其权限(这样我们就可以轻松地验证
/test
文件夹中的所有文件都是可读的) - 将运行这两种方法的容器的步骤添加到同一管道中,以便在同一管道中演示这两种方法
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
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
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
这篇关于Docker卷装载在Azure DevOps管道中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!