Dynamic FROM base images


Context

There are situations where you want to test different versions of languages, frameworks or tools. Using docker, we can produce docker images that only differ on their base image (which should point to the language, framework, etc. that we want to compare).

Implementation

Let’s look an imaginary ruby Dockerfile and how we can build images for different ruby versions:

ARG VERSION=2.7.2

FROM ruby:${VERSION}

WORKDIR /app

COPY . .

RUN bundle install

CMD ["bundle", "exec", "ruby", "entrypoint.rb"]

Then, we can build different docker images as follows:

# Defaults to build image with version 2.7.2
$ docker build -t my-image:2.7.2 .

# Using a different ruby version through the VERSION argument
$ docker build -t my-image:3.0.0 --build-arg VERSION="3.0.0" .

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.