I have two services in my docker-compose.yml file that are built from two different Dockerfiles, Dockerfile.foo and Dockerfile.bar. However, both Dockerfile.{foo, bar} share the same first half, and only differ in the latter parts.
Is it possible to build the two services using a single base Dockerfile.common to avoid unnecessary duplicate builds and maintenance?
I want to use docker-compose to automate the build process, where first docker build -f Dockerfile.common -t common . is executed, and then docker build -f Dockerfile.foo -t foo . && docker build -f Dockerfile.bar -t bar . so that Dockerfile.{foo, bar} can refer to the common image.
This will first build the common image using Dockerfile.common and tag it as common. Then, it will build the service1 and service2 images using the respective Dockerfile.foo and Dockerfile.bar, which both reference the common image.
Note: In your Dockerfile.foo and Dockerfile.bar, make sure to use the common image as the base image by specifying FROM common.