CodeBlanks Logo Icon CodeBlanks

The FROM instruction

Learn to specify a base image with FROM.

The FROM instruction

FROM chooses the base image the image is built on. The choice directly affects the image size and security:

  • python:3.12: Debian based, full (~1 GB)
  • python:3.12-slim: unnecessary packages removed (~150 MB)
  • python:3.12-alpine: based on Alpine Linux (~50 MB); can cause trouble with some packages because of musl libc
FROM python:3.12-slim

Pinning the version

Writing FROM node can bring a different Node version on every build. Pin the version; to be even more exact you can use the image digest:

FROM node:20-alpine
FROM node:20.18.0-alpine3.20
FROM node@sha256:3f2a…     # immutable: exactly the same image

FROM scratch is a completely empty image; it is used for statically compiled Go programs.

Practice Choosing a base image

Write the first two lines of a Dockerfile for a Node.js application: base image node:20-alpine, working directory /app.

Starter code

terminal
# buraya yaz

Sample solution

terminal
FROM node:20-alpine
WORKDIR /app

Expected output

[+] Building 4.2s (8/8) FINISHED
 => naming to docker.io/library/api:latest done

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What benefit usually comes with a small image?

  • A faster CPU
  • More memory
  • Automatic backups
  • Faster downloads and less security attack surface Correct answer

Explanation: A package not in the image has no vulnerabilities either.

2 Choose a lightweight Python base.

Question
FROM python:3.12-____
Answer
FROM python:3.12-slim

Explanation: The -slim tag is the Debian variant with unnecessary packages removed.

3 Match the base images.

Term Meaning
python:3.12 Full Debian, ~1 GB
python:3.12-slim Trimmed, ~150 MB
python:3.12-alpine Alpine, ~50 MB
scratch Empty image

All lessons in Beginner