FROM: Base Image Selection
What you'll learn: How to choose and specify the right base image when building a Docker container using the FROM instruction.
Every Dockerfile begins with a FROM instruction—it's the foundation upon which your entire container image is built. Think of it like choosing the foundation for a house: you could start with bare concrete (minimal base images) or a pre-furnished starter home (full-featured OS images).
Understanding FROM Syntax
The FROM instruction follows this pattern:
FROM <image>:<tag>
FROM <image>@<digest>
FROM --platform=<platform> <image>:<tag>
For example:
FROM ubuntu:22.04
FROM python:3.11-slim
FROM alpine:latest
Choosing Your Base Image
Your base image choice depends on your application's needs:
- Full OS images (
ubuntu,debian) provide familiar environments but are larger - Language-specific images (
python:3.11,node:18) include runtime dependencies pre-installed - Minimal images (
alpine,scratch) reduce size and attack surface but may lack tools
Always specify explicit tags (like ubuntu:22.04) rather than latest to ensure reproducible builds.
Platform Specifications
When building for different architectures (ARM vs x86), use the --platform flag:
FROM --platform=linux/amd64 node:18-alpine
Multi-Stage Builds Setup
Multi-stage builds use multiple FROM instructions to create intermediate build environments, then copy only necessary artifacts to a final minimal image—dramatically reducing final image size.
FROM golang:1.21 AS builder
# ... build steps ...
FROM alpine:3.18
# ... copy only compiled binaries ...
Key Takeaway: The FROM instruction selects your base image foundation—choose wisely based on size, security, and compatibility needs, always use specific tags, and leverage multi-stage builds to optimize final image size.