liveBook · Manning

Base image

As explained in Learn Docker in a Month of Lunches, Second Edition

A base image is an existing container image that supplies the starting filesystem and layers for building another image.

Need to package an app without installing its runtime on your machine? Start the Dockerfile with `FROM` and an existing image. That base image supplies the starting layers; later instructions add your application. Images built from the same base can share its stored layers, so the sizes shown by `docker image ls` should not simply be added together to estimate disk use. section 3.4 section 4.2

Choose a base for the job each build stage performs:

  • For a build stage, use an image with the compiler or packaging tools. For the final stage, use one with what the app needs to run. A Go build can use `diamol/golang:2e` for its tools and `diamol/base:2e` for the compiled server. section 4.4
  • Prefer the smallest compatible runtime and OS variant, then test the app. Extra tools increase image size and can give an attacker more to use if the container is compromised. A JRE may be a better fit than a JDK for a Java app that only needs to run. section 17.2
  • For Windows apps, choose an image that supplies the needed dependencies without unnecessary ones. An installation stage can use Windows Server Core, then copy the installed output into a Nano Server stage; test the Windows version you select. section 13.2
  • If you may need more platforms later, start with a multi-platform base image and avoid OS-specific `RUN` and `CMD` instructions. section 12.5

Build with tools; run without them

A two-stage Go Dockerfile begins with `FROM diamol/golang:2e AS builder`, compiles the server, then starts a second stage with `FROM diamol/base:2e`. `COPY --from=builder /server .` brings the binary into the application image. Build it with `docker image build -t image-gallery .`; compare image sizes with `docker image ls`. The example application image is 35.1 MB, compared with 230 MB for the Go toolset image. section 4.4

Base-image choice is an ongoing optimization, not a one-time size contest. Check compatibility before adopting a smaller variant, and monitor build and push/pull times as images change. Teams can maintain a curated set of base images for repeated use. section 17.2 section 17.5

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Works through base selection in multi-stage builds and compares variants, but does not detail how to create a base image from scratch.
Examples
Uses Go builder and runtime images, Java JDK/JRE size comparisons, and Windows Server Core and Nano Server stages.
What it assumes
Assumes basic Dockerfile use but requires no locally installed Java tools for the Java build example.
What you can do after
Apply it via Dockerfiles by selecting and testing build-stage and runtime-stage images for compatibility, size, and security.
How it connects
Connects Dockerfile `FROM`, image layers, multi-stage builds, runtime dependencies, attack surface, and multi-platform images.

Also explained in

Compare how 7 books explain base image →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.