Is there a way to specify dependencies between doc...
# general
a
Is there a way to specify dependencies between docker images without setting the context root to a common parent directory of all the Dockerfiles? (more context in thread)
Let's say I have a folder structure similar to the following:
Copy code
.
├── pants.toml
└── services
    ├── base_images
    │   ├── service_a
    │   │   ├── BUILD
    │   │   ├── Dockerfile
    │   │   ├── file_a1.txt
    │   │   ├── file_a2.txt
    │   │   └── file_a3.txt
    │   └── service_b
    │       ├── BUILD
    │       ├── Dockerfile
    │       └── file_b1.txt
    └── core
        └── service_c
            ├── BUILD
            ├── Dockerfile
            ├── file_c1.txt
            └── file_c2.txt
In this example we have three Dockerfiles that live inside a nested folder structure (in reality I have a bunch more but this simplified example should illustrate the point).
The various files
file_*.txt
represent files that need to be copied into the docker images via
COPY
instructions.
In order to avoid lengthy paths in those
COPY
instructions, I'd like to keep the
context_root
for each docker image in the folder where the respective Dockerfile lives. I.e., I'd like to be able to write something like
Copy code
COPY file_a1.txt file_a2.txt file_a3.txt .
instead of
Copy code
COPY services/base_images/service_a/file_a1.txt services/base_images/service_a/file_a2.txt services/base_images/service_a/file_a3.txt .
(Admittedly in this simple example we could use globs instead of listing all filenames explicitly, but in my real example there is no relation between the filenames.)
However, the complication is that the docker image for
service_b
is derived from the one for
service_a
, and similarly for
service_c
and
service_b
.
My understanding is that in order to tell pants about these dependencies, the BUILD files needs to look similar to this (with the context root at the toplevel of the repository).
Copy code
$ cat services/base_images/service_a/BUILD

docker_image(name="base-image-a")
Copy code
$ cat services/base_images/service_b/BUILD

docker_image(
  name="base-image-b",
  dependencies=[
    "services/base_images/service_a:base-image-a",
  ],
)
Is there any way to keep the context roots at subfolders containing the Dockerfiles while still being able to specify the dependencies between docker images?
Many thanks for reading this whole thread and for any suggestions!
(I'm also happy to provide a minimal example repo if it would be useful.)
To answer my own question: the approach described at https://www.pantsbuild.org/2.21/docs/docker#dependency-inference-support works as expected:
Copy code
# services/base_image/service_b/Dockerfile

ARG BASE_IMAGE=services/base_images/service_a:base-image-a
FROM $BASE_IMAGE
This works even when I declare
docker_image
targets with
context_root = "./"
. My confusion yesterday stemmed from the fact that in my initial attempts I had explicitly declared the docker image dependencies explicitly in the
docker_image
declaration, for example:
docker_image(name="base-image-b", dependencies=["services/base_images/service_a:base-image-a"])
. This approach requires the context root to be a common parent because now the dependency needs to be within the docker context. By contrast, when using the
ARG
approach described in the docs, the dependency is resolved by pants itself without Docker having to know about it. 👌