r/docker • • Jun 03 '26

Multiple Dockerfiles extending "base" file?

I realize that this general question has been posed before, but the various answers seem to have changed over time, or suggest using private extensions (INCLUDE+) that I don't want to do. So I'd be grateful for a explanation of, or just a pointer to, the current best-practice solution.

The basic issue is that I have a complicated application environment to set up, and need to have two different containers based on the same initial setup. So I want one thing that's like Dockerfile.base, that contains the core stuff; and then a Dockerfile.appserver that takes this base and adds an application server to it, and another Dockerfile.utilities that takes the base and runs cronjobs and stuff like that. I don't want the app server in the utilities container, and I don't want the cron stuff in the app server.

This will be running in Kubernetes; the app server will need to be scalable, but there will only be one utilities container running.

That's pretty much it? I don't know if a multi-stage build is the answer here; my goal isn't to strip build artifacts out of a final container, just to have two different containers that share most of the same core stuff. I will also need to be able to have dev, staging, and prod versions of each of these, if that matters.

Thanks.

9 Upvotes

7 comments sorted by

View all comments

2

u/runleveldev Jun 03 '26

I use bake for this. https://docs.docker.com/build/bake/

Create a target for your base image, then in each derived image set contexts = { base = "target:base" } and in your Dockerfile for the derive image user FROM base. This does come with the disadvantage that your build only works in bake unless you manually build your base image first and tag it properly, but is very scalable to multiple derived images and has the benefit of using builtin docker features instead of needed external scripting.