docker / docker/buildx

Feature Request: Allow `docker build` to output additional files from stages

Open
#3,396 19 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/builder
Dominant language
Go
Stars
4.5k
Forks
682
Avg merge
2d 14h
Merged PRs (30d)
29

Description

Description

Currently, it is not possible to extract files from a failed docker build image. For extracting unit test results, this would be very helpful though.

Current Workaround

Current workaround is to execute docker build multiple times with different target stages, and make the unit tests not fail inside the Dockerfile to create a successful image of the unit test run, then create a docker container from that image to extract the unit test result file, and then continue with the remaining build. Something like this:

docker build --force-rm=true --target test-build -t test-build:latest .

id=`docker create test-build:latest`
docker cp $id:/src/.test-results.xml .
docker rm -v $id

unit_test_exit_status=0
docker build --force-rm=true --target test-fail -t test-fail:latest . || unit_test_exit_status=$?
if [ $unit_test_exit_status -ne 0 ]; then
    echo "Some unit tests failed"
    # cleaning up docker images with failed tests to avoid sporadic test timeouts to be cached in docker layers for the next builds with unchanged code base
    docker rmi test-fail:latest || true
    docker rmi test-build:latest || true
    exit $unit_test_exit_status
fi

docker build --force-rm=true --target app -t app:latest .
# syntax = docker/dockerfile:experimental

FROM node:12.16.3-alpine as node

FROM node as build-base
WORKDIR /src
COPY . ./

FROM build-base as test-build
RUN echo "0" > EXIT_STATUS_FILE; npm run test:generate-output:dist || echo $? > EXIT_STATUS_FILE;

# we need test-build not to fail, because we need to extract the test results from it - rather fail in this stage that will be executed separately
FROM test-build as test-fail
RUN exit $(cat EXIT_STATUS_FILE)
RUN npm audit

FROM build-base as app-build
RUN npm run build

FROM node as app
WORKDIR /app
COPY --from=app-build /src/.build ./.build/

CMD ["npm","start"]
Desired Feature

It would be great if a docker stage could have something like a "write only" mount during build.

Example usage:

docker build --force-rm=true -t app:latest --output-mount source="$(pwd)"/docker-output,target=/output .
# syntax = docker/dockerfile:experimental

FROM node:12.16.3-alpine as node

FROM node as build-base
WORKDIR /src
COPY . ./

FROM build-base as test-build
RUN echo "0" > EXIT_STATUS_FILE; npm run test:generate-output:dist || echo $? > EXIT_STATUS_FILE;
RUN --mount=type=bind,target=/output,writeonly cp .test-results.xml /output/
RUN exit $(cat EXIT_STATUS_FILE)
RUN npm audit

FROM build-base as app-build
RUN npm run build

FROM node as app
WORKDIR /app
COPY --from=app-build /src/.build ./.build/

CMD ["npm","start"]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Dockerfile examples and the docker build commands in the issue to understand the requested output-mount behavior and its interaction with failed stages. No source files or tests are named; done means a build can export the requested test-result file while preserving the build's failure status.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.