如何获取容器生成的制品?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.
There are a few reliable methods to pull artifacts out of a Docker container, depending on your workflow:
Use
docker cpfor direct copying
This works whether the container is running or stopped. First, grab your container's name or ID (rundocker ps -aif it's stopped). Then use thecpcommand 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-directoryExample: If your container is named
test-runnerand artifacts live at/app/build/output, run:docker cp test-runner:/app/build/output ./local-outputBind 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-vflag to mount a local directory to the path where the container creates artifacts:docker run -v $(pwd)/local-artifacts:/app/build/output my-container-imageAny files the container writes to
/app/build/outputwill instantly show up in your local./local-artifactsfolder—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 /distAfter 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
Absolutely! This is a super common use case for testing workflows. Here are the most straightforward ways to do it:
Option 1: Bind mount during test run (recommended for CI)
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:
- Find your container ID/name with
docker ps -a - 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

