Docker Explained: Containerization, Namespaces, Cgroups & Multi-Stage Builds
Comprehensive Docker guide: kernel namespaces, cgroups, layered image filesystems, writing optimized multi-stage Dockerfiles for Java and Node.
# Multi-stage Dockerfile for Spring Boot Application
# Stage 1: Build JAR using Maven
FROM maven:3.9-eclipse-temurin-21-alpine AS builder
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn clean package -DskipTests
# Stage 2: Minimal Production JRE Image
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar
EXPOSE 8080
USER 1000:1000
ENTRYPOINT ["java", "-jar", "app.jar"]
1. Containers vs. Virtual Machines
A Virtual Machine runs a full guest operating system on top of a hypervisor, consuming gigabytes of memory and requiring minutes to boot. Docker containers share the host Linux kernel, isolating processes using two fundamental Linux kernel features:
- Namespaces: Isolate view of system resources (PID, NET, MNT, IPC, UTS).
- Control Groups (cgroups): Meter and limit hardware consumption (CPU, RAM, I/O bandwidth).
2. The Power of Multi-Stage Builds
Multi-stage builds allow developers to compile source code in a heavy build environment with compilers and SDKs, then copy only the compiled binary into a lightweight production runtime image, reducing image size from 800MB to under 120MB.
- Docker images use immutable, cached layers; order Dockerfile steps from least-frequently changed to most-frequently changed.
- Never run container processes as root; declare a dedicated non-root USER.
- Multi-stage builds dramatically shrink attack surface and download times.
Frequently Asked Questions
What is the difference between CMD and ENTRYPOINT?
Gain deep mastery over these concepts with direct instructor guidance, code reviews, and mock interviews at C-60 R.K Tower, 3rd Floor, RDC, Ghaziabad.
Explore Related Course Call 7503962162