Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 115 of 8022. Docker Image LifecycleFree lesson

Selecting Minimal Base Images

Choose lightweight base images like Alpine or Distroless to significantly reduce the attack surface and download size of your containerized applications.

In this lesson, you will learn how selecting a lightweight base image reduces your container's security risks and improves deployment speed.

When you start a Dockerfile with FROM ubuntu or FROM node, you are often pulling in a full-featured operating system. This includes package managers, shell utilities, and system libraries that your application likely doesn't need to run. Every unnecessary file is a potential "attack surface"—a place where a hacker could find a tool to exploit your container. Furthermore, these large images take longer to download and consume more storage on your clusters.

To keep your images lean, you should choose minimal base images. Two popular industry standards are:

Alpine Linux

Alpine is a security-oriented, lightweight distribution. It replaces the heavy standard C library (glibc) with a smaller one (musl), resulting in a base image that is often under 5MB.

# Use Alpine to keep the image size tiny
FROM alpine:3.19
RUN apk add --no-cache python3

Distroless

Maintained by Google, "Distroless" images contain only your application and its runtime dependencies. They do not contain shells, package managers, or any other programs you would expect to find in a standard Linux distribution. If a hacker manages to get inside a Distroless container, they will find no tools to help them navigate or attack the system.

# A Distroless image contains nothing but your app
FROM gcr.io/distroless/static-debian12
COPY my-app /
CMD ["/my-app"]

Why it matters

By choosing a minimal base, you aren't just saving disk space. You are following the principle of least privilege. If a vulnerability is discovered in a tool you don't even have installed (like a rarely used curl or netcat version), your container is naturally immune to it. Additionally, smaller images move faster across the network during CI/CD pipeline steps, meaning your deployments happen in seconds rather than minutes.

Key Takeaway: Choosing minimal base images like Alpine or Distroless shrinks your attack surface and accelerates deployment by removing unnecessary system tools and libraries.