The Problem Docker Solves

Before Docker, deploying an application meant manually installing the right version of Node.js (or Python, Java, PHP) on each server, configuring environment variables, installing libraries, and hoping nothing conflicted with other applications on the same machine. If your app worked locally but not in production, debugging was painful.

Docker packages your app, its runtime, and all dependencies into a container — a portable, isolated environment that runs identically on any machine with Docker installed.

Docker vs Virtual Machines

FeatureDocker ContainerVirtual Machine
OSShares host OS kernelFull guest OS (Windows, Linux)
SizeMegabytes (10–500 MB)Gigabytes (3–20 GB)
Start timeSeconds (sub-second common)Minutes
IsolationProcess-level (namespace/cgroups)Full hardware virtualisation
PerformanceNear-native (minimal overhead)5–15% overhead from hypervisor
PortabilityRun anywhere Docker is installedTied to hypervisor (VMware, HyperV)
Best forApp packaging, microservices, CI/CDFull OS isolation, different kernels

Core Docker Concepts

Image

A read-only template built from a Dockerfile. Contains the OS base layer, runtime, dependencies, and your application code. Images are immutable — to change an image, you rebuild it. Images are stored in registries (Docker Hub, AWS ECR, GitHub Container Registry).

Container

A running instance of an image. Containers are isolated from each other and from the host, but they share the host OS kernel. You can run thousands of containers on a single host. A container has its own filesystem, network interface, and process space.

Dockerfile

A text file with step-by-step instructions for building an image. Each instruction creates a layer — Docker caches layers, so unchanged layers do not need to be rebuilt, making builds fast.

Registry

A storage and distribution service for Docker images. Docker Hub is the default public registry. You push images to a registry and pull them on any machine.

Writing a Dockerfile

Dockerfile — Node.js app example # Start from official Node.js 20 image FROM node:20-alpine # Set working directory inside container WORKDIR /app # Copy package.json first (separate layer for better caching) COPY package*.json ./ # Install dependencies RUN npm ci --only=production # Copy application code COPY . . # Document which port the app uses EXPOSE 3000 # Command to run when container starts CMD ["node", "server.js"]
Build and run docker build -t my-app:1.0 . # build image docker run -p 3000:3000 my-app:1.0 # run container (map port) docker ps # list running containers docker logs <container-id> # view logs docker stop <container-id> # stop container

Docker Compose for Multi-Container Apps

docker-compose.yml — web app + PostgreSQL + Redis version: '3.8' services: web: build: . ports: - "3000:3000" environment: DATABASE_URL: postgres://user:pass@db:5432/myapp REDIS_URL: redis://cache:6379 depends_on: - db - cache db: image: postgres:16 volumes: - postgres_data:/var/lib/postgresql/data environment: POSTGRES_PASSWORD: pass POSTGRES_USER: user POSTGRES_DB: myapp cache: image: redis:7-alpine volumes: postgres_data:
Start / stop all services docker compose up -d # start all services in background docker compose down # stop and remove containers docker compose logs -f # follow all service logs

Essential Docker Commands

CommandWhat it does
docker build -t name .Build image from Dockerfile in current directory
docker run -p 80:80 nameRun container, map host port 80 to container port 80
docker psList running containers
docker ps -aList all containers including stopped
docker imagesList local images
docker exec -it <id> shOpen a shell inside a running container
docker logs -f <id>Follow container logs
docker rm <id>Remove a stopped container
docker rmi <image>Remove an image
docker pull nginxPull image from Docker Hub
docker push user/imagePush image to registry

💡 Use .dockerignore

Create a .dockerignore file (similar to .gitignore) to exclude unnecessary files from the build context: node_modules, .git, *.log, .env. This speeds up builds and reduces image size significantly.

⚠️ Never Store Secrets in Docker Images

Do not put API keys or passwords in your Dockerfile or bake them into the image. Use environment variables passed at runtime, Docker secrets, or a secrets manager. Anyone with access to the image can extract its layers and read any secrets baked in.

How We Research and Update This Guide

We test the underlying formula or workflow, compare outputs with reliable references, and revise examples whenever the page content changes.

  • The workflow or formula is tested directly in the tool and compared against independent reference examples.
  • Examples are kept practical so readers can verify the result without hidden assumptions.
  • Pages are revised whenever the interface, calculation flow, or surrounding guidance materially changes.

Frequently Asked Questions — Docker