Shared build logic with docker-compose and multi-stage Dockerfiles


April 2019


432 time


I am using docker-compose with multi-stage Dockerfiles to build and run multiple services. This works, but the "build" portion of each multi-stage build is largely copy-and-pasted between each service's Dockerfile. I want to reduce the copy-and-paste / centralize the common build logic in one spot.

Reading I could create a local image with the shared build steps and have the service Docker files depend on it, but I want the development experience to be a simple docker-compose up. Creating a local build image means a developer would have to know to run docker build [common_build_image] first so that the build image exists locally and THEN run docker compose up to build and run all the services that depend on it.

There doesn't appear to be a way to include a Dockerfile into another Dockerfile. FROM does not appear to support local paths.

Is there a way to accomplish what I want? Of course I can use a shell script to tie everything together, but that is basically what multi-stage builds was trying to solve in the first place.

1 answers


It turns out you can "compose" docker-compose: which is what I was looking for.