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

2023-11-22

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

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

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

In 入口点.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"]

入口点.sh

#!/bin/sh

echo "START Running Docker"
echo "Listing Files"
ls -la

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中

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Docker 卷安装在 Azure DevOps Pipeline 中不起作用 的相关文章

随机推荐