Hi guys, I'm having trouble with building docker i...
# general
s
Hi guys, I'm having trouble with building docker images. I want to build image based on env variable if available, else using the platform of the local os. A macro I registered so I can use it in build file:
Copy code
def get_docker_platform():
    env_platform = os.getenv("DOCKER_PLATFORM")
    if env_platform:
        return env_platform
    arch = platform.machine()
    if arch == "x86_64":
        return "linux/amd64"
    elif arch in ["aarch64", "arm64"]:
        return "linux/aarch64"
    else:
        raise ValueError(f"Unsupported architecture: {arch}")
This function is working when no env variable is set, but when I try to set the env variable in my terminal it's ignored. do you have any idea how I could fix it?
This is how I'm using it in build file:
Copy code
TARGET_BUILD_PLATFORM = get_docker_platform()
docker_image(
    context_root="src",
    build_platform=[TARGET_BUILD_PLATFORM],
)
e
You can't use
os.getenv
in a build file or macro, but you can use
env
instead: https://www.pantsbuild.org/stable/reference/build-file-symbols/env
s
is it possible to use env() in macro defined outside of build file?
e
Yeah, I have a macro that generates docker images from a python entrypoint script. It uses
env
to get the image tags from the environment.
s
Thanks!
👍 1