Our project used to extract test report from docker build with Dockerfile
:
FROM ubuntu AS build-stage
LABEL stage=build
RUN make setup
RUN make test test-report
RUN make build
FROM ubuntu
COPY --from=build-stage /bin /app
And extract report with
docker image -q --filter label=stage=build
to get build-stage image and then use docker cp
to copy report files.
It worked, but when I tried to reproduce it on both local WSL2 and Github Actions, it cannot get the intermediate image anymore. Only final stage image was found.
I have searched for the topic and it seems that new version of docker use builx as default, and should use docker build --target build-stage .
(maybe some file cache reason?).
I also tried to add DOCKER_BUILDKIT=0
and query with docker images -a -q --filter label=stage=build
and successfully got the image, however there’s a deprecated warning, so it seems not a good idea to do so.
Questions
- Use multiple docker build command only for test report is a bit annoying, are there any workaround to simply get the intermediate image back, or are there any better way to copy the files generated during docker build and not in final image?
- If the only option is to target and build intermediate stage first, such as
docker build --target build-stage .
docker build -t myapp:latest .
since that setup and testing were done in first docker build image and build cache will be reused by second docker build command, can I confirm that final image will be the same as simply running docker build -t myapp:latest .
?