假設你準備接手一個小網站,筆電上已經裝了一套 Python,專案卻需要另一個版本。光想到調整環境,就有點想先去倒杯水。容器吸引人的地方,正是讓應用程式連同需要的檔案一起安排好;不過,這個看起來獨立的小空間,究竟獨立到哪裡?
Imagine taking over a small website. Your laptop already has Python installed, but the project needs another version. Just thinking about adjusting the environment might send you off for a glass of water. A container helps by packaging an application with the files it needs. But how independent is this seemingly separate space?
先看看,箱子底下是什麼 / Look beneath the boxes
在一般 Linux 容器的情境裡,容器內可以有自己的應用程式、函式庫與檔案配置,執行時卻共用承載它的 Linux 核心。虛擬機器則可以有自己的客體核心。把兩者都畫成一個方框很方便,卻容易漏掉這個關鍵差別。[1]
In a typical Linux container setup, each container can have its own application, libraries, and filesystem layout while sharing the Linux kernel that runs it. A virtual machine can have its own guest kernel. Drawing both as simple boxes is convenient, but it hides this essential difference. [1]
圖一|先找核心在哪一層,比只看方框更容易理解差異。原創簡化示意圖,非產品截圖。 / Figure 1 | Locate the kernel before comparing the boxes. Original simplified diagram, not a product screenshot.
這也讓「容器裡有一套 Linux」需要多說一句:你看到的可能是某個發行版的使用者空間,不代表裡面另開了一顆核心。本文先限定在 Linux 容器的結構,不把這張簡圖直接套到每一種桌面產品或執行環境。[1]
That is why saying “there is Linux inside the container” needs some care. You may be looking at a distribution’s userspace rather than a separate running kernel. This article focuses on Linux containers; the simplified diagram does not describe every desktop product or runtime configuration. [1]
看得到的範圍,與用得到的資源 / Visibility and resources
既然共用核心,為什麼容器看起來各過各的生活?其中一項機制是命名空間(namespace):依不同種類,讓程序看到自己的程序編號、掛載點或網路資源。它像替觀察範圍畫出邊界;這個比喻說的是視野,不能直接解讀成效能配額。[2]
If containers share a kernel, why do they seem to live separately? Namespaces are part of the answer. Different namespace types give processes separate views of resources such as process IDs, mount points, or networking. Think of them as boundaries around what a process can see; that analogy does not imply a performance quota. [2]
資源分配則要看另一項機制:控制群組(cgroup)。Linux 的 cgroup 可以把程序組織起來,由控制器管理資源。設定與啟用哪些限制,仍取決於執行環境;不能因為程式放進容器,就認定記憶體與處理器使用量已經受到合適限制。[3]
Resource management involves another mechanism: control groups, or cgroups. Linux can organize processes into groups whose resources are managed by controllers. Which limits are configured and enabled depends on the runtime environment. Putting an application in a container does not by itself establish appropriate memory or CPU limits. [3]
圖二|分開理解「看到什麼」和「資源如何分配」,查問題時比較有方向。原創概念圖。 / Figure 2 | Separating visibility from resource management helps frame troubleshooting. Original conceptual diagram.
留一個能自己看的小步驟 / One small observation to try
若你手邊有可以操作的 Linux 終端機,可以輸入下面這行,看看目前 shell 的命名空間連結。它是唯讀觀察,不會替你建立容器,也不會調整系統限制。這裡提供的是操作建議,本文沒有實際執行或附上測試輸出。[2]
If you have access to a Linux terminal, the command below lists namespace links for the current shell. It is a read-only observation: it neither creates a container nor changes system limits. This is a suggested exercise; it was not executed for this article, and no test output is presented. [2]
ls -l /proc/$$/ns
看見清單後,先不用急著背下所有名稱。挑一個你在意的問題,例如「這個程序看到哪一組網路資源」,再回頭查對應的命名空間。這份清單本身也不能證明容器的隔離完整或安全;它只是把抽象概念拉近一點的起點。[2]
You do not need to memorize every name. Start with one question, such as which network resources a process can see, then read about the corresponding namespace. The list alone does not establish complete or secure container isolation. It is simply a starting point for making the concept more tangible. [2]
回到原先那個版本不同的小網站,容器讓環境安排多了一種做法。下次再看到那個整齊的方框,可以多問兩句:核心在哪裡?邊界又是由誰設定?弄清楚這兩件事,再往下讀部署文件,腳步就踏實一些。
Back at the small website with a different Python version, containers offer another way to arrange its environment. The next time you see a tidy box in a diagram, ask two questions: where is the kernel, and what configures the boundaries? Those questions provide a firmer footing for reading deployment documentation.
資料與圖片說明 / Sources and illustrations
查核日期:2026-10-30。本文依官方文件與 Linux 維護者手冊整理,開頭為假設情境;未聲稱親身測試。兩張圖為本篇原創 SVG 結構示意(以 PNG 呈現),省略部分執行環境細節,非實際介面或實測截圖。 / Checked on 2026-10-30. Based on official documentation and the Linux maintainer manual. The opening is hypothetical; no hands-on test is claimed. Both original SVG diagrams, presented as PNG images, simplify the architecture and are not screenshots.
1. Docker Docs:What is a container?
2. Linux man-pages:namespaces(7)
3. Linux Kernel:Control Group v2
留言
張貼留言