Skip to main content
Account
Tutorial

Dockerfile Tip 1: Define a Base Layer

David Flanagan May 03, 2019 1 min read

With the advent of the Multi Stage Dockerfile, it’s very common for people to very specific layers that do a single task; which often utilise the same “primary” base layer.

Example:

FROM elixir:1.8 AS deps
RUN mix deps.get
FROM elixir:1.8 AS compile
COPY --from=deps ...
RUN mix compile

This allows lengthy tasks to utilise the build cache and pass artefacts between layers, without rerunning the lengthy task. However, I often see this “primary” layer duplicated 4/6/10 times in a single Dockerfile; making maintenance cumbersome.

The Tip

Create an empty layer with your primary image:

FROM elixir:1.8 AS base
FROM base AS deps
# ...
FROM base AS compile
# ...

This reduces duplication and makes maintenance, and pull-requests, easier to review/manage/change.

Until next time :metal:

Published May 03, 2019

Related Articles

Related Videos

Rawkode Academy newsletter

Academy publications by email

Get an email when Rawkode Academy publishes new tutorials, articles, and videos.

Loading email signup. If it does not load, manage email preferences in account settings.