early-twilight-26336
06/28/2024, 3:15 AMdocker_image
I was a bit surprised that I couldn't access a Dockerfile
outside of the BUILD
directory or children. It makes sense with how Pants works in general, but if you compare to Python where source files are a first-class target, it's a bit different. Would it make sense to have a dockerfile_source
target that can get referenced in the docker_image
source
field, which I believe would allow for the kind of access I was looking for?curved-television-6568
06/29/2024, 6:37 AMsource
field? If so, you can't do that with python sources either..
You can't reference other targets in the source
field, so a dockerfile_source
target wouldn't help. I guess you might've thought of using the dependencies
field for this, which would work, but the better approach is to simply move your docker_image
target up in the source tree instead, as the location of the Dockerfile
affects the scope of your docker build context, there's a can of worms lurking if we support pointing to them arbitrarily.early-twilight-26336
07/02/2024, 1:27 PMdependencies
field on `docker_image`to point at a target created with dockerfile_source
. So something like this:
/docker/service/Dockerfile
/docker/service/BUILD <- has `dockerfile_source(name="image", source="Dockerfile")
/service-a/BUILD <- has `docker_image(dependencies=["//docker/service:image", ":bin"])`
/service-b/BUILD <- same docker_image, different `:bin` target
but yeah, I'm somewhat unaware of the problems that may cause. Maybe it wouldn't work.curved-television-6568
07/02/2024, 1:42 PMDockerfile
on disk, you can put the contents in a macro file:
# prelude.py
DOCKER_INSTRUCTIONS = """
FROM foo
COPY bar baz
...
""".split("\n")
then in any BUILD file:
# /service-a/BUILD
docker_image(instructions=DOCKER_INSTRUCTIONS, ...)
that way you can share the same Dockerfile instructions between multiple docker image targets in different parts of the repo.
The other option is to share a common base image, and get a "kind of" multi stage build:
# /docker/service/BUILD
docker_image(name="common", ...)
# /service-a/Dockerfile
ARG base=docker/service:common
FROM $base
COPY svc-a /specifics/
Pants will pick up on your dependency to the common base image, and build it first, then build service-a image where the common base image will be used in the FROM instruction..early-twilight-26336
07/02/2024, 1:57 PM