astonishing-dog-77654
05/30/2024, 9:30 PMastonishing-dog-77654
05/30/2024, 9:31 PM.
├── 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
astonishing-dog-77654
05/30/2024, 9:33 PMastonishing-dog-77654
05/30/2024, 9:34 PMfile_*.txt
represent files that need to be copied into the docker images via COPY
instructions.astonishing-dog-77654
05/30/2024, 9:38 PMCOPY
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 file_a1.txt file_a2.txt file_a3.txt .
instead of
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.)astonishing-dog-77654
05/30/2024, 9:40 PMservice_b
is derived from the one for service_a
, and similarly for service_c
and service_b
.astonishing-dog-77654
05/30/2024, 9:46 PM$ cat services/base_images/service_a/BUILD
docker_image(name="base-image-a")
$ cat services/base_images/service_b/BUILD
docker_image(
name="base-image-b",
dependencies=[
"services/base_images/service_a:base-image-a",
],
)
astonishing-dog-77654
05/30/2024, 9:47 PMastonishing-dog-77654
05/30/2024, 9:47 PMastonishing-dog-77654
05/30/2024, 9:48 PMastonishing-dog-77654
05/31/2024, 6:27 PM# 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. 👌