Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Dockerfile FROM another Dockerfile?

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:

  1. docker build -f Dockerfile.common -t common . at first;
  2. and then 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.
like image 802
Dummmy Avatar asked Sep 09 '25 21:09

Dummmy


1 Answers

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

like image 139
edrevo Avatar answered Sep 13 '25 04:09

edrevo