docker - docker-compose不挂载测试结果文件夹

标签 docker docker-compose docker-volume

作为构建docker-compose文件的一部分运行单元测试后,在容器中创建的文件未显示在我的本地文件系统上。

我有以下Dockerfile:

# IDM.Test/Dockerfile
FROM microsoft/aspnetcore-build:2.0
WORKDIR /src

# Variables
ENV RESTORE_POLICY --no-restore
ENV IGNORE_WARNINGS -nowarn:msb3202,nu1503

# Restore
COPY IDM.sln ./

# Copying and restoring other projects...

COPY IDM.Test/IDM.Test.csproj IDM.Test/
RUN dotnet restore IDM.Test/IDM.Test.csproj $IGNORE_WARNINGS

# Copy
COPY . .

# Test
RUN dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml"
RUN ls -alR

运行RUN ls -alR时,我可以看到文件/src/IDM.Test/TestResults/test-results.xml是在容器内生成的。到现在为止还挺好。

我正在使用docker-compose -f docker-compose.test.yml build开始构建。
docker-compose看起来像这样:
version: '3'

services:
  idm.webapi:
    image: idmwebapi
    build:
      context: .
      dockerfile: IDM.Test/Dockerfile
    volumes:
      - ./IDM.Test/TestResults:/IDM.Test/TestResults/

我已经在本地创建了IDM.Test/TestResults文件夹,但是在成功运行docker-compose build命令后没有任何显示。

有什么线索吗?

最佳答案

也许有了这个解释我们就能解决。让我逐步说出一些显而易见的事情,以避免混淆。容器创建有两个步骤:

  • docker build / docker-compose build->创建镜像
  • docker run / docker compose up / docker-compose run->创建容器

  • 在第二步中创建卷(容器创建),而命令dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml"在第一个命令中执行(图像创建)。

    If you creates a folder inside container in the same path where you've mounted volume, data in this new folder will only be available locally inside container.



    明确地说,我的建议可以从以下几点恢复:
  • 检查是否在构建阶段未创建装入卷的目标文件夹,因此在Dockerfile中未定义任何RUN mkdir /IDM.Test/TestResults/
  • 另一个小建议不是强制性的,但我想在docker-compose文件中定义具有绝对路径的卷。
  • 请勿在Dockerfile中执行会在外部生成所需数据的命令,除非您将此命令指定为ENTRYPOINT或CMD,而不是RUN。
  • 在Dockerfile中,ENTRYPOINT或CMD(或命令:在docker-compose中)指定在容器启动时在buildind之后执行的命令。

  • 尝试使用此Dockerfile:
    # IDM.Test/Dockerfile
    FROM microsoft/aspnetcore-build:2.0
    WORKDIR /src
    
    # Variables
    ENV RESTORE_POLICY --no-restore
    ENV IGNORE_WARNINGS -nowarn:msb3202,nu1503
    
    # Restore
    COPY IDM.sln ./
    
    # Copying and restoring other projects...
    
    COPY IDM.Test/IDM.Test.csproj IDM.Test/
    RUN dotnet restore IDM.Test/IDM.Test.csproj $IGNORE_WARNINGS
    
    # Copy
    COPY . .
    
    # Test
    CMD dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml"
    

    或者这个docker-compose:
    版本:“3”
    services:
      idm.webapi:
        image: idmwebapi
        build:
          context: .
          dockerfile: IDM.Test/Dockerfile
        volumes:
          - ./IDM.Test/TestResults:/IDM.Test/TestResults/
        command: >
          dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml"
    

    创建容器后,可以使用ls检查生成的文件。

    关于docker - docker-compose不挂载测试结果文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873949/

    相关文章:

    mongodb - Docker/Mongodb 数据不持久

    ruby-on-rails - Docker,Rails,Postgres-找不到数据库服务错误

    docker - 使用 docker-compose 时无法连接到 postgres

    apache - Apache HTTP Server Docker镜像-需要在Dockerfile中进行更干净的配置

    docker - Docker在docker swarm群集上组成单独的节点上的单独容器

    Docker 中的 Docker - 第二层中的卷不工作 : Full of files in 1st level container, 为空

    Docker-compose 卷 : persistant, 但在哪里?

    apache - Docker apache 图像,将日志存储在主机中?

    php - Docker MySQL mysqli::real_connect():(HY000/2002):连接被拒绝

    docker - 容器创建停留在ContainerCreating状态