Docker Explained for Beginners 2026 - Containers, Images & Docker vs VMs
A beginner's guide to Docker: what containers are, how images and Dockerfiles work, how Docker differs from virtual machines, and how DevOps teams use Docker in CI/CD and Kubernetes.
Docker turned "it works on my machine" into a solved problem. It packages an application with everything it needs to run into a single portable unit called a container.
This page covers building and running containers. Running many of them across a fleet of machines is a different job - see Kubernetes explained simply for that half.
What is Docker?
Docker packages applications with all their dependencies - libraries, runtime and config - into containers that run identically on any machine, from your laptop to a production cloud server.
Why Docker matters
It eliminates environment mismatches, makes deployments repeatable, starts in seconds, and is the foundation that CI/CD pipelines and Kubernetes build on.
Key concepts
- Image: a read-only template (your app plus dependencies).
- Container: a running instance of an image.
- Dockerfile: the recipe that defines how an image is built.
- Registry: where images are stored and shared (e.g. Docker Hub).
The relationship worth fixing in your head early: a Dockerfile builds an image, an image runs as a container. Editing a running container changes nothing about the image, which is why "I fixed it inside the container" is never a fix.
A real Dockerfile
This is a production-shaped Dockerfile for a Node service. It is a multi-stage build, which is the single biggest difference between a beginner image and a professional one.
FROM node:24-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:24-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
Three things there are doing real work:
- Copying
package*.jsonbefore the rest of the source. Docker caches each instruction as a layer and reuses it while its inputs are unchanged. Dependencies change far less often than your code, so this ordering means a one-line code edit does not reinstall every package. Get this wrong and every build reinstalls from scratch. - Two
FROMstages. Build tools, dev dependencies and source code stay in the first stage. Only the compiled output is copied into the final image, which is typically several times smaller and has a much smaller attack surface. USER node. Containers run as root by default. There is rarely a reason for that, and a container escape from a root process is considerably worse than from an unprivileged one.
The .dockerignore file nobody writes
Without one, COPY . . ships your node_modules, your .git directory and any local .env file into the image. That makes builds slow and can leak secrets into a layer that anyone who pulls the image can read. A minimal one:
node_modules
.git
.env
*.log
dist
The commands you will actually use
| Command | What it does |
|---|---|
docker build -t myapp:1.2.0 . | Build an image from the Dockerfile in this directory and tag it. |
docker run -p 3000:3000 myapp:1.2.0 | Run it, mapping host port 3000 to container port 3000. |
docker ps / docker ps -a | List running containers, or all of them including stopped ones. |
docker logs -f <id> | Follow a container's output. First stop when something will not start. |
docker exec -it <id> sh | Get a shell inside a running container to look around. |
docker system prune -a | Reclaim disk. Images and layers accumulate quickly and fill drives. |
Choosing a base image
The FROM line decides most of your image size and most of what a vulnerability scanner will complain about, so it deserves more thought than it usually gets.
| Base | What is in it | Trade-off |
|---|---|---|
node:24 (full Debian) | Compilers, a package manager and plenty of tools | Easiest to debug and the most for a scanner to flag. Fine for a build stage, heavy for a final image. |
node:24-slim | Debian with most extras stripped out | A sensible default for the final stage. It uses glibc, so native modules behave the way they do on most servers. |
node:24-alpine | A very small image built on musl libc instead of glibc | Small and quick to pull, but some native modules and prebuilt binaries misbehave on musl. Test before committing to it. |
Distroless or scratch | Your app and its runtime; no shell, no package manager | The smallest attack surface, but there is nothing to exec into when something breaks. Suits teams with good logging. |
Whichever you pick, pin it. node:24-alpine today and node:24-alpine next month are different images. Teams that need fully reproducible builds pin by digest (node:24-alpine@sha256:...) and let Dependabot or Renovate open a pull request when a new image is published.
Running several containers locally with Compose
Most real applications are more than one container: the app, a database, perhaps a cache. Docker Compose describes them in a single file, so a new teammate can start the whole stack with one command.
services:
web:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:app@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
retries: 10
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:
docker compose up builds and starts both containers; docker compose down stops them, and the named volume keeps the database between runs. The password in that file is fine for a laptop and nowhere else. Two details matter. The app reaches the database at the hostname db, the service name, not localhost, because each container has its own network namespace. And depends_on with a health condition waits until Postgres accepts connections, not merely until its container starts; without it, the app's first connection fails on every cold start.
Compose is for local development and small deployments, not a replacement for Kubernetes in production. A working Compose file is still a good first step towards Kubernetes manifests, because it forces you to write down every service, port, variable and volume the application needs.
Docker vs virtual machines
A virtual machine virtualises an entire operating system - it is heavy (gigabytes) and slow to boot. A container shares the host OS kernel and only packages the app and its dependencies - it is lightweight (megabytes) and starts in seconds. You can run far more containers than VMs on the same hardware, which is why containers won for modern app deployment. VMs still matter when you need strong isolation or a different operating system.
That shared kernel is also the security trade-off, and it is worth being able to say so in an interview: containers isolate processes, not kernels. Two VMs on one host are separated far more strongly than two containers are.
Mistakes that show up in every first Dockerfile
- Tagging everything
latest. You then cannot tell which build is running in production, and rollback becomes guesswork. Tag with a version or the commit SHA. - Passing secrets as build arguments. Build args are visible in the image history. Inject secrets at run time as environment variables, or use a secrets manager.
- Installing debug tools "just in case". Every extra package is more to patch and more for a scanner to flag.
- Running as root because the tutorial did.
- Treating a container as a small server - SSHing in, editing files, restarting things by hand. Containers are meant to be replaced, not repaired.
Error messages you will meet early, and what they mean
Most Docker errors name their cause once you know how to read them. These are the ones beginners run into first.
| You see | What it means | What to do |
|---|---|---|
Cannot connect to the Docker daemon ... Is the docker daemon running? | The CLI works, but the engine behind it is not running. | Start Docker Desktop, or run sudo systemctl start docker on Linux. |
permission denied while trying to connect to the Docker daemon socket | On Linux, your user is not in the docker group. | sudo usermod -aG docker $USER, then log out and back in. Docker's post-install guide warns that the group grants root-level privileges, so think twice on shared servers. |
port is already allocated | Another process, often an earlier container of yours, holds the left-hand port in -p. | Find it with docker ps, or choose another host port: -p 3001:3000. |
exec format error as the container starts | The image was built for another CPU architecture, typically on an ARM laptop for an x86 server. | Build with docker build --platform linux/amd64, or run a multi-platform build. |
"/package.json": not found on a COPY line | The file is outside the build context: the wrong directory was passed to docker build, or .dockerignore excludes it. | COPY paths are relative to the context (the last argument), not to the Dockerfile. |
toomanyrequests: You have reached your pull rate limit | Docker Hub allows 100 anonymous pulls per 6 hours per IP address, and a whole office network often shares one address. | docker login with a free account raises it to 200; at work, use the company registry mirror. |
docker-compose: command not found | The tutorial was written for Compose V1, which stopped getting updates in July 2023. | Type docker compose, with a space. If it warns that version is obsolete, delete that line. |
One failure prints nothing: the container exits at once and docker ps is empty. docker ps -a shows the exit code, and docker logs usually shows why. Code 137 means the process was killed, often for running out of memory.
How DevOps uses Docker
Docker images are built in CI/CD pipelines, pushed to a registry, and deployed - usually orchestrated by Kubernetes - to run reliably at scale.
In a normal pipeline the image is built once, tagged with the commit that produced it, scanned for vulnerabilities, pushed to a registry, and then that exact tag is deployed to staging and later to production. Building separately for each environment defeats the point: the artifact you tested must be the artifact you ship.
Related Learning Resources
Kubernetes Explained Simply 2026 - Orchestration, Networking & K8s vs Docker Swarm
Kubernetes for beginners: what it is, core concepts (pods, deployments, services, clusters), how Kubernetes networking works, and how K8s compares to Docker Swarm.
Terraform & Infrastructure as Code Explained 2026 - IaC, Terraform vs CloudFormation & Config Management
A beginner's guide to Infrastructure as Code with Terraform: what IaC is and why it matters, how Terraform works across clouds, Terraform vs CloudFormation, and how configuration management fits in.
GitOps Explained: ArgoCD, Flux and Progressive Delivery
How the reconciliation loop actually works, how Argo CD and Flux differ once you live with them, what canary and blue-green add on top, and the secrets and drift problems tutorials skip.
Ready to Start Your DevOps Career?
Join our comprehensive DevOps + GenAI course with hands-on projects, live mentorship, and placement support