Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FROM ${TARGETARCH} wrongly detected as "base name should not be blank" #4012

Closed
timon-schelling opened this issue Jul 20, 2023 · 8 comments · Fixed by #4065
Closed

FROM ${TARGETARCH} wrongly detected as "base name should not be blank" #4012

timon-schelling opened this issue Jul 20, 2023 · 8 comments · Fixed by #4065

Comments

@timon-schelling
Copy link

timon-schelling commented Jul 20, 2023

Dockers build in automatic platform args in the global scope should be recognized and should not be annotated as missing.
https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope

Example:

FROM alpine:latest as amd64
COPY /appbin/x86_64-unknown-linux-gnu/bin /app
RUN chmod +x /app

FROM alpine:latest as arm
COPY /appbin/armv7-unknown-linux-gnueabihf/bin /app
RUN chmod +x /app

FROM ${TARGETARCH} as build
FROM scratch
COPY --from=build /app /app
ENTRYPOINT ["/app"]

image

@bwateratmsft
Copy link
Contributor

@rcjsuen can you take a look?

@bwateratmsft bwateratmsft added this to the 1.28.0 milestone Jul 26, 2023
@rcjsuen
Copy link
Contributor

rcjsuen commented Aug 7, 2023

@timon-schelling Interesting. I guess you are using the AS to set something "equal to" ${TARGETPLATFORM} so that FROM ${TARGETPLATFORM} works. Is this correct?

@timon-schelling
Copy link
Author

@rcjsuen, No ${TARGETPLATFORM} is provided by docker.

See https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope

Docker predefines a set of ARG variables with information on the platform of the node performing the build (build platform) and on the platform of the resulting image (target platform). The target platform can be specified with the --platform flag on docker build.

The following ARG variables are set automatically:

  • TARGETPLATFORM - platform of the build result. Eg linux/amd64, linux/arm/v7, windows/amd64.
  • TARGETOS - OS component of TARGETPLATFORM
  • TARGETARCH - architecture component of TARGETPLATFORM
  • TARGETVARIANT - variant component of TARGETPLATFORM
  • BUILDPLATFORM - platform of the node performing the build.
  • BUILDOS - OS component of BUILDPLATFORM
  • BUILDARCH - architecture component of BUILDPLATFORM
  • BUILDVARIANT - variant component of BUILDPLATFORM

@rcjsuen
Copy link
Contributor

rcjsuen commented Aug 8, 2023

@rcjsuen, No ${TARGETPLATFORM} is provided by docker.

@timon-schelling Right. But to clarify, you can't just use that variable to build, right?

FROM ${TARGETARCH} as build
$ cat Dockerfile
FROM ${TARGETARCH} as build
$ docker build --no-cache .
[+] Building 0.7s (4/4) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                         0.0s
 => => transferring dockerfile: 36B                                                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                              0.0s
 => ERROR [internal] load metadata for docker.io/library/amd64:latest                                                                                                        0.6s
 => [auth] library/amd64:pull token for registry-1.docker.io                                                                                                                 0.0s
------
 > [internal] load metadata for docker.io/library/amd64:latest:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed

@timon-schelling
Copy link
Author

Looks like a buildx thing. I don't know how to go forward than. When using buildx the hint is wrong but otherwise...

@timon-schelling
Copy link
Author

timon-schelling commented Aug 8, 2023

@rcjsuen, No ${TARGETPLATFORM} is provided by docker.

@timon-schelling Right. But to clarify, you can't just use that variable to build, right?

FROM ${TARGETARCH} as build
$ cat Dockerfile
FROM ${TARGETARCH} as build
$ docker build --no-cache .
[+] Building 0.7s (4/4) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                         0.0s
 => => transferring dockerfile: 36B                                                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                              0.0s
 => ERROR [internal] load metadata for docker.io/library/amd64:latest                                                                                                        0.6s
 => [auth] library/amd64:pull token for registry-1.docker.io                                                                                                                 0.0s
------
 > [internal] load metadata for docker.io/library/amd64:latest:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed

Yes one needs to define platform stages first. I define stages for all platforms I need like this:

FROM ... as amd64
...

FROM ... as arm
...

FROM ${TARGETARCH}
...

@rcjsuen
Copy link
Contributor

rcjsuen commented Sep 9, 2023

Since the architecture cannot be deduced with certainty I think I may have to just loosen the rules to allow "undefined" variables to be set to a FROM. The FROM instruction is arguably the most important instruction of a Dockerfile and any errors in FROM are fatal. I think dropping this validation should not be a deal breaker given that if a variable was truly undefined the build would fail instantaneously and it should be clear to the user what correction action should be taken.

@rcjsuen
Copy link
Contributor

rcjsuen commented Sep 10, 2023

Opened rcjsuen/dockerfile-utils#119 to track this. Thanks for reporting this, @timon-schelling.

@bwateratmsft bwateratmsft linked a pull request Sep 11, 2023 that will close this issue
@alexyaang alexyaang modified the milestones: 1.28.0, 1.27.0 Sep 19, 2023
@microsoft microsoft locked and limited conversation to collaborators Nov 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants