Containers
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A container is not a tiny computer: it is an ordinary process that the Linux kernel walls off. Two Python apps that would clash on one server, one needing Python 3.12 and one Python 3.9, both on port 8000, run side by side in two containers. Watch namespaces give each its own files, process numbers and network, control groups cap its memory and CPU, and see why containers are lighter than virtual machines.
Two apps, one server
One server must run two Python apps. Shop needs Python 3.12, report needs Python 3.9, and both listen on port 8000. Installed side by side they clash: one Python version wins, and only one app can own port 8000.
shop needs Python 3.12, listens on :8000 report needs Python 3.9, listens on :8000 same server, no walls: one python3 on the PATH → one app breaks 2nd app on :8000 → OSError: Address already in use
اقرأ الدرس خطوة بخطوة
Two apps, one server
One server must run two Python apps. Shop needs Python 3.12, report needs Python 3.9, and both listen on port 8000. Installed side by side they clash: one Python version wins, and only one app can own port 8000.
shop needs Python 3.12, listens on :8000 report needs Python 3.9, listens on :8000 same server, no walls: one python3 on the PATH → one app breaks 2nd app on :8000 → OSError: Address already in useA container is a walled process
A container is not a tiny computer. It is an ordinary process with walls round it, built by the Linux kernel. Inside, the app sees only its own files, its own processes and its own network. Each app gets one.
Docker: "a container is simply an isolated process with all of the files it needs" walls = kernel namespaces (what it can see) limits = control groups (what it can use)docker run starts one
docker run asks the kernel for a fresh set of walls, called namespaces, then starts the app inside them. The last word, shop:1.0, names the image: a packed copy of every file the app needs.
docker run ... shop:1.0 1. make namespaces (mount, pid, net, uts, ...) 2. make a cgroup for its limits 3. set up its file tree from shop:1.0 4. start: python app.py images get their own lesson nextEach sees its own files
The mount namespace gives each container its own file tree, taken from its image. Shop finds Python 3.12 at /usr/local/bin; report finds Python 3.9 at the same path. Neither can see the other's files.
inside shop: python3 --version → 3.12 inside report: python3 --version → 3.9 same path, two different files the host's own files stay hiddenIts own process numbers
Inside, os.getpid() prints 1: the app is the first process in its own PID namespace. The host's process list shows the same process as 4211. One process, two numbers, depending on who looks.
import os, socket print("pid", os.getpid()) print("host", socket.gethostname()) inside the container prints: pid 1 host 3f2a9c1e7b04 on the host, ps shows: shop 4211 hostname defaults to the container ID the host's own PID 1 is systemdIts own network
Each container has its own network, with its own ports, so both apps can listen on port 8000. -p 8080:8000 publishes one: a request to the host's port 8080 is forwarded to port 8000 inside shop.
-p 8080:8000 host port : container port browser → host:8080 → shop:8000 (its own network namespace) report also listens on :8000, no clashLimits: control groups
Walls hide things, but they do not ration anything. Control groups (cgroups) do: --memory 256m caps shop at 256 MiB, and --cpus 0.5 gives it at most half of one CPU's time. Here it uses 120 MiB.
--memory 256m at most 256 MiB of RAM --cpus 0.5 at most 50% of one CPU shop now: 120 / 256 MiB namespaces = what it sees cgroups = how much it usesOver the limit
A bug makes shop eat memory. When it hits 256 MiB and cannot free any, the kernel's out-of-memory killer stops it. Report keeps running untouched: one app's bug cannot starve its neighbour.
shop: 120 → 256 MiB (the cap) the kernel cannot reclaim enough → out-of-memory (OOM) killer stops shop report: still running, its memory untouchedOne kernel for all
Both containers run on the host's one Linux kernel. Nothing boots when a container starts: the kernel just starts a process with walls. That is why containers start about as fast as the app itself and many fit on one server.
container start = process start no guest operating system to boot shared: the Linux kernel private: files, PIDs, network, limitsNot a virtual machine
A virtual machine is a whole operating system with its own kernel, running on a hypervisor. Its walls are stronger, but it must boot and it carries a full OS. Containers share one kernel, so they are lighter.
virtual machine container app app guest OS + its kernel (shares host kernel) hypervisor namespaces + cgroups VM: stronger walls, boots an OS container: lighter, starts like a process
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What is a container, underneath?
Docker's own docs call a container an isolated process. The kernel's namespaces decide what it can see and control groups decide how much it can use.
02Inside a container, a Python app runs print(os.getpid()) and gets 1. Why?
The first process in a new PID namespace gets PID 1. The same process has another PID, such as 4211, in the host's namespace.
03What does -p 8080:8000 do in docker run?
The format is host port : container port. Docker's docs: -p 8080:80 maps port 8080 on the Docker host to TCP port 80 in the container.
04A container started with --memory 256m tries to use more than 256 MiB and cannot free any. What happens?
The limit is a control group. When the group hits its cap and the kernel cannot reclaim enough, the OOM killer acts inside that group only.
05Which is true of containers compared with virtual machines?
A VM is an entire operating system with its own kernel on a hypervisor. Containers share one kernel: lighter and quicker, but a kernel bug can break their walls.
المسرد
- Container
- An ordinary process that the kernel isolates (namespaces) and limits (control groups), with all the files it needs taken from an image.
- Namespace
- A Linux kernel feature that gives a process its own view of one kind of resource, such as files (mount), process numbers (PID) or the network.
- Control group (cgroup)
- A Linux kernel feature that limits and counts the resources, such as memory and CPU, used by a group of processes.
- Image
- A packed, read-only copy of every file an app needs to run. A container starts from an image.
- Port publishing
- Forwarding a port on the host to a port inside a container, written host:container, as in -p 8080:8000.
- OOM killer
- The kernel's out-of-memory killer. It stops processes when memory runs out, including inside a cgroup that reaches its limit.
- Kernel
- The core of the operating system that runs processes and talks to the hardware. All containers on a host share it.
- Virtual machine (VM)
- A whole computer in software: a guest operating system with its own kernel, running on a hypervisor: the software that runs virtual machines and shares one real computer's hardware between them.
الملاحظات والمصادر
- Containers as described here are a Linux kernel feature. Docker Desktop on macOS and Windows runs them inside a small Linux virtual machine; Windows also has its own Windows containers.
- Linux has eight kinds of namespace: mount, PID, network, UTS (hostname), IPC, user, cgroup and time. The figure shows three of them: files, process numbers and network.
- The PIDs 4211 and 4388 and the container ID 3f2a9c1e7b04 are example values; your machine prints its own. A process has one PID in each PID namespace it is visible in. The app is PID 1 only when the image starts it directly (exec form, CMD ["python", "app.py"]); in shell form, /bin/sh is PID 1.
- The OOM killer runs when the cgroup reaches its memory.max limit and the kernel cannot reclaim enough memory. With --memory 256m and no --memory-swap, Docker also allows as much swap again (512 MiB in total) if the host has swap; the figure assumes a host without swap. By default Docker does not restart the killed container; a restart policy or Kubernetes can.
- Container walls are weaker than a virtual machine's, because every container shares the host kernel: a kernel bug can break them. That is one reason clouds often run containers inside VMs.
- Docker Docs: What is a container?
- Docker Docs: Docker overview (the underlying technology: namespaces)
- Docker Docs: Resource constraints (--memory, --cpus, OOM)
- Docker Docs: Port publishing and mapping
- Docker Docs: Networking overview (default hostname)
- Kubernetes docs: Containers
- Linux man-pages: namespaces(7)
- Linux man-pages: pid_namespaces(7)
- Linux man-pages: cgroups(7)
- Linux kernel docs: Control Group v2 (memory.max)
- Python docs: os.getpid
اختبار يتاح بعد الخطوة الأخيرة.