Container Image Types and Sizes

What are Container Image Variants?

When selecting a Docker image, developers encounter multiple variants with names like slim, alpine, full, or version-specific tags. These variants represent different base images with varying sizes, dependencies, and capabilities. Understanding these differences is crucial for creating efficient, production-ready containers.

Container image size directly impacts:


Image Size Comparison

graph LR A["Alpine
5-50 MB"] -->B["Slim
100-200 MB"] B -->C["Standard/Full
500 MB - 2+ GB"] style A fill:#90EE90 style B fill:#FFD700 style C fill:#FF6B6B

Base Image Types

Alpine

The smallest and most lightweight option. Alpine Linux is a minimal Linux distribution containing only essential components.

Characteristics:

Example - Python Alpine:

# Alpine variant FROM python:3.9-alpine WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "app.py"]

Image size: ~100 MB vs ~900 MB for standard Python image.

Slim

A middle-ground option that removes non-essential packages while keeping common development tools.

Characteristics:

Example - Node.js Slim:

FROM node:18-slim WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "index.js"]

Image size: ~150 MB vs ~900 MB for standard Node.js.

Standard/Full

Complete images with development tools, debuggers, and additional libraries pre-installed.

Characteristics:

Example - Node.js Standard:

FROM node:18 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "index.js"]

Image size: ~900 MB (includes development tools).


Linux-Based vs Specialized Containers

Linux-Based Images (Ubuntu, Debian, Alpine)

Standard Linux distributions used as base images.

# Ubuntu-based (larger, full-featured) FROM ubuntu:22.04 RUN apt-get update && apt-get install -y python3 python3-pip # Debian-based (smaller than Ubuntu) FROM debian:bullseye-slim RUN apt-get update && apt-get install -y python3 python3-pip # Alpine-based (smallest) FROM alpine:latest RUN apk add --no-cache python3

Size comparison:

.NET Container Images

Microsoft provides optimized .NET images with different variants.

Runtime Image (Production - contains only runtime):

FROM mcr.microsoft.com/dotnet/runtime:7.0 WORKDIR /app COPY --from=builder /app/publish . ENTRYPOINT ["dotnet", "MyApp.dll"]

Size: ~100-200 MB (contains only .NET runtime).

ASP.NET Image (For web applications):

FROM mcr.microsoft.com/dotnet/aspnet:7.0 WORKDIR /app COPY --from=builder /app/publish . EXPOSE 80 ENTRYPOINT ["dotnet", "MyWebApp.dll"]

Size: ~200-300 MB (includes ASP.NET runtime and web hosting).

SDK Image (Development - includes build tools):

FROM mcr.microsoft.com/dotnet/sdk:7.0 WORKDIR /app COPY . . RUN dotnet publish -c Release -o publish FROM mcr.microsoft.com/dotnet/aspnet:7.0 COPY --from=builder /app/publish . ENTRYPOINT ["dotnet", "MyApp.dll"]

Size: ~2+ GB (includes compiler, build tools, debuggers).


Multi-Stage Builds: Optimizing Image Size

Multi-stage builds use one image for compilation and another (smaller) image for runtime.

# Stage 1: Build (large image with build tools) FROM mcr.microsoft.com/dotnet/sdk:7.0 AS builder WORKDIR /src COPY ["MyApp/MyApp.csproj", "MyApp/"] RUN dotnet restore "MyApp/MyApp.csproj" COPY . . RUN dotnet publish -c Release -o /app/publish # Stage 2: Runtime (small image, only runtime needed) FROM mcr.microsoft.com/dotnet/runtime:7.0 WORKDIR /app COPY --from=builder /app/publish . ENTRYPOINT ["dotnet", "MyApp.dll"]

Result:


Practical Comparison: Multi-Purpose Application

Consider a Python web application with specific dependencies:

graph TD A["Python Application
Decision Matrix"] -->B{What is
primary use?} B -->|Production| C["Use Alpine/Slim
50-200 MB"] B -->|Development| D["Use Standard
500+ MB"] B -->|CI/CD| E["Use Slim
100-300 MB"] C -->C1["Minimal overhead
Fast deployment"] D -->D1["Full debugging tools
Slower startup"] E -->E1["Balance of both
Reasonable size"] style C fill:#90EE90 style D fill:#FF6B6B style E fill:#FFD700

Image Selection Guidelines

Use Case Recommended Size Reasoning
Production microservices Alpine/Slim 50-200 MB Fast deployment, minimal attack surface
Production monolith Slim 150-300 MB Still small, more tools available
Development environment Standard 500+ MB Full tooling for debugging and development
CI/CD build pipeline Slim 100-300 MB Quick builds, sufficient for compilation
High-security apps Alpine 5-50 MB Minimal dependencies reduce vulnerabilities

Common Pitfalls

Always test image variants thoroughly before production deployment.


Key Takeaways

Next Steps: Evaluate current container images using docker image ls --format "table {{.Repository}}\t{{.Size}}" to identify optimization opportunities, then refactor Dockerfiles using multi-stage builds for production applications.