I have a structure where i have a set of container...
# general
f
I have a structure where i have a set of containers that are generated by parameterization
Copy code
❯ pants list common/base-containers::
common/base-containers:images-python@parametrize=py310alpine
common/base-containers:images-python@parametrize=py310deb
common/base-containers:images-python@parametrize=py311alpine
common/base-containers:images-python@parametrize=py311deb
common/base-containers:images-python@parametrize=py312alpine
common/base-containers:images-python@parametrize=py312deb
What is the "BASE_IMAGE" name to use for a sibling container, to choose (in the Dockerfile ? )
Copy code
# services/myservice/BUILD

docker_image(
  name="myservice_cntr",
  repository="therepository",
  image_tags=["{build_args.DEFAULT_TAG}"],
  dependencies= [
    "//common/base-containers:images-python@parametrize=py310deb",
  ],
)
In the Dockerfile - what is the correct format, to use the container that is a dependee ?
Copy code
#services/myservce/Dockerfile

# ARG BASE_IMAGE=common/base-containers:images-python@parametrize=py310deb
# ARG BASE_IMAGE=//common/base-containers:images-python@parametrize=py310deb
# ARG BASE_IMAGE=:images-python@parametrize=py310deb
ARG BASE_IMAGE=../../common.base-containers:images-python@parametrize=py310deb
FROM $BASE_IMAGE
👀 1
b
Sorry for the trouble. Did you try them and find they didn’t work?
f
yes I tried all of those - I have not • tried a non parameterised "other" folder docker image I have - worked around the problem right now by passing the image i want, via extra_args - but local dev and the CI/CD pipeline have different docker registry addresses - so I will investigate that - at another time.
what does a "non" parameterised (seperate BUILD) file inference look like ?
b
I’m not 100% sure! From https://www.pantsbuild.org/blog/2022/08/02/optimizing-python-docker-deploys-using-pants#multiple-images-and-tagging it looks like it may use the repository and registry etc rather than pants path
f
okay - so "inference" of the image, only works if it is in the same BUILD maybe - (we will play) I will have the "working" local + pipeline running later tomorrow - so I will post the result of what it looks like.
right now I have the below - but that will need to change when the image changes in CI/CD
Copy code
version_and_changelog(hyphenated_format=True)

docker_image(
  name="mything",
  repository="mything",
  image_tags=["{build_args.DEFAULT_TAG}"],
  extra_build_args=["BASE_IMAGE=registry.internal/library/python:3.10-slim"],
  dependencies= [
    ...,
    "//common/base-containers:images-python@parametrize=py310deb",
  ],
)
c
this does look like a bug, thanks for flagging.. investigating..
once that's in, to answer the original question (for the record) both of these should then work:
Copy code
# ARG BASE_IMAGE=common/base-containers:images-python@parametrize=py310deb
# ARG BASE_IMAGE=//common/base-containers:images-python@parametrize=py310deb
what does a "non" parameterised (seperate BUILD) file inference look like ?
not sure what you mean here... it's the same, only difference is the address used for the build ARG value in the Dockerfile instruction. Or this became a non-question now that we know you hit a bug? 🙂
f
I think it is a non-question .. the bug masked my problem.
👍 1
@curved-television-6568 - I have some testing data now for you - which makes it clear where / and how parametrized and injected images names do and don't work. Context: I am using pants 2.20.0rc1 which has the fix for https://github.com/pantsbuild/pants/pull/20633 In summary the
pants docker base_image (FROM via ARGS)
mechanism the build-arg BASE_IMAGE/ARG can either be - static, repository reachable, images inside the Dockerfile - static, inside the Dockerfile - pants targets, (relative, absolute, and parametrized) - "dynamic", passed in via the extra_build_args, ONLY if FROM image value is a : repository reachable, images inside the Dockerfile (not a pants inferenced one) to be clear, target inference only works on the static Dockerfile, it does not update, or make changes to any "args" being passed to
docker .. --build_args
for clarity This is the output run (but you need to see the BUILD and Dockerfiles to understand what is happening)
Copy code
❯ pants list containers/sample:: | xargs -Ixx sh -c "echo ======= xx; pants package xx 2>&1 | sed 's#^#  #'"
======= containers/sample:sample-external-image-via-build_args
  08:10:44.95 [INFO] Starting: Building docker image sample-external-image-via-build_args:latest
  08:10:53.83 [INFO] Completed: Building docker image sample-external-image-via-build_args:latest
  08:10:53.84 [INFO] Wrote dist/containers.sample/sample-external-image-via-build_args.docker-info.json
  Built docker image: sample-external-image-via-build_args:latest
  Docker image ID: sha256:fed834eb510decd18e8cdc0e4abf9b1d8ecb293f58941cd616f7725ce0e00479
======= containers/sample:sample-other-pants-tgt-basic-image-relative-via-build_args
  08:11:00.68 [INFO] Starting: Building docker image python:3.11-slim
  08:11:03.16 [INFO] Completed: Building docker image python:3.11-slim
  08:11:03.16 [INFO] Starting: Building docker image sample-other-pants-tgt-basic-image-relative-via-build_args:latest
  08:11:05.01 [INFO] Completed: Building docker image sample-other-pants-tgt-basic-image-relative-via-build_args:latest
  08:11:05.01 [WARN] Docker build failed for `docker_image` containers/sample:sample-other-pants-tgt-basic-image-relative-via-build_args. There are files in the Docker build context that were not referenced by any `COPY` instruction (this is not an error):

    * containers.common/python-3-11-basic.docker-info.json
    * my_simple_lib-4.3.1-py3-none-any.whl
    * my_simple_lib-4.3.1.tar.gz


  08:11:05.01 [ERROR] 1 Exception encountered:

  Engine traceback:
    in `package` goal

  ProcessExecutionFailure: Process 'Building docker image sample-other-pants-tgt-basic-image-relative-via-build_args:latest' failed with exit code 1.
  stdout:

  stderr:
  #0 building with "default" instance using docker driver

  #1 [internal] load .dockerignore
  #1 transferring context: 2B done
  #1 DONE 0.0s

  #2 [internal] load build definition from Dockerfile
  #2 transferring dockerfile: 115B done
  #2 DONE 0.0s

  #3 [internal] load metadata for <http://docker.io/containers/common:python-3-11-basic|docker.io/containers/common:python-3-11-basic>
  #3 ERROR: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
  ------
   > [internal] load metadata for <http://docker.io/containers/common:python-3-11-basic|docker.io/containers/common:python-3-11-basic>:
  ------
  Dockerfile:2
  --------------------
     1 |     ARG BASE_IMAGE
     2 | >>> FROM $BASE_IMAGE
     3 |
     4 |     RUN apt update && apt install -y \
  --------------------
  ERROR: failed to solve: containers/common:python-3-11-basic: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed



  Use `--keep-sandboxes=on_failure` to preserve the process chroot for inspection.

======= containers/sample:sample-other-pants-tgt-parametrized-image-absolute-via-build_args
  08:11:12.22 [INFO] Starting: Building docker image python:3.11-slim
  08:11:13.82 [INFO] Completed: Building docker image python:3.11-slim
  08:11:13.83 [INFO] Starting: Building docker image sample-other-pants-tgt-parametrized-image-absolute-via-build_args:latest
  08:11:13.93 [INFO] Completed: Building docker image sample-other-pants-tgt-parametrized-image-absolute-via-build_args:latest
  08:11:13.94 [WARN] Docker build failed for `docker_image` containers/sample:sample-other-pants-tgt-parametrized-image-absolute-via-build_args. There are files in the Docker build context that were not referenced by any `COPY` instruction (this is not an error):

    * containers.common/images-python@parametrize=py311deb.docker-info.json
    * my_simple_lib-4.3.1-py3-none-any.whl
    * my_simple_lib-4.3.1.tar.gz


  08:11:13.94 [ERROR] 1 Exception encountered:

  Engine traceback:
    in `package` goal

  ProcessExecutionFailure: Process 'Building docker image sample-other-pants-tgt-parametrized-image-absolute-via-build_args:latest' failed with exit code 1.
  stdout:

  stderr:
  #0 building with "default" instance using docker driver

  #1 [internal] load .dockerignore
  #1 transferring context: 2B done
  #1 DONE 0.0s

  #2 [internal] load build definition from Dockerfile
  #2 transferring dockerfile: 115B done
  #2 DONE 0.0s
  Dockerfile:2
  --------------------
     1 |     ARG BASE_IMAGE
     2 | >>> FROM $BASE_IMAGE
     3 |
     4 |     RUN apt update && apt install -y \
  --------------------
  ERROR: failed to solve: failed to parse stage name "//containers/common:images-python@parametrize=py311deb": invalid reference format



  Use `--keep-sandboxes=on_failure` to preserve the process chroot for inspection.

======= containers/sample:sample-other-pants-tgt-parametrized-image-relative-via-build_args
  08:11:20.97 [INFO] Starting: Building docker image python:3.11-slim
  08:11:22.59 [INFO] Completed: Building docker image python:3.11-slim
  08:11:22.59 [INFO] Starting: Building docker image sample-other-pants-tgt-parametrized-image-relative-via-build_args:latest
  08:11:22.70 [INFO] Completed: Building docker image sample-other-pants-tgt-parametrized-image-relative-via-build_args:latest
  08:11:22.70 [WARN] Docker build failed for `docker_image` containers/sample:sample-other-pants-tgt-parametrized-image-relative-via-build_args. There are files in the Docker build context that were not referenced by any `COPY` instruction (this is not an error):

    * containers.common/images-python@parametrize=py311deb.docker-info.json
    * my_simple_lib-4.3.1-py3-none-any.whl
    * my_simple_lib-4.3.1.tar.gz


  08:11:22.70 [ERROR] 1 Exception encountered:

  Engine traceback:
    in `package` goal

  ProcessExecutionFailure: Process 'Building docker image sample-other-pants-tgt-parametrized-image-relative-via-build_args:latest' failed with exit code 1.
  stdout:

  stderr:
  #0 building with "default" instance using docker driver

  #1 [internal] load .dockerignore
  #1 transferring context: 2B done
  #1 DONE 0.0s

  #2 [internal] load build definition from Dockerfile
  #2 transferring dockerfile: 115B done
  #2 DONE 0.0s
  Dockerfile:2
  --------------------
     1 |     ARG BASE_IMAGE
     2 | >>> FROM $BASE_IMAGE
     3 |
     4 |     RUN apt update && apt install -y \
  --------------------
  ERROR: failed to solve: failed to parse stage name "containers/common:images-python@parametrize=py311deb": invalid reference format



  Use `--keep-sandboxes=on_failure` to preserve the process chroot for inspection.

======= containers/sample:sample-other-pants-tgt-parametrized-image-via-static_in_dockerfile
  08:11:29.59 [INFO] Starting: Building docker image python:3.11-slim
  08:11:31.23 [INFO] Completed: Building docker image python:3.11-slim
  08:11:31.24 [INFO] Starting: Building docker image sample-other-pants-tgt-parametrized-image-via-static_in_dockerfile:latest
  08:11:35.83 [INFO] Completed: Building docker image sample-other-pants-tgt-parametrized-image-via-static_in_dockerfile:latest
  08:11:35.83 [INFO] Wrote dist/containers.sample/sample-other-pants-tgt-parametrized-image-via-static_in_dockerfile.docker-info.json
  Built docker image: sample-other-pants-tgt-parametrized-image-via-static_in_dockerfile:latest
  Docker image ID: sha256:a0c60e6f2e41b60d445837ee76b5cc993f90d7097b302b517d3632d834f06aad
without looking at the code, my summary is that pants docker inference support is only happening on the Dockerfile, not on the build_args going in.
in "logical pseudo code" • It appears like pants does -
sed -i s/pants_tgt_address/pants_image_sha_pre-built/g Dockerfile
• and not
cat $pants_docker_cmd_line_args | sed s/pants_tgt_address/pants_image_sha_pre-built/g
c
Ah, I see what you're going for here, and that is indeed not supported. The docs perhaps hints at this, but I see now it could've been made more clear and explicit: https://www.pantsbuild.org/2.19/docs/docker#dependency-inference-support
But, I see how this could be useful, so will ponder a bit at how difficult it would be to support.. 🙂
👍 1
f
Yes the docs (and I'm happy to expand) need examples of what does (and does not work )
c
yea, it's the phrasing "Pants will provide the built Docker image name for that target as the
BASE_IMAGE
build arg to the Docker build command." that was intended to say: You can't use this as a regular build arg.. 😉
👍 1
f
I mean negative docs i dont like ;-) yeah :-)
c
no, but it could be called out explicitly stating that pants only looks at the default value for the arg in the Dockerfile, so if you provide a value yourself, it's going to be used verbatim as a regular build arg.