Docker bascis made easy
On this page
#
Docker Fundamentals: From Dockerfiles to Containers, Kernels, Virtualization and Volumes
Docker can seem confusing at first because several concepts—Dockerfiles, images, containers, kernels, virtualization, and volumes—are closely related.
This guide explains these concepts step by step.
#1. What is a Dockerfile?
A Dockerfile is a script containing a series of instructions that tells Docker how to build a Docker image.
For example:
FROM node:20
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]Each instruction defines part of the application's environment.
The important benefit is consistency. Instead of manually configuring Node.js, dependencies, and application setup on every machine, Docker follows the same instructions every time.
#2. How does a Dockerfile provide consistency?
Without Docker, different developers might have different environments:
Developer A → Node 20
Developer B → Node 18
Developer C → Node 22
This can cause the classic:
"It works on my machine."
With Docker:
Dockerfile
↓
Docker Image
↓
Same environment
↓
Same application behaviorThe Dockerfile defines the base image, runtime, dependencies, and setup steps, making the resulting environment much more predictable.
#3. What happens if the same Dockerfile runs on macOS and Windows?
Suppose we use:
FROM node:20
and build the application on both macOS and Windows.
Docker Desktop provides a Linux environment for running Linux containers.
Conceptually:
macOS Windows
↓ ↓
Docker Desktop Docker Desktop
↓ ↓
Linux environment Linux environment
↓ ↓
Docker Container Docker ContainerTherefore, the application inside the container gets essentially the same Linux-based environment on both machines.
The host operating systems can be different, while the container environment remains largely consistent.
#4. What is a host machine?
A host machine is the physical computer on which Docker or a virtual machine is running.
For example, if you're using a MacBook:
MacBook → Host machine
macOS → Host operating system
Docker → Software running on the hostIf you're using a Windows PC:
Windows PC → Host machine
Windows → Host operating system
Docker → Software running on the host#5. What is a kernel?
The kernel is the core part of an operating system.
It manages important resources such as:
CPU
Memory
Processes
Filesystems
Hardware
Different operating systems have different kernels.
For example:
Linux → Linux kernel
macOS → XNU kernel
Windows → Windows NT kernelThis becomes important when running containers.
#6. Does Docker share the host machine's kernel?
The answer depends on the host operating system.
#On Linux
Docker can directly use the host's Linux kernel:
Linux Host
↓
Linux Kernel
↓
Docker
↓
ContainersContainers share the host's Linux kernel.
#On macOS
macOS does not provide a Linux kernel.
Therefore:
Mac
↓
macOS
↓
Docker Desktop
↓
Linux VM
↓
Linux Kernel
↓
Linux ContainersThe containers share the Linux kernel inside the Linux environment, not the macOS kernel.
#7. What is virtualization?
Virtualization is the technology that allows a virtual computer to run inside a physical computer.
For example:
Physical Mac
↓
Virtualization
↓
Linux Virtual MachineThe virtual machine behaves like a separate computer and has its own operating system and kernel.
#8. Is Docker the same as virtualization?
No.
Docker primarily uses containerization, while virtual machines use virtualization.
#Virtual Machine
Physical Machine
↓
Virtual Machine
↓
Complete Operating System
↓
Application#Docker Container
Operating System
↓
Docker
↓
Container
↓
ApplicationContainers share the underlying kernel, whereas a VM has its own complete operating system and kernel.
This is one reason containers are generally more lightweight than virtual machines.
#9. Why does Docker use virtualization on macOS and Windows?
A Linux container needs Linux kernel functionality.
However:
macOS → macOS kernel
Windows → Windows kernelNeither provides a Linux kernel directly.
Docker Desktop therefore provides a Linux environment, typically through a lightweight virtual machine.
Mac
↓
macOS
↓
Docker Desktop
↓
Linux VM
↓
Linux Kernel
↓
Linux ContainerSo Docker on macOS is not running the Linux container directly on the macOS kernel.
#10. Why are containers lightweight?
A traditional virtual machine usually contains a complete operating system:
VM 1
├── Linux kernel
├── OS
├── Libraries
└── Application
VM 2
├── Linux kernel
├── OS
├── Libraries
└── ApplicationContainers can instead share the same underlying kernel:
Linux Kernel
│
├── Container 1 → Node.js
├── Container 2 → Python
└── Container 3 → NginxBecause containers don't each need a separate kernel and complete operating system, they generally require fewer resources and start faster than traditional VMs.
#11. Is a Docker container a VM?
No.
A VM virtualizes a complete computer.
A container isolates an application and its environment while sharing the underlying kernel.
#VM
VM
├── Kernel
├── Operating System
├── Libraries
└── Application#Container
Shared Linux Kernel
├── Container
├── Container
└── ContainerThis distinction is fundamental to understanding Docker.
#12. Is data inside a Docker container temporary?
By default, yes.
When an application writes data inside a container, that data is written to the container's writable layer.
For example:
Container
├── /app
│ ├── server.js
│ └── data.txt
└── Writable layerIf the container is removed, its writable layer is removed as well.
docker rm my-container
Therefore:
Container removed
↓
Writable layer removed
↓
Data lost#13. Does stopping a container delete its data?
No.
Stopping a container:
docker stop my-containerdoes not normally remove its data.
The container still exists:
Stopped Container
├── Application files
└── Writable layerIf you start the same container again:
docker start my-containerthe data in its writable layer is still available.
The data is normally lost when the container itself is removed.
#14. How do we persist data?
For data that needs to survive container removal, Docker provides volumes.
Example:
docker volume create mydataThen attach it to a container:
docker run -v mydata:/data myimageNow:
Container
│
│ /data
▼
Docker Volume
│
▼
Persistent DataIf the container is removed:
Container → ❌ Removed
Volume → ✅ Remains
Data → ✅ Remains#15. What is a Docker volume?
A Docker volume is persistent storage managed by Docker and stored outside the container's writable layer.
The key difference is:
Container writable layer → Temporary
Docker volume → PersistentVolumes are commonly used for databases and other data that must survive container replacement.
#16. Why are volumes useful for databases?
Consider a MySQL container:
MySQL Container
│
▼
MySQL Volume
│
▼
Database DataIf the MySQL container is removed:
MySQL Container → ❌
MySQL Volume → ✅
Database Data → ✅A new MySQL container can be attached to the same volume.
This allows the database data to survive container replacement.
#17. What is the container writable layer?
The container writable layer is the layer where changes made by a running container are stored when those changes aren't being written to a volume.
Conceptually:
Docker Image
↓
Container Writable Layer
↓
Application ChangesThe writable layer belongs to the container.
Therefore, when the container is removed, the writable layer is removed too.
#18. What is a Docker storage driver?
A storage driver is the mechanism Docker uses to manage the container's layered filesystem.
For example, Linux Docker commonly uses:
overlay2 / OverlayFS
Conceptually:
Application
↓
Container Writable Layer
↓
Storage Driver
↓
StorageThe storage driver manages how the container's filesystem layers are stored and accessed.
#19. What does "volumes bypass the storage driver overhead" mean?
A container's normal writable data goes through its writable filesystem layer:
Application
↓
Container Writable Layer
↓
Storage Driver
↓
StorageA volume is stored separately from that container writable layer:
Application
↓
Volume
↓
Persistent StorageTherefore, volume data isn't managed as part of the container's writable layer.
This is generally better for persistent and high-I/O workloads such as databases.
#20. The complete Docker mental model
Putting everything together:
Physical Computer
│
Host Machine
│
Operating System
│
Docker Desktop
│
┌─────▼─────┐
│ Linux VM │
│ │
│Linux Kernel│
│ │ │
│ Docker │
│ / \ │
│Container Container
└───────────┘And for storage:
Container
│
├── Writable Layer
│ ↓
│ Temporary
│
└── Volume
↓
Persistent
↓
Data#Key Takeaways
Dockerfile — Instructions used to build a Docker image.
Docker Image — A packaged application environment containing everything needed to run an application.
Container — A running instance of a Docker image.
Host Machine — The physical computer on which Docker is running.
Kernel — The core part of an operating system that manages resources such as CPU, memory, processes, and hardware.
Virtualization — Technology that allows a virtual computer to run inside a physical computer.
Docker — A platform used to build, run, and manage containers.
Writable Layer — Temporary storage where changes made inside a container are stored.
Volume — Persistent storage managed by Docker that can survive container removal.
Storage Driver — The mechanism Docker uses to manage the layered filesystem of containers.
#The most important distinction
Docker uses containerization. On Linux, containers can directly share the host's Linux kernel. On macOS and Windows, Docker Desktop uses virtualization underneath to provide a Linux environment and kernel required by Linux containers.