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
| Feature | Docker Container | Virtual Machine |
|---|---|---|
| OS | Shares host OS kernel | Full guest OS (Windows, Linux) |
| Size | Megabytes (10–500 MB) | Gigabytes (3–20 GB) |
| Start time | Seconds (sub-second common) | Minutes |
| Isolation | Process-level (namespace/cgroups) | Full hardware virtualisation |
| Performance | Near-native (minimal overhead) | 5–15% overhead from hypervisor |
| Portability | Run anywhere Docker is installed | Tied to hypervisor (VMware, HyperV) |
| Best for | App packaging, microservices, CI/CD | Full 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
Docker Compose for Multi-Container Apps
Essential Docker Commands
| Command | What it does |
|---|---|
| docker build -t name . | Build image from Dockerfile in current directory |
| docker run -p 80:80 name | Run container, map host port 80 to container port 80 |
| docker ps | List running containers |
| docker ps -a | List all containers including stopped |
| docker images | List local images |
| docker exec -it <id> sh | Open 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 nginx | Pull image from Docker Hub |
| docker push user/image | Push 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
Docker is a tool that packages your application and everything it needs to run (code, runtime, libraries, config) into a single unit called a container. You can run this container on any machine that has Docker installed — development laptop, CI server, or cloud VM — and it behaves identically everywhere. It solves the classic "works on my machine" problem.
A Docker image is a read-only template — like a class definition or a blueprint. It contains the application code, runtime, and all dependencies, but it is not running. A container is a running instance of an image — like an object created from a class. You can run many containers from the same image simultaneously. Images are built once; containers are started, stopped, and destroyed.
A virtual machine (VM) virtualises the entire hardware stack — it includes a full guest operating system (3-10 GB, takes minutes to start). Docker containers share the host OS kernel — only the application and its dependencies are packaged (typically tens of MB, starts in seconds). VMs provide stronger isolation (separate kernel). Containers are lighter and faster but share the host kernel, so a kernel exploit could potentially affect all containers on a host.
A Dockerfile is a text file with instructions for building a Docker image. Each instruction creates a layer in the image. Key instructions: FROM (base image), RUN (execute command), COPY (copy files into image), WORKDIR (set working directory), EXPOSE (document port), CMD (default command when container starts), ENV (set environment variable). You run "docker build" to turn a Dockerfile into an image.
Docker Compose is a tool for defining and running multi-container applications. You describe all your services (web app, database, cache) in a docker-compose.yml file and start everything with one command: "docker compose up". It handles networking between containers, volume mounts, environment variables, and startup order. Compose is ideal for local development environments — it replaces pages of manual docker run commands.
Yes — Docker containers are widely used in production. For single-server deployments, Docker Compose or plain Docker works well. For multi-server, high-availability production systems, you would typically use an orchestrator like Kubernetes or Docker Swarm to manage container scheduling, scaling, health checks, and rolling updates. Major platforms like AWS ECS, Google Cloud Run, and Azure Container Apps all run Docker containers natively.