https://pantsbuild.org/ logo
#general
Title
# general
b

brave-hair-402

03/07/2023, 10:35 AM
I'm using the same Docker base image, "python:3.10.10-slim-bullseye@.sha256:...", in several different
BUILD
files. Is there a way to define this base image in only one place and reuse it across all my
BUILD
files? I've considered using environmental variables, but I'm not sure if that's the best approach. Does anyone have any suggestions on how to achieve this?
r

refined-addition-53644

03/07/2023, 10:41 AM
So is this inside the dockerfile that you are using it or for a docker related target?
b

brave-hair-402

03/07/2023, 10:42 AM
Docker related target.
r

refined-addition-53644

03/07/2023, 10:43 AM
For any target e.g.
docker_environment
, you can use
___defaults___
https://www.pantsbuild.org/docs/targets#field-default-values
b

brave-hair-402

03/07/2023, 10:46 AM
I see. That way I can change the default values. But, I have figured out how to define a global env variable in pants.toml; how can I access it from BUILD files?
r

refined-addition-53644

03/07/2023, 10:47 AM
Can you give an example of what and how your target looks like where you are using this base image repeatedly?
The other way to do it would be using macro https://www.pantsbuild.org/docs/macros
Copy code
def base_docker_image(**kwargs):
     kwargs["base_image"] = base_image
     return your_docker_target(**kwargs)
b

brave-hair-402

03/07/2023, 10:57 AM
I have already created a macro that generates the docker images (for aws lambda). It looks like this:
Copy code
def lambda_docker(name, handler, repository, registries, image_tags, **kwargs):
    entry_point, handler_fun = handler.split(":")
    entry_app, _ = entry_point.split(".")
    entry_dir = build_file_dir().parts[-1]

    pex_binary(
        name="bin-srcs",
        entry_point=entry_point,
        layout="packed",
        include_requirements=False,
        include_tools=True,
    )

    pex_binary(
        name="bin-deps",
        entry_point=entry_point,
        dependencies=["3rdparty/python:poetry#awslambdaric"],
        layout="packed",
        include_sources=False,
        include_tools=True,
    )

    base_image = "python:3.10.10-slim-bullseye@sha256:fcf375288c9348c9708cc7ea3d511b512224219fdc164b6960b3ce85288e1cbf"

    docker_image(
        name=name,
        repository=repository,
        dependencies=[":bin-srcs", ":bin-deps"],
        registries=registries,
        image_tags=image_tags,
        instructions=[
            f"FROM {base_image} as deps",
            f"COPY {'.'.join(build_file_dir().parts) + '/' + 'bin-deps.pex'} /bin-deps.pex",
            "RUN PEX_TOOLS=1 /usr/local/bin/python3.10 /bin-deps.pex venv --scope=deps --compile /app",
            f"FROM {base_image} as srcs",
            f"COPY {'.'.join(build_file_dir().parts) + '/' + 'bin-srcs.pex'} /bin-srcs.pex",
            "RUN PEX_TOOLS=1 /usr/local/bin/python3.10 /bin-srcs.pex venv --scope=srcs --compile /app",
            f"FROM {base_image}",
            "COPY --from=deps /app /app",
            "COPY --from=srcs /app /app",
            "WORKDIR /app",
            'ENTRYPOINT [ "/app/bin/python", "-m", "awslambdaric" ]',
            f'CMD ["{entry_dir}.{entry_app}.{handler_fun}"]',
        ],
        source=None,
    )
r

refined-addition-53644

03/07/2023, 10:58 AM
So I suppose you want to use it inside the instructions?
b

brave-hair-402

03/07/2023, 11:00 AM
Sorry for the delay; had some bitbucket issues. Yes, and also in the build environment definition:
Copy code
+docker_environment(
+    name="lambda_docker_env",
+    platform="linux_x86_64",
+    image="python:3.10.10-slim-bullseye@sha256:fcf375288c9348c9708cc7ea3d511b512224219fdc164b6960b3ce85288e1cbf",
 )
Which is in a different BUILD file. And I am just foreseeing that we will be using python 3.10 docker images alot; I was just trying to figure out how to make it so that we can easily update all of them simultaneously.
r

refined-addition-53644

03/07/2023, 11:06 AM
If it was just docker_image then you can use this https://www.pantsbuild.org/docs/docker#build-arguments
Copy code
[docker]
build_args = [
  "BASE_IMAGE=value1",
]

# Dockerfile
ARG BASE_IMAGE
FROM $BASE_IMAGE
But that doesn’t help with
docker_environment
Currently you can’t use a custom local image for
docker_environment
without pushing it to some docker registry private or public. Otherwise you could create a default
docker_image
target with this base image and use that everywhere
👍 1
e

enough-analyst-54434

03/07/2023, 11:14 AM
It's not well documented, but both the
./pants
script and scie-pants (
pants
) support sourcing a
.pants.bootstrap
file and gathering exported env vars from there. See https://github.com/pantsbuild/pants/discussions/17633 If using scie-pants (
pants
) the more efficient and standard
.env
files are supported as well. See: https://github.com/pantsbuild/scie-pants#features
👀 1
b

brave-hair-402

03/07/2023, 11:15 AM
Yes; I have considered creating a custom local image; that seems like the best option at the moment.
@enough-analyst-54434 If I can export them using .pants.bootstrap; can I access them somehow from BUILD files?
e

enough-analyst-54434

03/07/2023, 11:24 AM
Yes. Although I'm not sure if
env("X", "optional default")
is supported in 2.15.0 or just 2.16.x.
Quick to test.
b

brave-hair-402

03/07/2023, 11:25 AM
I am already using 2.16.0.dev7
e

enough-analyst-54434

03/07/2023, 11:27 AM
Ok, you should be able to use that inline for target field values.
b

broad-processor-92400

03/07/2023, 7:12 PM
You could also define this as a constant “macro”: https://www.pantsbuild.org/docs/macros