What is Docker?
A platform for packaging an application and its dependencies into a portable image and running it as an isolated container on any host.
Definition
Docker is an open-source platform for building, shipping, and running applications in containers. A container packages an application with everything it needs (runtime, system libraries, dependencies, configuration) so it behaves the same on a developer's laptop, a CI runner, and a production server. Docker, released in 2013, did not invent Linux containers, but it made them practical with a simple build format, a friendly CLI, and a public image registry, Docker Hub. What does Docker do under the hood? On Linux, a container is an ordinary process isolated with kernel features: namespaces give it its own view of process IDs, network interfaces, and filesystem mounts, and cgroups cap how much CPU and memory it can use. The Docker Engine (dockerd, with containerd and runc underneath) builds images, manages containers, and wires up networks and volumes. On macOS and Windows, Docker Desktop runs a lightweight Linux VM to host the same engine. Docker image vs container: an image is a read-only, layered template built from a Dockerfile, where each instruction such as RUN or COPY adds a cached layer. A container is a running (or stopped) instance of an image with a thin writable layer on top. One image can back many containers, much as one class can create many objects. Images are versioned with tags such as node:20-alpine and stored in registries like Docker Hub, GitHub Container Registry, or Amazon ECR. Docker vs virtual machine: a VM virtualises hardware and boots a full guest operating system with its own kernel on a hypervisor, so it needs gigabytes of disk and often tens of seconds to start. Containers share the host kernel and isolate only user space, so they start in about a second, are often tens of megabytes, and pack far more densely per host. The trade-off is weaker isolation: a kernel vulnerability can affect every container on the host, which is why untrusted multi-tenant workloads often run containers inside VMs or sandboxes such as gVisor or Firecracker. What is Docker used for? Reproducible development environments, consistent CI builds, packaging microservices, and shipping software to any server with a container runtime. In DevOps the image becomes the unit of delivery: the CI/CD pipeline builds it once, tests it, and promotes the same image to staging and production, often under an orchestrator such as Kubernetes.
When to Use Docker
Use Docker when you need the same environment across development, CI, and production, when several services with conflicting dependencies share hosts, or when your platform (Kubernetes, Cloud Run, ECS, Fly.io, or a self-hosted VPS with Compose) expects container images. It is also handy for running throwaway databases and tools locally without installing them. It adds little for a single static site or a serverless function the platform already packages for you. Choose a VM instead when you need a different kernel or operating system, strong isolation for untrusted code, or kernel-level tuning. Keep images small and reproducible: pin base image versions, use multi-stage builds so compilers stay out of the final image, run as a non-root user, and treat containers as disposable, with persistent data in volumes or managed databases.
Docker in Practice
- A Dockerfile for a Node.js API: FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --omit=dev COPY . . EXPOSE 3000 CMD ["node", "server.js"] Copying the package files before the source lets Docker reuse the cached dependency layer when only application code changes.
- Build the image, run a container from it, and inspect it: docker build -t my-api:1.0 . docker run -d --name api -p 3000:3000 -e NODE_ENV=production my-api:1.0 docker ps docker logs -f api The -p flag maps port 3000 on the host to port 3000 inside the container.
- Docker Compose (compose.yaml) for an app plus its database: services: web: build: . ports: ["3000:3000"] environment: DATABASE_URL: postgres://app:secret@db:5432/app depends_on: [db] db: image: postgres:16 environment: POSTGRES_USER: app POSTGRES_PASSWORD: secret volumes: ["pgdata:/var/lib/postgresql/data"] volumes: pgdata: Running docker compose up -d starts both on a shared network where web reaches the database at the hostname db.
- A new developer clones the repo and runs docker compose up instead of following a page of instructions to install the right versions of Postgres, Redis, and Python. The image that passes CI is the image that gets deployed, so 'works on my machine' bugs caused by mismatched library versions largely disappear.
Common Mistakes
- Treating a container like a VM: shelling in and patching it by hand. Changes vanish when the container is replaced; change the Dockerfile and rebuild instead.
- Keeping database files in the container's writable layer. The data is lost when the container is removed; use a named volume or an external database.
- Deploying the latest tag to production. It is a moving target, so two deploys of the 'same' image can differ; pin a version tag or digest.
- Baking secrets into images with ENV or COPY. Anyone who can pull the image can read every layer; inject secrets at runtime.
- Placing COPY . . before dependency installation in the Dockerfile, which invalidates the cache and reinstalls every dependency on each code change.