ENع
→Containers
الدرس 07 · الحاويات وKubernetes

Containers

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 7 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. A 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)
  3. 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 next
  4. Each 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 hidden
  5. Its 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 systemd
  6. Its 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 clash
  7. Limits: 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 uses
  8. Over 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 untouched
  9. One 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, limits
  10. Not 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

اختبار يتاح بعد الخطوة الأخيرة.