Docker 卷安装在 Azure DevOps Pipeline 中不起作用

Mik*_*rsh 6 docker azure-devops azure-pipelines azure-pipelines-yaml

Docker 卷安装在 Azure DevOps Pipeline 中不起作用,请在下面找到我的代码:

\n

我尝试了两种方法在管道中运行我的 docker 容器 - 请参阅下面 - 都返回空卷 - 卷安装没有发生。我不确定我在这里犯了什么错误。如果有人能帮助我解决这个问题,我将不胜感激。

\n

我想将run.sh、test.sh 和 test.txt挂载在/test

\n

entrypoint.sh中- 我列出了docker内的所有文件 - 但它返回空 - 未安装所有这些文件run.sh、test.sh 和 test.txt

\n

在过去的两天里,我一直在努力解决这个问题,但没有得到任何解决方案 - 任何帮助将不胜感激。

\n

这是我的文件夹结构:

\n
my-app/\n\xe2\x94\x9c\xe2\x94\x80 test/\n\xe2\x94\x82  \xe2\x94\x9c\xe2\x94\x80 run.sh\n\xe2\x94\x82  \xe2\x94\x9c\xe2\x94\x80 test.sh\n\xe2\x94\x82  \xe2\x94\x9c\xe2\x94\x80 test.txt\n\xe2\x94\x9c\xe2\x94\x80 azure-pipeline.yml\n
Run Code Online (Sandbox Code Playgroud)\n

测试文件

\n
#!/bin/bash\n\nrootPath=$1\necho "Root path: $rootPath"\n./run.sh $rootPath\n
Run Code Online (Sandbox Code Playgroud)\n

运行sh

\n
#!/bin/bash\n\necho "starting run script"\n\nNAME="testApp"\nIMAGE="sample/test-app:1.1"\nROOTPATH=$1\n\necho "$ROOTPATH"\n# Finally run\ndocker stop $NAME > /dev/null 2>&1\ndocker rm $NAME > /dev/null 2>&1\ndocker run --name $NAME -i -v $ROOTPATH:/test -w /test $IMAGE\n
Run Code Online (Sandbox Code Playgroud)\n

azure-pipeline.yml(方法-1)

\n
trigger:\n  - none\n\njobs:\n  - job: test\n    pool:\n      name: my-Linux-agents\n    displayName: Run tests\n    steps:\n      - task: Bash@3\n        displayName: Docker Prune\n        inputs:\n          targetType: inline\n          script: |\n            docker system prune -f -a\n\n      - task: Docker@2\n        displayName: Docker Login\n        inputs:\n          containerRegistry: myRegistry w/ asdf\n          command: login\n\n      - task: Bash@3\n        displayName: Execute Sample Java\n        inputs:\n          targetType: filePath\n          filePath: 'test/test.sh'\n          arguments: '$PWD'\n          workingDirectory: test\n
Run Code Online (Sandbox Code Playgroud)\n

azure-pipeline.yml(方法-2)

\n
trigger:\n  - none\n\njobs:\n  - job: test\n    pool:\n      name: my-Linux-agents\n    displayName: Run tests\n    steps:\n      - task: Bash@3\n        displayName: Docker Prune\n        inputs:\n          targetType: inline\n          script: |\n            docker system prune -f -a\n\n      - task: Docker@2\n        displayName: Docker Login\n        inputs:\n          containerRegistry: myRegistry w/ asdf\n          command: login\n\n      - bash: |\n          echo "Executing docker run command"\n          echo $(Build.SourcesDirectory)\n          echo $PWD\n          docker run --name testApp -i -v $(Build.SourcesDirectory):/test -w /test sample/test-app:1.1\n
Run Code Online (Sandbox Code Playgroud)\n

我的 Docker 映像 - 文件\n Dockerfile

\n
FROM alpine:3.12\n\nCOPY entrypoint.sh /\nRUN echo "hello"\n\nENTRYPOINT ["/entrypoint.sh"]\n
Run Code Online (Sandbox Code Playgroud)\n

入口点.sh

\n
#!/bin/sh\n\necho "START Running Docker"\necho "Listing Files"\nls -la\n
Run Code Online (Sandbox Code Playgroud)\n

dan*_*orn 6

长话短说

Docker 卷挂载在 Azure DevOps Pipelines 中工作(至少在 Microsoft 托管代理上)。

如下所示,OP 中描述的两种方法都适用于ubuntu-latest代理池。如果使用自托管代理,则my-Linux-pool问题可能出在它们身上,而不是原始帖子中共享的 Dockerfile 或管道配置。

请参阅git repoPipeline的完整工作演示

编辑:如果自托管管道代理作为 docker 容器运行,问题可能来自于内部容器引用仅存在于外部容器中而不存在于主机上的路径。有关如何在从另一个 docker 容器启动 docker 容器时挂载卷的更多详细信息,请参阅Azure Pipeline 文档中有关在 Docker 容器内使用 Docker 挂载卷的部分,或来自Mounting docker run in Azure Pipeline job 的答案

测试设置

我已经修改了azure-pipelines.yaml创建一个完全独立的示例:

azure-pipelines.ymlOP 中进行了以下更改

  1. 将代理池设置为ubuntu-latest而不是my-Linux-agents
  2. Docker login将步骤更改为docker build实际构建图像作为管道一部分的步骤。(不是必需的,但它使管道独立于自定义图像注册表)
  3. 添加一个步骤,递归列出存储库中的所有文件及其权限(以便我们可以轻松验证文件夹中的所有文件是否/test可供所有人读取)
  4. 将运行两种方法中的容器的步骤添加到同一管道中,以便在同一管道中演示这两种方法

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
Run Code Online (Sandbox Code Playgroud)

其余文件看起来相同,完整的测试设置可以在这里找到

检测结果

运行管道时,会获得以下输出(完整的管道运行可以在此处找到

列出文件

/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
Run Code Online (Sandbox Code Playgroud)

在管道中内联运行 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
Run Code Online (Sandbox Code Playgroud)

运行测试.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

Run Code Online (Sandbox Code Playgroud)