https://pantsbuild.org/ logo
#general
Title
# general
a

alert-dawn-51425

04/07/2023, 1:19 PM
Is there a way to have a base docker image, and build other docker images from that base? So, build a base python image with certain installations, then having the other docker images reference that image in the FROM command?
I noticed that this is something that can be done in other systems, like please: https://please.build/codelabs/k8s/#3. I would like to leverage something similar to avoid having to have each docker file repeat a bunch of things, and to also more strictly define the versions available for use.
h

high-yak-85899

04/07/2023, 2:06 PM
You could achieve this with a macro where you specify the docker instructions in the target instead of a file.
r

refined-addition-53644

04/07/2023, 2:14 PM
You can have multiple
docker_image
target with dependency. You can see the last example in this tutorial https://blog.pantsbuild.org/optimizing-python-docker-deploys-using-pants/
c

curved-television-6568

04/08/2023, 12:54 PM
still on my todo to add this to the documentation, but you can also have pants replace a target address in your FROM instruction. Example:
Copy code
docker_image(name="base", ...)  # this is src/images:base in this example 

docker_image(
  name="service",
  instructions=[
    "ARG BASE=src/images:base", 
    "FROM $BASE",
    ...
  ],
  ...
)
Only real world example I have right now is from the tests: https://github.com/pantsbuild/pants/blob/690e6a60ba244cc46dd191885ea7fa54a3e4554d/[…]src/python/pants/backend/docker/util_rules/dependencies_test.py
3 Views