Dockerfile:
# syntax=docker/dockerfile:experimental
FROM scratch as production
RUN echo production && touch /artifact
FROM scratch as test
RUN echo test && touch /artifact
FROM scratch
ARG environment=production
COPY --from=${environment} /artifact /artifact
How I run it:
DOCKER_BUILDKIT=1 docker build -f tools/docker/Test.Dockerfile .
Output:
[+] Building 1.5s (4/4) FINISHED
=> [internal] load build definition from Test.Dockerfile 0.0s
=> => transferring dockerfile: 299B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:experimental 0.7s
=> CACHED docker-image://docker.io/docker/dockerfile:experimental@sha256:888f21826273409b5ef5ff9ceb90c64a8f8ec7760da30d1ffbe6c3e2d323a7bd 0.0s
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = invalid reference format
What I'm actually trying to achieve is to build files for production, but copy them from filesystem for tests. The example I provided is a simplification of that :)
P.S. I use experimental syntax here since I actually need --mount=type=cache in actual build, but I get almost the same error without it
you can use:
ARG environment=production
FROM scratch as production
RUN echo production && touch /artifact
FROM scratch as test
RUN echo test && touch /artifact
FROM $environment AS currentenv
FROM scratch
COPY --from=currentenv /artifact /artifact
This is explained in https://medium.com/@tonistiigi/advanced-multi-stage-build-patterns-6f741b852fae
Oh, I missed that in the article. Thank you so much! The article is just awesome.
Most helpful comment
you can use:
This is explained in https://medium.com/@tonistiigi/advanced-multi-stage-build-patterns-6f741b852fae