Software Containerisation

Table of Contents

Introduction

What are containers?

Every time you create a new VM, you carry over a guest OS, which (a) consumes a lot of resources, and (b) is another attack surface for hackers.

In comparison, a container can be much smaller and lighter.

Containers vs virtual machines:

Containers vs VMs

What features in Linux enable containers?

  1. Control groups (cgroups): used to limit usage of resources by processes
    • OOM killer kills processes when memory gets exhausted
    • cgroups can be controlled with filesystem at /sys/fs/cgroup
    • every PID represented once in each hierarchy
  2. Namespaces: mechanisms allowing separation/isolation of resources that would otherwise be in global scope. can see them in /proc/PID/ns, or with lsns (in util-linux package)
    • mount namespace
    • interprocess communication (message queues, semaphores, shared mem)
    • unix time sharing (isolates hostname and NIS domain name of a process)
    • process ID (isolates process ID number space)
    • network
    • user ID
    • control group
    • time
  3. Union filesystem: create union of the contents of different filesystems. used to isolate changes to container filesystem in its own layer, and avoid duplicating complete set of files every time you run an image as a new container.

Kubernetes

Distributed system, made of group of master nodes and one or more worker nodes, which run containers grouped in pods.

OCI: Open Container Initiative

Provides two specifications:

Container runtimes

Container Runtime Interface (CRI): lets a kubelet use different container runtimes without having to recompile.

Example runtimes: containerd, CRI-O, docker

Docker

Ecosystem

Engine architecture

Diagram of Docker Engine parts

Image architecture

Docker images have layers, and are built from declarative Dockerfiles. Most commands in Dockerfile result in new layer being added. After docker build the image, all layers are read-only. When docker runs the image, it adds a new writable layer, and any created/modified files are lost when the container stops.

Layers in a Docker image

Dockerfile:

Copy-on-Write (CoW) strategy

Docker-compose

Lets you orchestrate multiple containers, including setup of networking between them. Containers are described in docker-compose file, using YAML.

Persistent storage

You have a few options:

tmpfs is not persistent, only stored in memory. useful to temporarily store sensitive files.

Networking

By default, docker creates 3 networks: bridge, host, none. When you start a container, it’s attached to default bridge network.

Bridge default network:

User bridge networks:

Connect an existing container to a network with docker network connect <network-name> <container-name>

When containers are part of same user defined bridge network, they can refer to each other both via IP and via container name (“automatic service discovery”).

You can connect a container to the host network, at which point it’s not isolated from the host network.