Home/Learn/Docker/What is Docker & Why Containers?

What is Docker & Why Containers?

Beginner
Fundamentals

Docker packages an application and all its dependencies into a lightweight, portable container that runs identically on any machine — solving the classic "works on my machine" problem.

Overview

Before containers, deploying software meant wrestling with environment differences: dev runs Java 17, prod has Java 11; dev is macOS, prod is Ubuntu. Virtual Machines (VMs) solved isolation but at huge cost — each VM carries a full OS kernel (GBs of overhead). Docker containers share the host OS kernel and only package the application runtime and its libraries, making them start in milliseconds and occupy tens of MBs instead of GBs. Docker uses three Linux kernel features under the hood: namespaces (process isolation), cgroups (resource limits), and Union File System (layered images). A container is not a VM — it is an isolated process running on the same kernel as the host.

VM vs Container

A VM hypervisor virtualises hardware and runs a full guest OS. A container engine (Docker) shares the host kernel and isolates only the user-space. This makes containers 10-100x lighter and faster to start.

Container vs VM architecture
┌──────────────────────────────────────────┐

│              YOUR MACHINE                │

│                                          │

│  ┌──────────┐   ┌──────────┐            │

│  │ Container│   │ Container│            │

│  │  App A   │   │  App B   │            │

│  │ + libs   │   │ + libs   │            │

│  â””────┬─────┘   â””────┬─────┘            │

│       â””──────┬────────┘                 │

│         Docker Engine                   │

│         (shared OS kernel)              │

│              │                          │

│        Host OS Kernel                   │

â””──────────────────────────────────────────┘



vs VM:

┌──────────────────────────────────────────┐

│  VM 1: full Linux OS (2 GB)             │

│  VM 2: full Linux OS (2 GB)             │

│         Hypervisor (VMware/VirtualBox)   │

│              Host OS                    │

â””──────────────────────────────────────────┘

Core Concepts: Image, Container, Registry

An Image is a read-only blueprint (like a class). A Container is a running instance of an image (like an object). A Registry (Docker Hub, ECR, GCR) stores and distributes images. You pull images from a registry, run them as containers, and push your own images back to a registry.

bash — first Docker commands
# Pull an image from Docker Hub

docker pull nginx:1.25



# Run a container from that image

docker run -d -p 8080:80 --name my-nginx nginx:1.25

#           │   │              â””─ container name

#           │   â””─ host:container port mapping

#           â””─ detached (background)



# List running containers

docker ps



# See logs

docker logs my-nginx



# Stop and remove

docker stop my-nginx && docker rm my-nginx

Your First Dockerfile

A Dockerfile is a recipe that builds a custom image. Each instruction (FROM, RUN, COPY, CMD) creates a layer. Layers are cached — if a layer has not changed, Docker reuses the cached version, making rebuilds fast.

Dockerfile — Node.js example
# Dockerfile for a Node.js app

FROM node:20-alpine          # base image (Alpine = minimal Linux, ~5 MB)



WORKDIR /app                 # all subsequent commands run here



COPY package*.json ./        # copy dependency files first (cache trick)

RUN npm ci --only=production # install; this layer is cached until package.json changes



COPY . .                     # copy app source (invalidates only if source changes)



EXPOSE 3000                  # document the port (doesn't actually publish it)



CMD ["node", "server.js"]    # default command when container starts

Key Points to Remember

  • 1Containers share the host OS kernel; VMs have their own full OS.
  • 2An image is a read-only blueprint; a container is a running instance.
  • 3Docker layers are cached — put infrequently-changing instructions first.
  • 4Registries (Docker Hub, ECR) store and distribute images.
  • 5Docker uses Linux namespaces, cgroups, and Union FS under the hood.

Interview Questions

Sign in to ask Aria
1

What is the difference between a Docker image and a container?

2

How are containers different from VMs?

3

What problem does Docker solve?

Ask Aria about What is Docker & Why Containers?

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…