0PricingLogin
Linux Command Line & Bash Scripting Mastery · Lesson

Writing Lean Dockerfiles and Shell Entrypoints

Author multi-stage build scripts and robust entrypoint shims with signal handling and config templating.

Why Lean Dockerfiles Matter for DevOps

In production environments, every megabyte in a Docker image has a cost: slower pulls, larger attack surfaces, and wasted registry storage. Lean Dockerfiles combined with robust shell entrypoints are a hallmark of mature DevOps practice.

  • Multi-stage builds separate build-time tooling from the final runtime image, dramatically reducing size.
  • Entrypoint shims are small shell scripts that bootstrap containers: they template config files, validate environment variables, handle signals, and finally exec the main process.
  • Together they form the backbone of reliable, portable container workloads in Kubernetes, ECS, and bare-metal environments.

This lesson covers both disciplines end-to-end, with production-grade patterns you can drop directly into your pipelines.

Anatomy of a Multi-Stage Dockerfile

A multi-stage Dockerfile uses multiple FROM instructions. Each stage is an isolated layer set; you copy only the artifacts you need into the next stage.

  • Stage 0 (builder): installs compilers, test runners, build dependencies.
  • Stage 1 (runtime): starts from a minimal base (e.g. alpine, distroless) and copies only compiled binaries or app bundles.
  • The final image never contains gcc, make, or source code unless you explicitly copy them.

Use --from=<stage> in COPY to pull files across stage boundaries. Name stages with AS <name> for readability and selective targeting with docker build --target.

# ---- Stage 0: builder ----
FROM golang:1.22-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags='-s -w' -o /app/server ./cmd/server

# ---- Stage 1: runtime ----
FROM gcr.io/distroless/static-debian12
COPY --from=builder /app/server /server
ENTRYPOINT ["/server"]

All lessons in this course

  1. Writing Lean Dockerfiles and Shell Entrypoints
  2. Templating Configs with envsubst and heredocs
  3. Scripting Cloud Resources via CLI and jq
  4. Health Probes, Readiness Gates, and Wait Loops
← Back to Linux Command Line & Bash Scripting Mastery