Technical skill

Linux containers

One host, separate workspaces

A container lets a program run in a more isolated workspace on a Linux computer. One container may see its own process list, network, and mounted folders while another sees different ones. Both still use the Linux kernel of the computer running them (the host), so a container is not a complete separate computer or virtual machine.

Two separate application spaces run on one shared computer.

Each app has its own working space, while both run on the same computer.

A container runtime sets up and starts the processes. It can give them separate views of selected resources and set limits on memory or processor time. A container image is a packaged starting point with a filesystem and metadata. The running container consists of the processes and settings created from that image, with writable state where the runtime allows it.

Look at a container before changing it

Docker provides commands for checking what is running and how it is configured:

docker ps
docker inspect web
docker stats web

docker ps lists running containers, docker inspect reports the configuration and current state of one, and docker stats shows resource use. If an application cannot reach a database, inspect the container's network view and any port publishing. A container does not automatically share the host's network view; the configured network mode and host kernel features affect what it can see.

This example starts a small web server container:

docker run -d --name web --memory=256m --cpus=0.5 -p 8080:80 nginx

The command runs it in the background, calls it web, sets a 256 MB memory limit, caps CPU use at half of one CPU's capacity, and connects port 8080 on the host to port 80 in the container. Check for a port conflict and enough host capacity, and make sure those limits fit the service. Docker options can vary by version, so check the installed version before relying on a command.

How Linux separates containers

Linux namespaces give a process a selected view of system resources. A PID namespace changes which process IDs it can see. A network namespace gives it a separate network stack view, and a mount namespace gives it a separate view of mounted filesystems. User namespaces can map user and group IDs. Linux has other namespace types; the runtime chooses which ones to use.

Control groups, usually called cgroups, group processes so Linux can account for or limit resources such as memory and CPU when the needed controllers are enabled and configured. Namespaces affect what a process can see; cgroups affect resource accounting and limits. A container can hit its cgroup limit while the host still has memory or CPU available, so check the process's cgroup context when diagnosing a resource problem.

These boundaries do not make a workload safe by themselves. Capabilities, mounted filesystems, device access, seccomp filters, Linux security modules, user mappings, and other runtime settings affect what its processes can do. A privileged container or a sensitive host folder mounted inside it can expose the host. Review the complete runtime settings, including mounts and privileges, and check the documentation for the running system.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful