Table of Contents


Docker architecture

Docker operates on a client-server model: the Docker CLI (client) sends requests to the Docker daemon (server), which is responsible for building images, managing container lifecycle, and handling networking.

Reference material: https://flipbook.page/n/0be69c158f3f47b8bbe5e2fdcd5b7d14

img-202604292204.png

Verify that the Docker engine is installed and running:

$ docker --version
Docker version 28.2.2

Docker first steps

The quickest way to validate a Docker installation is to run a minimal test image:

$ docker run hello-world
 
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
Status: Downloaded newer image for hello-world:latest
 
Hello from Docker!
This message shows that your installation appears to be working correctly.

What just happened

  1. The Docker client contacted the Docker daemon.
  2. The daemon pulled the hello-world image from Docker Hub.
  3. The daemon created a new container from that image and ran it.
  4. The daemon streamed the container output back to your terminal.

To explore further, run an interactive Ubuntu container:

$ docker run -it ubuntu bash

More examples: https://docs.docker.com/get-started/

List containers

View all containers, including those that have exited:

$ docker ps -a
 
CONTAINER ID   IMAGE         COMMAND   CREATED             STATUS                     PORTS   NAMES
e19fa8619899   hello-world   "/hello"  About a minute ago  Exited (0) 1 minute ago            lucid_goodall

List images

docker image ls

Remove the container

docker rm e19f   # or by name: docker rm lucid_goodall

Hello World with web engine

Run a real web server container and publish it to the host network.

Search Docker Hub for available images:

$ docker search hello
NAME               DESCRIPTION                                     STARS   OFFICIAL
circleci/hello     This image is for internal use                  0
hello-world        Hello World! (an example of minimal Dockeriz…   2560    [OK]
hello-seattle      Hello from DockerCon 2016 (Seattle)!            14      [OK]
nginxdemos/hello   NGINX webserver that serves a simple page co…   173

Start a container in detached mode (-d) and map host port 8080 to container port 80 (-p):

docker run -d --name hello-world -p 8080:80 nginxdemos/hello

Verify the container is running:

$ docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS         PORTS                                       NAMES
20d6eeca6627   nginxdemos/hello   "/docker-entrypoint.…"   25 seconds ago  Up 25 seconds  [IP_ADDRESS]:8080->80/tcp, [::]:8080->80/tcp   hello-world

Open a browser at http://localhost:8080 to confirm the service is serving.

Clean up when finished:

docker stop hello-world
docker rm hello-world

Containers life cycle

# start a new container
docker run -d --name hello-world -p 8080:80 nginxdemos/hello
 
docker stop hello-world     # graceful stop (SIGTERM)
docker start hello-world    # restart a stopped container
docker restart hello-world  # stop + start
docker kill hello-world     # force stop (SIGKILL)
docker rm hello-world       # remove the container

Destructive commands

docker kill and docker rm discard the container process and its writable layer. Data stored inside a removed container is lost unless it is persisted in a volume.

Debugging containers

# start a container
docker run -d --name hello-world -p 8080:80 nginxdemos/hello
 
# open a shell inside the running container
docker exec -it hello-world sh
# list processes inside the container: ps
 
# view container output
docker logs hello-world
 
# inspect container metadata (JSON)
docker inspect hello-world
 
# extract a specific field with jq
docker inspect hello-world | jq '.[0].NetworkSettings.IPAddress'
 
# show processes running in the container from the host
docker top hello-world
 
# monitor live resource usage (CPU, memory, network)
docker stats

Container ports

# start a container using a specific image tag
docker run -d --name hello-world -p 3000:80 nginxdemos/hello:plain-text
 
# list the container's port mappings
docker port hello-world
 
# confirm the listener on the host
netstat -lnt
 
# test the mapped port
$ curl localhost:8080/test
Server address: [IP_ADDRESS]:80
Server name: 77ea8fa3b385
Date: 29/Apr/2026:21:38:32 +0000
URI: /test
Request ID: 696b481365d46684b96d27530420c240

Manage images

$ docker images
 
REPOSITORY          TAG          IMAGE ID       CREATED        SIZE
nginxdemos/hello    plain-text   801cc216dbcc   8 months ago   52.5MB
nginxdemos/hello    latest       3e97ffb459b9   8 months ago   52.6MB
 
# delete an image by ID
docker image rm 3e97ffb459b9   # or by name: docker image rm nginxdemos/hello:latest
 
# download an image from the registry
docker pull nginxdemos/hello:latest

Image tags

Example of tag selection on Docker Hub: https://hub.docker.com/_/mariadb

img-202604292304.png

Environment variables

Pass configuration to a container at runtime with --env. Here, the MariaDB root password is injected as an environment variable:

# start a MariaDB container
docker run --detach --name my-mariadb --env MARIADB_ROOT_PASSWORD=123456 mariadb:latest
 
# open a shell inside the container
docker exec -it my-mariadb sh
 
# print the environment variables
$ printenv
HOSTNAME=cafbb34739a3
HOME=/root
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MARIADB_ROOT_PASSWORD=123456
LANG=C.UTF-8
GOSU_VERSION=1.19
MARIADB_VERSION=1:12.2.2+maria~ubu2404
PWD=/

Verify that the password took effect:

$ mariadb
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
 
$ mariadb -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 12.2.2-MariaDB-ubu2404 mariadb.org binary distribution
 
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> exit

Secrets in environment variables

Passing passwords via --env exposes them in docker inspect output and shell history. Use Docker secrets or a secrets manager for anything beyond lab exercises.

Clean up:

docker kill my-mariadb
docker rm my-mariadb

Managing Resources

By default a container has unlimited access to the host’s CPU and memory. Production containers should always set limits, so one misbehaving service cannot starve the others.

Start the hello web server with hard limits: at most half a CPU core and 128 MB of memory:

docker run -d --name hello-limited \
  --cpus=0.5 \
  --memory=128m \
  --pids-limit=100 \
  -p 8080:80 \
  nginxdemos/hello
FlagEffect
--cpus=0.5Caps total CPU usage at half a core (CFS quota)
--memory=128mHard memory cap; exceeding it triggers an OOM kill
--memory-swap=128mDisables swap (equal to --memory) — optional, add for strictness
--pids-limit=100Caps the number of processes inside the container

Verify the limits are active:

# inspect the configured limits
docker inspect hello-limited --format 'CPU: {{.HostConfig.NanoCpus}} | Memory: {{.HostConfig.Memory}}'
 
# watch live usage — usage will never cross the limits
docker stats --no-stream hello-limited
$ docker stats --no-stream hello-limited
CONTAINER ID   NAME            CPU %   MEM USAGE / LIMIT   MEM %   NET I/O          BLOCK I/O   PIDS
a2b3e7f1c9d4   hello-limited   0.05%   4.2MiB / 128MiB     3.28%   1.2kB / 8.4kB    0B / 0B     2

Test the CPU limit — generate load with a second container and compare:

# unlimited container burning CPU (4 workers)
docker run -d --name stress-unlimited polinux/stress \
  stress --cpu 4 --timeout 60s
 
# same load, but capped at 0.5 CPU
docker run -d --name stress-limited --cpus=0.5 polinux/stress \
  stress --cpu 4 --timeout 60s
 
# the limited one plateaus around 50% CPU, the unlimited one near 400%
docker stats --no-stream

Reading docker stats

CPU % is normalized to one core: 100% = one full core. So a container with --cpus=0.5 tops out at ~50%, regardless of how many cores the host has.

Test the memory limit — run the container with a tiny cap and force an allocation beyond it:

docker run --rm --memory=16m polinux/stress \
  stress --vm 1 --vm-bytes 64M --timeout 30s

The process is OOM-killed by the kernel cgroup and the container exits with code 137 (SIGKILL), the same behavior explained in Optional reading.

$ docker run --rm --memory=16m polinux/stress stress --vm 1 --vm-bytes 64M --timeout 30s
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: FAIL: [1] (415) <-- worker 7 got signal 9
stress: WARN: [1] (417) now re-running with 0 workers
stress: FAIL: [1] (446) error: must be root to perform panic action

Memory limits and OOM kills

When a container crosses its --memory limit, the kernel kills the process immediately (exit 137). There is no swap-by-default warning — plan memory limits with headroom, and monitor with docker stats before assuming a value.

Clean up:

docker rm -f hello-limited stress-unlimited stress-limited

Optional reading - Linux namespaces and cgroups

Docker containers are not virtual machines — they are ordinary Linux processes wrapped in two kernel features: namespaces (isolation) and cgroups (resource limits). This chapter explains both and gives commands you can run directly on a Linux host to see them working, without Docker.

Namespaces - isolating what a process can see

A namespace makes a process believe it has its own view of a kernel resource. There are several types, and Docker combines all of them for each container:

NamespaceIsolatesExample effect inside a container
PIDProcess IDsContainer PID 1 is the app; it cannot see host processes
NETNetwork stackIts own interfaces, routes, and the -p 8080:80 port space
MNTMount pointsIts own root filesystem (the image layers)
UTSHostname and domainHOSTNAME=cafbb34739a3 seen in the MariaDB example
IPCSysV IPC, POSIX message queuesPrivate shared-memory segments
USERUID/GID mappingsRoot inside the container can map to a non-root user on the host
CGROUPcgroup root viewHides the host’s cgroup hierarchy

The key idea: nothing is emulated. The container’s processes run in kernel mode on the same kernel as the host — namespaces only change what those processes can see, while cgroups change what they can consume.

cgroups - limiting what a process can use

Control groups (cgroups, v2 in modern kernels) organize processes into hierarchies and enforce limits per group: CPU time, memory, I/O bandwidth, and PIDs. This is why docker run --memory=512m --cpus=1 works — Docker simply writes those values into the container’s cgroup files under /sys/fs/cgroup.

Division of labor

  • Namespaces answer: “What can this process see?”
  • cgroups answer: “How much of the system can this process consume?”

A container = namespaces (isolation) + cgroups (limits) + a root filesystem (image layers) + UnionFS/copy-on-write.

Hands-on tests without Docker

All tests below run on a Linux host with root (or sudo). Use a disposable VM or lab machine.

Test 1 — Inspect the cgroup hierarchy

# confirm cgroups v2 is mounted
mount | grep cgroup2
 
# the "containers" of the kernel: one directory per control group
ls /sys/fs/cgroup

Test 2 — See which namespaces a process is in

# find your shell's PID
echo $$
 
# each symlink target identifies the namespace instance
ls -l /proc/$$/ns/

Compare with another container’s process (from the host): docker top hello-world, then ls -l /proc/<PID>/ns/ — every value differs from your shell’s, proving isolation.

Test 3 — Isolate with unshare (namespaces, manually)

# new UTS + PID namespace, hostname change visible only inside
sudo unshare --uts --pid --fork /bin/sh
hostname container-lab          # changes only inside this namespace
echo $$                          # prints 1 — this shell is the new namespace's PID 1
exit
hostname                         # unchanged on the host

Test 4 — Limit memory with cgroups

# create a cgroup that allows at most 100 MB of memory
sudo mkdir /sys/fs/cgroup/limited
echo "100M" | sudo tee /sys/fs/cgroup/limited/memory.max
 
# run a shell inside it
sudo sh -c 'echo $$ > /sys/fs/cgroup/limited/cgroup.procs; exec bash'
 
# inside that shell, try to allocate more than 100 MB — the kernel kills the process
yes | tr 'y' 'a' > /dev/null     # observe process being OOM-killed
cat /sys/fs/cgroup/limited/memory.events   # shows the oom counter
sudo rmdir /sys/fs/cgroup/limited

This is exactly what happens when a container exceeds --memory: the kernel OOM-kills it and Docker reports exit code 137.

Test 5 — Observe Docker’s own namespaces and cgroups

docker run -d --name lab -p 8080:80 nginxdemos/hello
 
# the container's cgroup: memory/cpu limits live here
cat /sys/fs/cgroup/system.slice/docker-$(docker inspect -f '{{.Id}}' lab).scope/memory.max
 
# the container's namespaces differ from the host's
ls -l /proc/$(docker inspect -f '{{.State.Pid}}' lab)/ns/
 
docker rm -f lab

Tying it back to Docker

  • docker run = clone with all namespaces + create a cgroup + pivot_root into the image layers.
  • docker exec = entering an existing container’s namespaces.
  • docker stats = reading the container’s cgroup counters.
  • Exit code 137 = cgroup OOM kill (SIGKILL), as seen in Test 4.

Further reading