Falak
Docker

Docker bascis made easy

1 min read

#
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:

JS
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:

JS
Dockerfile
    ↓
Docker Image
    ↓
Same environment
    ↓
Same application behavior

The 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:

JS
macOS                         Windows
  ↓                                      ↓
Docker Desktop              Docker Desktop
  ↓                                          ↓
Linux environment           Linux environment
  ↓                                           ↓
Docker Container            Docker Container

Therefore, 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:

JS
MacBook     → Host machine
macOS       → Host operating system
Docker      → Software running on the host

If you're using a Windows PC:

JS
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:

JS
Linux → Linux kernel
macOS → XNU kernel
Windows → Windows NT kernel

This 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:

JS
Linux Host
    ↓
Linux Kernel
    ↓
Docker
    ↓
Containers

Containers share the host's Linux kernel.

#On macOS

macOS does not provide a Linux kernel.

Therefore:

JS
Mac
 ↓
macOS
 ↓
Docker Desktop
 ↓
Linux VM
 ↓
Linux Kernel
 ↓
Linux Containers

The 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:

JS
Physical Mac
    ↓
Virtualization
    ↓
Linux Virtual Machine

The 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

JS
Physical Machine
      ↓
Virtual Machine
      ↓
Complete Operating System
      ↓
Application

#Docker Container

JS
Operating System
      ↓
Docker
      ↓
Container
      ↓
Application

Containers 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:

JS
macOS → macOS kernel
Windows → Windows kernel

Neither provides a Linux kernel directly.

Docker Desktop therefore provides a Linux environment, typically through a lightweight virtual machine.

JS
Mac
 ↓
macOS
 ↓
Docker Desktop
 ↓
Linux VM
 ↓
Linux Kernel
 ↓
Linux Container

So 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:

JS
VM 1
├── Linux kernel
├── OS
├── Libraries
└── Application

VM 2
├── Linux kernel
├── OS
├── Libraries
└── Application

Containers can instead share the same underlying kernel:

JS
Linux Kernel
    │
    ├── Container 1 → Node.js
    ├── Container 2 → Python
    └── Container 3 → Nginx

Because 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

JS
VM
├── Kernel
├── Operating System
├── Libraries
└── Application

#Container

JS
Shared Linux Kernel
├── Container
├── Container
└── Container

This 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:

JS
Container
├── /app
│   ├── server.js
│   └── data.txt
└── Writable layer

If the container is removed, its writable layer is removed as well.

docker rm my-container

Therefore:

JS
Container removed
      ↓
Writable layer removed
      ↓
Data lost

#13. Does stopping a container delete its data?

No.

Stopping a container:

JS
docker stop my-container

does not normally remove its data.

The container still exists:

JS
Stopped Container
├── Application files
└── Writable layer

If you start the same container again:

JS
docker start my-container

the 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:

JS
docker volume create mydata

Then attach it to a container:

JS
docker run -v mydata:/data myimage

Now:

JS
Container
    │
    │ /data
    ▼
Docker Volume
    │
    ▼
Persistent Data

If the container is removed:

JS
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:

JS
Container writable layer → Temporary
Docker volume            → Persistent

Volumes are commonly used for databases and other data that must survive container replacement.


#16. Why are volumes useful for databases?

Consider a MySQL container:

JS
MySQL Container
      │
      ▼
MySQL Volume
      │
      ▼
Database Data

If the MySQL container is removed:

JS
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:

JS
Docker Image
     ↓
Container Writable Layer
     ↓
Application Changes

The 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:

JS
Application
     ↓
Container Writable Layer
     ↓
Storage Driver
     ↓
Storage

The 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:

JS
Application
     ↓
Container Writable Layer
     ↓
Storage Driver
     ↓
Storage

A volume is stored separately from that container writable layer:

JS
Application
     ↓
Volume
     ↓
Persistent Storage

Therefore, 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:

JS
Physical Computer
                        │
                  Host Machine
                        │
                 Operating System
                        │
                  Docker Desktop
                        │
                  ┌─────▼─────┐
                  │  Linux VM │
                  │           │
                  │Linux Kernel│
                  │     │     │
                  │   Docker  │
                  │   /   \   │
                  │Container Container
                  └───────────┘

And for storage:

JS
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.

Share this post