KCNA: Containers and Docker

- 6 mins

Containers and Docker

A container isn’t a mini-VM; it’s just a standard Linux process wearing a very sophisticated disguise.

chroot - The Foundation

chroot is a process that changes the root directory for a running process and its children, effectively isolating them from the rest of the system.

Two Key Ingredients That Made Docker Possible

1. Linux Namespaces

Namespace Type What It Isolates Key Features
Network Networking stack Own IP addresses, routing tables, firewall rules
Mount File system mount points Independent mount points visible only within namespace
Process (PID) Process IDs Isolated process trees
User User and group IDs Independent user management
UTS Hostname and domain name Separate system identity
IPC Inter-process communication Isolated message queues and semaphores

2. Control Groups (cgroups)

Docker combines chroot + Linux Namespaces + cgroups to create:

Docker Desktop Architecture

Platform Differences

Containers vs. Virtual Machines (VMs)

The primary difference is the Kernel.

Feature Containers Virtual Machines
Guest OS None (Shares Host Kernel) Full Guest OS (Separate OS instance)
Startup Time Faster Slower
Resources Lightweight / High Density Heavy / Lower Density
Isolation Process-level (Namespaces) Hardware-level (Hypervisor)

On macOS and Windows, Docker Desktop runs a tiny, hidden Linux VM to provide the kernel needed for these Linux features to work!

Kernel = the core of the operating system. It directly controls the hardware.

Operating System (OS) = the entire system software that includes the kernel 
plus all the tools and programs that make the computer usable.

All containers:
- Call into the same kernel memory
- Use the same:
   - scheduler
   - memory manager
   - filesystem drivers
   - network stack
   - device drivers

Key Advantage: Kernel Sharing

Containers share the host OS kernel, which:

When we say containers share the kernel, we literally mean every container’s processes execute system calls inside the exact same running kernel code.

Container Images & The Union Filesystem

A image is a ready-to-run software package (the blueprint) containing everything needed to run an application:

Layered Approach

┌─────────────────┐
│   App Layer     │ ← Your application
├─────────────────┤
│   Lib Layer     │ ← Dependencies & libraries
├─────────────────┤
│   OS Layer      │ ← Base operating system
└─────────────────┘

Key Concepts:

The Image is the blueprint, while a Container is the running instance.

Tags and Versioning

OCI Compliance

Persistent Data: Docker Volumes

By default, data inside a container is ephemeral (it dies when the container dies). To keep data safe, we use Volumes.

Optimization: Multi-stage Builds

To keep images small and secure, use multi-stage builds. This allows you to use one large image to compile your code (with all the bulky tools) and then copy only the final executable into a tiny “production” image (like alpine or scratch).

# Build stage
FROM golang:1.19 AS builder
COPY . /app
RUN go build -o myapp

# Final stage
FROM alpine:latest
COPY --from=builder /app/myapp /usr/local/bin/
CMD ["myapp"]

Result: Smaller final images, only necessary dependencies and faster deployments.

Golden Rules:

  1. Stateless Design
    • Containers should not store state
    • Use external storage for data
  2. Immutable Containers
    • Never change code in running containers
    • Rebuild and redeploy instead of modifying
  3. Everything in the Image
    • Include all dependencies
    • Ensure consistent behavior across environments

Basic Container Operations

# Start a new container
docker run [OPTIONS] IMAGE_NAME

# Common flags
docker run -it ubuntu:latest    # Interactive terminal
docker run -d nginx            # Run in background (detached)
docker run -P nginx            # Expose all ports

🎛️ Important Docker Flags

Flag Purpose Usage
-i Interactive Allows user interaction with container process
-t Terminal Creates terminal I/O environment
-d Detached Runs container in background
-P Publish all Expose all container ports
-p Publish specific Map specific ports (e.g., -p 8080:80)
Lais Ziegler

Lais Ziegler

Dev in training... 👋