Docs / Containers & Docker / Docker Image Optimization Tips

Docker Image Optimization Tips

By Admin · Feb 25, 2026 · Updated Apr 23, 2026 · 31 views · 1 min read

Use Alpine Base Images

# Instead of (900+ MB)
FROM ubuntu:22.04

# Use (5 MB)
FROM alpine:3.19

# Or language-specific slim variants
FROM node:20-alpine     # ~130 MB vs ~1 GB
FROM python:3.12-slim   # ~150 MB vs ~900 MB
FROM php:8.2-fpm-alpine # ~80 MB vs ~500 MB

Minimize Layers

# BAD — each RUN creates a layer
RUN apt update
RUN apt install -y curl
RUN apt install -y git
RUN apt clean

# GOOD — single layer
RUN apt update && \
    apt install -y curl git && \
    apt clean && \
    rm -rf /var/lib/apt/lists/*

Order Layers by Change Frequency

# Dependencies change rarely — cache this layer
COPY package.json package-lock.json ./
RUN npm ci --production

# Application code changes often — this layer rebuilds
COPY . .

Use .dockerignore

# .dockerignore
node_modules
.git
*.md
.env
tests
docker-compose.yml

Check Image Size

# View image sizes
docker images

# Analyze layers
docker history myimage:latest

# Use dive for interactive layer inspection
dive myimage:latest

Was this article helpful?