Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Docker - Fundamental

Back


Fundamental

Docker Engine Architecture

Docker Client (CLI / API)
        ↓
Docker Daemon (dockerd)
        ↓
Container Runtime (containerd → runc)
        ↓
Containers (isolated processes)

Components

  1. Docker Client
    • The docker CLI you use (docker build, docker run)
    • Sends commands via REST API
  2. Docker Daemon (dockerd)
    • The brain of Docker
    • Responsible for:
      • building images
      • managing containers
      • handling networks & volumes
  3. Container Runtime
    • containerd: manages container lifecycle
    • runc: actually creates the container using Linux kernel features
  4. Containers
    • Lightweight, isolated processes (not full VMs)

Container runtime architecture

  1. Namespaces (isolation)
    • Process isolation (PID)
    • Network isolation
    • File system isolation
    • User isolation
  2. cgroups (resource control)
    • Limit CPU
    • Limit memory
    • Limit disk I/O

Example: docker run -d nginx

Docker does:

  1. CLI sends request
  2. Daemon checks image locally
    • If not found → pulls from registry
  3. Creates container:
    • adds read-write layer
    • sets up namespaces:
      • PID namespace: Process identifiers and capabilities
      • IPC namespace: Process communication over shared memory
      • UTS namespace: Host and domain name
      • NET namespace: Network access and structure
      • USR namespace: User names and identifiers
      • MNT namespace: Filesystem access and structure
    • applies cgroups
    • configures network
  4. runc starts the process

Implement Container

Feature: linux namespace


# ubuntu
docker run -d --name busybox busybox sleep 300
# 8ef189a2907c47d349cc5c52f0bae3d8c7a611fc9f562e994a99eb83752c82fe

# host
pstree
# systemd─┬─2*[agetty]
#         ├─containerd───14*[{containerd}]
#         ├─containerd-shim─┬─sleep
#         │                 └─10*[{containerd-shim}]

ps aux | grep sleep
# root         681  0.0  0.0   4436  1716 ?        Ss   20:16   0:00 sleep 300

ip a
# 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
#     link/ether 06:7b:a3:c8:28:94 brd ff:ff:ff:ff:ff:ff
#     inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
#        valid_lft forever preferred_lft forever
#     inet6 fe80::47b:a3ff:fec8:2894/64 scope link
#        valid_lft forever preferred_lft forever

# in container
docker exec -it busybox sh
# / #

ps -a
# PID   USER     TIME  COMMAND
#     1 root      0:00 sleep 300
#     7 root      0:00 sh
#    15 root      0:00 ps -a

ip a
# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
#     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
#     inet 127.0.0.1/8 scope host lo
#        valid_lft forever preferred_lft forever
#     inet6 ::1/128 scope host
#        valid_lft forever preferred_lft forever
# 2: eth0@if4: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
#     link/ether ea:13:d6:12:07:d0 brd ff:ff:ff:ff:ff:ff
#     inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
#        valid_lft forever preferred_lft forever

Linux kernel feature: cgroups


Feature: syscall


Feature: Linux capabilities

CMD DESC
getcap FILE Displays the capabilities assigned to files (file capabilities).
setcap FILE Assigns capabilities to an executable file.
getpcaps Shows the capabilities of a currently running process.
docker run -d --name busybox busybox sleep 300

ps -aux | grep sleep
# root        1569  0.0  0.0   4436  1652 ?        Ss   21:33   0:00 sleep 300

getpcaps 1569
# 1569: cap_chown,cap_dac_override,cap_fowner,cap_fsetid,cap_kill,cap_setgid,cap_setuid,cap_setpcap,cap_net_bind_service,cap_net_raw,cap_sys_chroot,cap_mknod,cap_audit_write,cap_setfcap=ep

Feature: Seccomp (secure computing mode)



Common Admin Commands

Command Description
docker system info Display system-wide information
docker system df Show disk usage (images, containers, volumes)
docker system prune Remove unused data (containers, networks, dangling images)
docker system prune -a Remove all unused images (not just dangling)
docker system prune -a --volumes Aggressive cleanup (includes volumes ⚠️ data loss risk)
docker volume ls List volumes
docker volume rm <volume> Remove a volume
docker volume prune Remove unused volumes
docker network ls List networks
docker network prune Remove unused networks
docker inspect <container/image> Show detailed metadata (JSON)
docker logs <container> View container logs
docker exec -it <container> /bin/bash Enter a running container
docker top <container> Show running processes inside container
docker stats Real-time CPU/memory usage of containers

Docker orchestration


Rootless Docker

Feature Root Docker Rootless Docker
Daemon privilege root non-root
Security risk higher lower
Performance native slightly slower
Networking full limited
Port binding any port >1024 only