I'm trying to get dependency inference to work cor...
# general
e
I'm trying to get dependency inference to work correctly in my docker images. FWIW I'm doing this in a macro (note the f-strings). With a set of "instructions" like this, it looks like pants is correctly inferring the BASE_IMAGE dependency, but not the DEPS_IMAGE or SRCS_IMAGE dependencies. Am I correct in understanding that ARG only introduces a dependency if that ARG is used in a
FROM $MY_ARG
instruction? (but not in a
COPY --from=$MY_ARG <...>
instruction)
Copy code
instructions = [
    "ARG BASE_IMAGE=src/docker:python_base",
    f"ARG DEPS_IMAGE=:{deps_name}",
    f"ARG SRCS_IMAGE=:{srcs_name}",
    "FROM $BASE_IMAGE",
    'CMD ["/opt/app/pex"]',
    "COPY --from=$DEPS_IMAGE /opt/app /opt/app",
    "COPY --from=$SRCS_IMAGE /opt/app /opt/app",
]
Copy code
instructions = [
    "ARG BASE_IMAGE=src/docker:python_base",
    f"ARG DEPS_IMAGE=:{deps_name}",
    f"ARG SRCS_IMAGE=:{srcs_name}",
    "FROM $SRCS_IMAGE",
    "FROM $DEPS_IMAGE",
    "FROM $BASE_IMAGE",
    'CMD ["/opt/app/pex"]',
    "COPY --from=$DEPS_IMAGE /opt/app /opt/app",
    "COPY --from=$SRCS_IMAGE /opt/app /opt/app",
]
Seems to work (note the extra FROM statements). Which seems a little unwieldy, but better than manually managing the dependencies and using actual docker image names in the instructions
Nevermind, this maps the dependencies correctly, but the ARG used in the COPY instruction isn't readable by docker.
Copy code
instructions = [
    "ARG BASE_IMAGE=src/docker:python_base",
    f"ARG DEPS_IMAGE=:{deps_name}",
    f"ARG SRCS_IMAGE=:{srcs_name}",
    "FROM $SRCS_IMAGE AS SRCS",
    "FROM $DEPS_IMAGE AS DEPS",
    "FROM $BASE_IMAGE",
    'CMD ["/opt/app/pex"]',
    "COPY --from=DEPS /opt/app /opt/app",
    "COPY --from=SRCS /opt/app /opt/app",
]
Explicitly FROM-ing the images and naming them for reuse does work
Am I missing something, or is this the best way to go about this right now?
r
We followed this recipe, and it seems to work:
Copy code
<https://www.pantsbuild.org/blog/2022/08/02/optimizing-python-docker-deploys-using-pants>
e
That was where I started, but I found it uncomfortable that the
COPY --from=<image>
statements could not reference
docker_image
targets, but rather had to reference tagged images (with
registry/repository:tag
) information.
r
I see. We pass the registry and tag as arguments. The repository matches a build target. IMHO this works well enough. I have learnt to be pragmatic with these types of tools and be content when some kind of reasonable solution works sufficiently well. 🙂