moby / buildkit

concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit
https://github.com/moby/moby/issues/34227
Apache License 2.0
8.19k stars 1.16k forks source link

Setting `FROM --platform` pointing to a stage is silently ignored #2946

Open ciaranmcnulty opened 2 years ago

ciaranmcnulty commented 2 years ago

I naively tried something like this in my project:

FROM alpine AS base

FROM --platform=${BUILDPLATFORM} base AS expensive
RUN sleep 100 && uname -a > /foo

FROM scratch AS final
COPY --from=expensive /foo .

My expectation was that the expensive stage would only run once on my native platform. However, I see it running twice in parallel

This however only runs the expensive stage once:

FROM --platform=${BUILDPLATFORM} alpine AS expensive
RUN sleep 100 && uname -a > /foo

FROM scratch AS final
COPY --from=expensive /foo .

My guess is that --platform only applies to docker image sources? Should it be an error if used with something else?

tonistiigi commented 2 years ago

We should probably show a warning for this case

cpuguy83 commented 2 years ago

It doesn't seem like this works with docker-image sources either.

docker buildx build --no-cache --platform=linux/arm64 --build-context=busybox:latest=docker-image://busybox:latest - <<EOF
FROM --platform=\${BUILDPLATFORM} busybox:latest
RUN echo hello > /foo
EOF
[+] Building 2.3s (6/6) FINISHED                                                                                                                                                                
 => [internal] load build definition from Dockerfile                                                                                                                                       0.1s
 => => transferring dockerfile: 107B                                                                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                                                                          0.1s
 => => transferring context: 2B                                                                                                                                                            0.0s
 => [context busybox] load metadata for busybox:latest                                                                                                                                     1.6s
 => [auth] library/busybox:pull token for registry-1.docker.io                                                                                                                             0.0s
 => [context busybox] busybox:latest                                                                                                                                                       0.4s
 => => resolve docker.io/library/busybox:latest                                                                                                                                            0.1s
 => => sha256:87379020f3b6731a4b64976e614d305f5c121d153c049d14ba600ff24bbac012 828.68kB / 828.68kB                                                                                         0.2s
 => => extracting sha256:87379020f3b6731a4b64976e614d305f5c121d153c049d14ba600ff24bbac012                                                                                                  0.1s
 => ERROR [1/2] RUN echo hello > /foo                                                                                                                                                      0.2s
------                                                                                                                                                                                          
 > [1/2] RUN echo hello > /foo:
#0 0.119 standard_init_linux.go:228: exec user process caused: exec format error
------
Dockerfile:2
--------------------
   1 |     FROM --platform=${BUILDPLATFORM} busybox:latest
   2 | >>> RUN echo hello > /foo
   3 |     
--------------------
error: failed to solve: process "/bin/sh -c echo hello > /foo" did not complete successfully: exit code: 1[+] Building 2.3s (6/6) FINISHED                                                                                                                                                                
 => [internal] load build definition from Dockerfile                                                                                                                                       0.1s
 => => transferring dockerfile: 107B                                                                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                                                                          0.1s
 => => transferring context: 2B                                                                                                                                                            0.0s
 => [context busybox] load metadata for busybox:latest                                                                                                                                     1.6s
 => [auth] library/busybox:pull token for registry-1.docker.io                                                                                                                             0.0s
 => [context busybox] busybox:latest                                                                                                                                                       0.4s
 => => resolve docker.io/library/busybox:latest                                                                                                                                            0.1s
 => => sha256:87379020f3b6731a4b64976e614d305f5c121d153c049d14ba600ff24bbac012 828.68kB / 828.68kB                                                                                         0.2s
 => => extracting sha256:87379020f3b6731a4b64976e614d305f5c121d153c049d14ba600ff24bbac012                                                                                                  0.1s
 => ERROR [1/2] RUN echo hello > /foo                                                                                                                                                      0.2s
------                                                                                                                                                                                          
 > [1/2] RUN echo hello > /foo:
#0 0.119 standard_init_linux.go:228: exec user process caused: exec format error
------
Dockerfile:2
--------------------
   1 |     FROM --platform=${BUILDPLATFORM} busybox:latest
   2 | >>> RUN echo hello > /foo
   3 |     
--------------------
error: failed to solve: process "/bin/sh -c echo hello > /foo" did not complete successfully: exit code: 1
cpuguy83 commented 2 years ago

The failure of course requires that you don't have binfmt or something else loaded.

ciaranmcnulty commented 1 year ago

This happened to me again today, and I went and asked on the Slack about it 🤦

A warning or fail at runtime would be useful "Can not override platform for target foo, using linux/arm64" or something