TExcelTAD
Home › Docker › Docker Intro

Docker Intro

Docker is a tool for packaging an application together with everything it needs to run (the language runtime, system libraries, configuration files) into a single unit called an image. Starting an image gives you a container: an isolated process that behaves the same on your laptop, on a colleague's machine, in CI and on a production server.

The problem Docker solves is an old one: "it works on my machine". Your app needs Python 3.12, a specific version of libpq, a few environment variables and a writable folder. Your teammate has Python 3.10 and a different libpq. The server has neither. With Docker, you describe that environment once in a Dockerfile, build an image from it, and every machine runs exactly that image.

How this course works

Docker itself cannot run inside a web page, so this course uses two kinds of code blocks:

  • Real Docker material (Dockerfiles, compose.yaml files and terminal sessions) is shown in normal highlighted blocks. Copy them into a terminal on a machine with Docker installed to try them. Typical output is shown in blocks labelled Sample output; IDs, timestamps and sizes will differ on your machine.
  • Runnable Python models make the ideas visible: a layer cache, a port mapper, a dependency resolver. Press ▶ Run, change the inputs, and watch what happens. They are simplified models, not Docker's real source code, but they follow the same rules.

Containers vs virtual machines

Both containers and virtual machines (VMs) isolate software, but they do it at different levels.

Virtual machine Container
What is virtualised Hardware: each VM boots its own kernel The operating system: containers share the host's kernel
Typical size Gigabytes (a full OS) Megabytes (just the app and its libraries)
Start-up time Tens of seconds to minutes Usually well under a second
Isolation Very strong (separate kernels) Strong, but one shared kernel
Runs Any OS the hypervisor supports Linux containers need a Linux kernel (Docker Desktop provides one on macOS and Windows)

A container is not a tiny VM. It is an ordinary Linux process that the kernel fences off using two features:

  • namespaces give the process its own view of process IDs, network interfaces, mount points and hostname, so it thinks it is alone on the machine;
  • cgroups (control groups) limit how much CPU, memory and I/O it may use.

The model below compares a server packed with VMs against the same server packed with containers. Each VM pays for its own guest operating system; containers share the host's.

python · try it yourself
HOST_RAM_MB = 16_384
HOST_OS_MB = 1_024
GUEST_OS_MB = 1_536          # each VM boots a full operating system
APP_MB = 300                 # memory the application itself needs

available = HOST_RAM_MB - HOST_OS_MB
vm_count = available // (GUEST_OS_MB + APP_MB)
container_count = available // APP_MB

print(f"RAM available for workloads: {available} MB")
print(f"VMs that fit:        {vm_count:3} (each {GUEST_OS_MB + APP_MB} MB)")
print(f"Containers that fit: {container_count:3} (each {APP_MB} MB)")
print(f"Density gain: {container_count / vm_count:.1f}x")
RAM available for workloads: 15360 MB
VMs that fit:          8 (each 1836 MB)
Containers that fit:  51 (each 300 MB)
Density gain: 6.4x

The numbers are illustrative, but the shape of the result is real: removing a guest operating system per workload lets you run many more workloads on the same hardware.

The key vocabulary

Term Meaning
Image A read-only template: a stack of filesystem layers plus metadata (which command to run, which ports, which user)
Container A running (or stopped) instance of an image, with its own thin writable layer on top
Dockerfile A text file of instructions that builds an image
Registry A server that stores images, such as Docker Hub or GitHub Container Registry
Docker Engine The background service (dockerd) that builds images and runs containers
Docker CLI The docker command you type; it sends requests to the engine

The relationship between an image and a container is like the one between a class and its objects: one image, many containers.

python · try it yourself
class Image:
    def __init__(self, name, files):
        self.name = name
        self.files = dict(files)       # read-only template

class Container:
    def __init__(self, image, name):
        self.image = image
        self.name = name
        self.changes = {}              # this container's own writable layer

    def write(self, path, text):
        self.changes[path] = text

    def read(self, path):
        return self.changes.get(path, self.image.files.get(path))

web = Image("nginx:1.27", {"/etc/motd": "welcome", "/index.html": "<h1>Hi</h1>"})
a = Container(web, "site-a")
b = Container(web, "site-b")

a.write("/index.html", "<h1>Site A</h1>")
print(a.name, "->", a.read("/index.html"))
print(b.name, "->", b.read("/index.html"))
print("image unchanged:", web.files["/index.html"])
site-a -> <h1>Site A</h1>
site-b -> <h1>Hi</h1>
image unchanged: <h1>Hi</h1>

Container site-a changed a file, yet site-b and the image itself are untouched. That is exactly how Docker behaves: changes live in the container's writable layer, never in the image.

A first taste

On a machine with Docker, this one command downloads a tiny image and runs it:

bash
docker run hello-world

Sample output (abridged):

text
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

What you will learn

  1. Running containers and working with images, layers and registries.
  2. Writing Dockerfiles, using the build cache well, and multi-stage builds.
  3. Ports, environment variables, volumes and networks.
  4. Docker Compose for multi-container apps.
  5. Production topics: image size, security, resource limits, restart policies, CI/CD and orchestration.

Note: Docker popularised containers, but the image format is an open standard (OCI). Images you build with Docker also run on Podman, containerd and Kubernetes.

Test yourself

What is the main reason containers start faster and use less memory than virtual machines?

  1. They are written in Go
  2. They share the host's kernel instead of booting their own operating system
  3. They do not use any isolation
  4. They run only one thread

Which statement about images and containers is correct?

  1. A container is a read-only template
  2. Changing a file in a container changes the image
  3. Many containers can be started from one image, each with its own writable layer
  4. An image can only be used by one container at a time

Which Linux kernel feature limits how much CPU and memory a container may use?

  1. namespaces
  2. cgroups
  3. chroot
  4. SELinux