Ankit KanojiyaBeginner

Evergreen guide by Ankit Kanojiya · Beginner · DevOps & Cloud

Docker and Containers: A Practical Introduction

An introduction to containers and Docker, covering images, layers, and how to write a lean, reproducible Dockerfile. Explains the difference between containers and virtual machines.

Containers package an application together with its dependencies so it runs the same way on a laptop, a CI runner, and a production server. Docker is the most common toolchain for building and running them, and understanding its core model removes most of the confusion newcomers face.

Containers Versus Virtual Machines

A virtual machine virtualizes hardware and runs a full guest operating system, including its own kernel. A container virtualizes the operating system: it shares the host kernel and isolates processes using kernel features such as namespaces and cgroups. The practical consequences:

  • Containers start in milliseconds; VMs take seconds to minutes.
  • A container image is megabytes; a VM image is gigabytes.
  • VMs give stronger isolation because the kernel is not shared.

Containers are not a security boundary as strong as a VM. For hostile multi-tenant workloads, teams often run containers inside VMs to get both density and isolation.

Images and Layers

An image is a read-only template; a container is a running instance of that image. Images are built in layers, and each instruction in a Dockerfile produces one layer. Layers are cached and shared, which is why ordering instructions well matters for build speed.

FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Copying the dependency manifest and installing before copying source code means the expensive install layer is reused whenever only application code changes. Reversing those two steps would invalidate the cache on every edit.

Writing a Lean Image

Image size affects pull times, storage, and attack surface. Two techniques help the most.

First, choose a small base. A slim or alpine variant drops hundreds of megabytes compared to a full distribution image, though alpine uses musl libc, which can cause subtle incompatibilities with some native modules.

Second, use multi-stage builds to keep build tools out of the final image:

FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /bin/app .

FROM gcr.io/distroless/static
COPY --from=build /bin/app /app
ENTRYPOINT ["/app"]

The compiler and source never ship; only the compiled binary lands in a minimal runtime image.

Running Containers

Common day-to-day commands:

# Build an image and tag it
docker build -t myapp:1.0 .

# Run it, mapping a port and setting an env var
docker run -p 3000:3000 -e NODE_ENV=production myapp:1.0

# List running containers and view logs
docker ps
docker logs -f <container-id>

Data written inside a container disappears when the container is removed. For anything that must persist, mount a volume:

docker run -v pgdata:/var/lib/postgresql/data postgres:16

Good Practices and Trade-offs

  • Run as a non-root user. By default processes run as root inside the container, which is unnecessary risk. Add a user and switch to it.
  • One main process per container. Containers are designed around a single foreground process; cramming several services into one makes logging, scaling, and restarts harder.
  • Treat images as immutable. Rebuild and redeploy rather than patching a running container, so the running state always matches a known artifact.
  • Pin base image versions. Using a floating tag means rebuilds can pull a different base without warning, undermining reproducibility.

There is a trade-off between reproducibility and freshness. Pinning exact versions gives repeatable builds but requires deliberate updates to pick up security patches. A reasonable compromise is pinning the major and minor version while allowing patch updates, combined with regular image scanning.

Summary

Containers share the host kernel to deliver fast, lightweight, reproducible environments. Docker builds them as cached layers from a Dockerfile; ordering instructions well and using multi-stage builds keeps images small. Run a single non-root process per container, persist state in volumes, and treat images as immutable artifacts you rebuild rather than edit.