I have an application that has two Dockerfile
, say Dockerfile.foo
and Dockerfile.bar
, which are expected to produce two different images for two different services of docker-compose.yml
.
For example, it may looks like:
# ...
services:
service1:
build:
context: .
dockerfile: Dockerfile.foo
service1:
build:
context: .
dockerfile: Dockerfile.bar
# ...
But both Dockerfile.{foo,bar}
share the same first half while their left parts differ.
How to avoid unnecessary duplicate builds and maintenance hassle by making .{foo,bar}
extend from a base Dockerfile.common
? In other words, is it possible to archive something like FROM Dockerfile.common
within docker-compose.yml
?
Update:
What I expect is using docker-compose
to automate the build process without manually (or with a separate build.sh
) running:
docker build -f Dockerfile.common -t common .
at first;docker build -f Dockerfile.foo -t foo . && docker build -f Dockerfile.bar -t bar .
so that Dockerfile.{foo, bar}
can properly refer to the common
image.If you want to avoid playing around with unnecessary intermediate tags and you use Docker 20.10+, you can also do this:
# syntax = edrevo/dockerfile-plus
INCLUDE+ Dockerfile.common
# The rest of Dockerfile.foo would go after the INCLUDE+ instruction
RUN echo "Hello World"
The INCLUDE+
instruction gets imported by the first line in the Dockerfile. You can read more about the dockerfile-plus at https://github.com/edrevo/dockerfile-plus
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With