Hi! I have docker builds that are dependent on one...
# general
m
Hi! I have docker builds that are dependent on one another and currently am using this syntax.
Copy code
ARG BUILD_IMAGE=//apps/ingress-nginx/build:docker-amd64
ARG BASE_IMAGE=//base-images/alpine:docker-amd64
I’m trying to make this support arm64 as well and we dont have the capability for multi arch manifests (yet) so I need to explicitly specify the arch. I tried this and it doesnt work
Copy code
ARG BUILD_IMAGE=//apps/ingress-nginx/build:docker-${TARGET_ARCH}
ARG BASE_IMAGE=//base-images/alpine:docker-${TARGET_ARCH}
Any guidance on how to effectively do this?
b
heya. Sorry for the trouble. When you say "it doesn't work", what does that mean? Are you seeing errors or unexpected behaviour?
m
Yep- the pants builds errors out because it cannot interpolate the
TARGET_ARCH
argument into the build or base images Heres an example I just ran:
Copy code
ARG TARGETARCH
ARG BASE_IMAGE=//base-images/alpine:docker-${TARGETARCH}
results in
Copy code
f"{arg_name}={address_to_built_image_tag[addr]}"
KeyError: Address(base-images/alpine:docker-${TARGETARCH})
b
Ah, failing with a
KeyError
is definitely a bug. Could you file an issue? Thanks. In terms of making this work… where does
TARGET_ARCH
get set? Is it provided as an environment variable to the pants invocation?
m
It should be inferred by buildkit- I did however pass in it as as var to the pants invocation and got the same result. Ill file a bug
b
Ah, I see. If you're willing to pull the instructions into the
BUILD
file rather than as a separate
Dockerfile
(using https://www.pantsbuild.org/stable/reference/targets/docker_image#instructions), then one can likely format the spec earlier so that Pants' dep inference gets the fully formatted value, rather than rely on docker's env-var substitution that only happens when actually building: • If you're willing to provide an env-var (not necessarily
TARGETARCH
) externally, then
instructions=[...., f"ARG BASE_IMAGE=//base-images/alpine:docker-{env('YOUR_VARIABLE_NAME_HERE')}", ....]
(
env
also accepts an optional default value for if the var isn't set: https://www.pantsbuild.org/stable/docs/using-pants/key-concepts/targets-and-build-files#environment-variables) • Alternatively, one could have duplicate
docker_image
targets via a loop (or a python function) e.g.
Copy code
for arch in ...:
    docker_image(..., instructions=[..., f"ARG BASE_IMAGE=//base-images/alpine:docker-{arch}", ...], ...)
If one wants to keep separate
Dockerfile
s, then I think one will just need to step away from
ARG
-based dep inference, and provide the
dependencies
manually, e.g.
FROM name-of-image
in the dockerfile, and then
dependencies=["//base-images/alpine:docker-..."]
in the build file (this might require duplicating the targets, too)