Getting Started with Docker: A Beginner's Guide
Containerization has revolutionized the way we develop, ship, and deploy applications. Before Docker, developers frequently suffered from the "it works on my machine" syndrome. By bundling application code, runtimes, system tools, and libraries into a single lightweight package, Docker guarantees that applications execute identically across dev, test, and production environments.
In this guide, we will break down containerization fundamentals and write a production-ready setup from scratch.
Virtual Machines vs. Containers
To appreciate Docker, we must first compare it to Traditional Virtualization (VMs). Virtual machines run on a hypervisor, which requires a complete copy of a Guest OS inside each VM. This incurs heavy CPU, memory, and disk space overhead.
Containers, on the other hand, run directly on the Host Operating System and share its kernel. They use Linux namespaces and cgroups to isolate process threads, resulting in fast start-up times (seconds vs minutes) and resource footprint sizes of megabytes instead of gigabytes.
Writing Your First Dockerfile
A Dockerfile is a text document containing all the commands a user could call on the command line to assemble an image. Here is an optimized, multi-stage build Dockerfile for a Node.js application:
# Stage 1: Build Dependencies
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 2: Runtime Production
FROM node:18-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --only=production
COPY --from=builder /app/dist ./dist
EXPOSE 3000
USER node
CMD ["node", "dist/index.js"]
Command Summary
Use these primary CLI utilities to build, run, and manage containers:
- Build an Image:
docker build -t my-app:1.0 . - Run Container:
docker run -p 3000:3000 --name web-app -d my-app:1.0 - Inspect Logs:
docker logs -f web-app - List Containers:
docker ps -a - Stop Container:
docker stop web-app
Containerization is the bedrock of modern DevOps engineering. Once you master individual containers, the next step is orchestration using Docker Compose or Kubernetes.
Written by Swapnil
DevOps & Infrastructure Engineer, focusing on container scaling, secure automated deployment pipelines, and cloud state architecture.