SECTION 03 · 35 MIN READ & EXPLORE
Containers and Images
Explore isolated runtimes, disposable filesystems, background services, and port mappings.
What are Linux Containers?
A container runs a process using an image and a writable layer. Stopping, removing, and recreating it are different operations.
Under the hood, Linux containers are regular Linux processes isolated from each other by two kernel features while sharing the same host kernel: namespaces (what a process can see) and control groups / cgroups (how much CPU, memory, and I/O it can use).
Linux namespaces and cgroups
Experiment freely. Changes disappear when you reset or refresh.
What are Container Images?
A container image is a self-contained, immutable package holding an application, its libraries, runtime, and base filesystem stacked as read-only layers. When you start a container from an image, Docker mounts those shared read-only layers and adds a thin read-write (R/W) layer on top for runtime changes.
Container images, layers, and the writable container layer
Experiment freely. Changes disappear when you reset or refresh.
History of Containers
Containers did not start with Docker. They are the result of decades of Unix and Linux kernel work aimed at isolating processes without the overhead of running a separate operating system.
From chroot to modern containers
chroot (Unix V7)
Introduced the ability to change a process’s apparent root directory (
/), creating the first building block for filesystem isolation.FreeBSD Jails
Extended
chrootby isolating processes, filesystems, users, and IP addresses into lightweight, self-contained environments.Linux Namespaces
Starting with mount namespaces in Linux 2.4.19, the kernel progressively added PID, network, IPC, UTS, and user namespaces so processes see isolated views of the system.
Control Groups (cgroups)
Added to the Linux kernel to limit, account for, and isolate resource usage such as CPU, memory, and disk I/O across groups of processes.
LXC (Linux Containers)
Combined namespaces and
cgroupsinto the first complete Linux container implementation without requiring kernel patches.DevOps
Coined with the first DevOpsDays in Ghent, driving teams toward automated, repeatable, and fast software delivery—creating huge demand for consistent environments from laptop to production.
Docker
Packaged Linux containers with portable layered images, a simple CLI, and a central registry—making containers practical for everyday development and deployment.
Kubernetes
Open-sourced by Google to automate scheduling, scaling, self-healing, and networking of containerized applications across clusters of machines.
Open Container Initiative (OCI)
Established open specifications for container image formats and runtimes (
runc), enabling interoperability across Docker, Podman, containerd, and Kubernetes.
Why and When to Use Containers?
Work through these five scenarios to see when containers shine: isolating legacy runtimes, running disposable sandboxes, launching background services, serving local files, and executing one-off CLI tools.
Running containers with docker run
Legacy Runtime (Python 2.7)
-it--rm# 1. Check your host Python version (typically 3.x)
python --version
# 2. Start an interactive Python 2.7 container that cleans itself up on exit
docker run -it --rm python:2.7-alpine
# 3. Verify the version inside the container, then press Ctrl+D to exit
python --versionWHAT TO OBSERVE
Inside the container, python --version reports Python 2.7.x. Exiting destroys the container automatically (--rm), leaving your host environment untouched.
Ephemeral Sandbox (Alpine Linux)
-it--rm# 1. Start a lightweight Alpine Linux shell
docker run -it --rm alpine sh
# 2. Delete /bin inside the container's writable layer
rm -rf /bin
# 3. Try running ls (fails because /bin/ls was removed)
ls
# 4. Exit and start a new container from the same image
exit
docker run -it --rm alpine sh
lsWHAT TO OBSERVE
ls fails inside the first container after rm -rf /bin, but works immediately in the new container. Images are immutable; changes only live in a container’s disposable writable layer.
Background Service (Redis)
-d--name-p# 1. Start Redis in the background with a name and localhost port mapping
docker run -d --name ticket-db -p 127.0.0.1:6379:6379 redis
# 2. Confirm the container is running
docker ps
# 3. Connect from a temporary container sharing the same network namespace
docker run -it --rm --network container:ticket-db redis redis-cli -h 127.0.0.1
# 4. Inside redis-cli, run PING (expect PONG), exit, and clean up
docker rm -f ticket-dbWHAT TO OBSERVE
docker ps lists ticket-db as Up, and redis-cli returns PONG when you send PING.
Serving Local Files (Nginx Bind Mount)
-d--name-p-v# 1. Create a local HTML file
echo "<h1>Match Day: Morocco vs. Brazil</h1>" > index.html
# 2. Run Nginx and bind-mount the current directory to Nginx's web root
docker run -d --name press-server -p 127.0.0.1:8080:80 -v "$(pwd)":/usr/share/nginx/html nginx
# 3. Open http://localhost:8080 in your browser
# 4. Edit the file locally and refresh the browser to see the update
echo "<h1>Full Time: 1-0</h1>" > index.html
# 5. Clean up when finished
docker rm -f press-serverWHAT TO OBSERVE
Opening http://localhost:8080 renders your local index.html, and refreshing after editing the host file immediately displays the updated content without restarting the container.
One-Off CLI Task (Whalesay)
--rm# Pass command arguments to the container and remove it as soon as it exits
docker run --rm docker/whalesay cowsay "Kickoff at 20:00!"WHAT TO OBSERVE
The container prints the ASCII art message to stdout, exits immediately, and disappears from docker ps -a.
Quick Reference
Docker CLI & flags to remember
Core CRUD operations for images and containers, plus the most common docker run flags.
Images (CRUD)
docker build- Build an image from a Dockerfile
docker image ls- List local images (alias: docker images)
docker build / tag- Rebuild or retag an image
docker rmi- Delete an image
Containers (CRUD)
docker run- Create and start a container
docker ps -a- List running (or all) containers
docker exec- Run a command inside a running container
docker rm- Remove a container (-f to force)
Common docker run flags
-it- Interactive terminal attached to the container
--rm- Automatically remove the container when it exits
-d- Detached mode (run container in the background)
--name NAME- Assign a custom, readable container name
-p HOST:CONT- Publish a container port to the host
-v SRC:DEST- Mount a host directory or named volume
Containers: the course whiteboard
Experiment freely. Changes disappear when you reset or refresh.
Take a moment to explain the idea in your own words.
