How to dynamically change the docker's base image

From Docker version 17.05, you can do something like this:

ARG MYAPP_IMAGE=myorg/myapp:latest
FROM $MYAPP_IMAGE

You can provide MYAPP_IMAGE as a command line paramether:

docker build -t container_tag --build-arg MYAPP_IMAGE=localimage:latest .

More info here: https://www.jeffgeerling.com/blog/2017/use-arg-dockerfile-dynamic-image-specification


You can have multiple FROM statements in a Dockerfile. Like for example:

FROM ubuntu:latest
FROM centos:latest
COPY config /tmp/config
EXPOSE 9080 8080

will create two images. From the Docker reference docs:

FROM can appear multiple times within a single Dockerfile in order to create multiple images.


You could make use of image tagging to handle this.

Change the FROM to something like:

FROM base-image

Then just tag RHEL or Centos "base-image" before you do the build (using -f)

$ docker tag -f centos base-image
$ docker build -t my_image/builtfromcentos .
$ docker tag -f rhel base-image
$ docker build -t my_image/builtfromrhel .