You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取容器生成的制品?Docker测试覆盖率目录提取咨询

Hey there! Let's break down your questions clearly—both are totally solvable, and I'll walk you through practical, actionable ways to handle them.

1. 如何获取容器生成的制品?

There are a few reliable methods to pull artifacts out of a Docker container, depending on your workflow:

  • Use docker cp for direct copying
    This works whether the container is running or stopped. First, grab your container's name or ID (run docker ps -a if it's stopped). Then use the cp command to copy the target directory to your local machine:

    # Replace <container-name-or-id> with your actual container identifier
    docker cp <container-name-or-id>:/path/to/container/artifacts ./local-target-directory
    

    Example: If your container is named test-runner and artifacts live at /app/build/output, run:

    docker cp test-runner:/app/build/output ./local-output
    
  • Bind a local directory during container runtime
    This is my go-to for CI/CD workflows, since it syncs artifacts directly to your local filesystem as they're generated. When starting the container, use the -v flag to mount a local directory to the path where the container creates artifacts:

    docker run -v $(pwd)/local-artifacts:/app/build/output my-container-image
    

    Any files the container writes to /app/build/output will instantly show up in your local ./local-artifacts folder—no post-run copying needed.

  • Multi-stage builds (for artifact-focused images)
    If you're building an image specifically to generate artifacts, you can use a multi-stage Dockerfile to isolate the artifacts in a lightweight image. Then you can extract them later:

    # Stage 1: Generate artifacts
    FROM node:18 as builder
    WORKDIR /app
    COPY package*.json ./
    RUN npm install
    COPY . .
    RUN npm run build # Generates artifacts in /app/dist
    
    # Stage 2: Only keep the artifacts
    FROM scratch
    COPY --from=builder /app/dist /dist
    

    After building this image, create a temporary container to copy the artifacts out:

    docker create --name temp-artifact-container my-artifact-image
    docker cp temp-artifact-container:/dist ./local-dist
    docker rm temp-artifact-container
    
2. 提取容器内生成的coverage目录作为制品是否可行?

Absolutely! This is a super common use case for testing workflows. Here are the most straightforward ways to do it:

When you start your test container, mount a local directory to the path where the coverage report is generated. For example, if your Dockerfile runs pytest and outputs coverage to /app/coverage:

docker run -v $(pwd)/local-coverage:/app/coverage my-test-image

Once the test completes, your local ./local-coverage folder will have all the coverage files ready for analysis (like HTML reports or JSON data).

Option 2: Copy after test run

If you already ran the test container and it's stopped (or still running), use docker cp to grab the directory:

  1. Find your container ID/name with docker ps -a
  2. Run the copy command:
    docker cp <container-id>:/app/coverage ./local-coverage
    

Quick check to verify

Before copying, you can double-check the coverage directory exists in the container by running:

docker exec -it <container-name> ls /app/coverage

This ensures the test actually generated the files you're trying to extract.


内容的提问来源于stack exchange,提问作者Ludo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:08:52