跳到主要內容

Docker 容器為什麼不像一台完整電腦?從共用核心看懂隔離 / Why a Docker Container Is Not a Whole Computer

假設你準備接手一個小網站,筆電上已經裝了一套 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]

兩個 Linux 容器共用同一核心;兩台虛擬機各有客體核心 / Two Linux containers share one kernel; each VM has a guest kernel

圖一|先找核心在哪一層,比只看方框更容易理解差異。原創簡化示意圖,非產品截圖。 / 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]

命名空間管理程序所見的資源範圍,控制群組管理程序群組的資源分配 / Namespaces shape resource visibility; cgroups manage resources for process groups

圖二|分開理解「看到什麼」和「資源如何分配」,查問題時比較有方向。原創概念圖。 / 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

留言

這個網誌中的熱門文章

kimiflare:將 Moonshot Kimi-K2.6 直接部署於 Cloudflare 的終端 AI 編碼助手 | kimiflare: A Terminal Coding Agent Powered by Moonshot Kimi-K2.6 on Cloudflare

無需中繼,在你的 Cloudflare 帳戶中直接運行強大的 Kimi 終端編碼智能體。 | No middleman, run the powerful Kimi terminal coding agent directly on your own Cloudflare account. 🔎 工具速覽 / AT A GLANCE Category AI Coding Agent / Developer Tool Pricing Free (Open-weight model / Cloudflare Workers AI pricing) BestFor Developers seeking a private, high-context terminal AI assistant with direct infrastructure control. GitHub Stars ⭐ 36 🚀 引言 / Introduction kimiflare 是一款為開發者量身打造的終端編碼智能體,它將 Moonshot 的 1 兆參數開源模型 Kimi-K2.6 與 Cloudflare Workers AI 完美結合。不同於傳統 AI 工具,kimiflare 讓流量直接在你的 Cloudflare 帳戶中流動,徹底擺脫第三方代理或 AI 閘道的束縛,實現最高等級的控制力與隱私。 | kimiflare is a terminal coding agent tailored for developers, seamlessly combining Moonshot's 1T-parameter open-source model Kimi-K2.6 with Cloudflare Workers AI. Unlike traditional AI tools, kimiflare routes traffic directly through your Cloudflare account, eliminating the n...

text-to-cad: 讓 AI 驅動的參數化 CAD 建模成為現實 / text-to-cad: Bringing AI-Driven Parametric CAD Modeling to Reality

一個讓 AI 代理能生成、預覽並迭代 3D CAD 模型的開源框架 / An open-source harness for AI agents to generate, preview, and iterate 3D CAD models. 🔎 工具速覽 / AT A GLANCE Category AI-Powered CAD Tooling / AI 驅動 CAD 工具 Pricing Free (Open Source) / 免費(開源) BestFor Robotics engineers and product designers using AI coding agents / 使用 AI 程式碼代理的機器人工程師與產品設計師 GitHub Stars ⭐ 93 🚀 引言 / Introduction text-to-cad 是一個強大的開源框架,旨在填補大型語言模型(LLM)與專業 3D 建模之間的鴻溝。它允許開發者利用 Claude Code 或 Codex 等 AI 代理,透過程式碼生成精確的參數化幾何模型,並提供即時預覽與迭代能力。 text-to-cad is a powerful open-source harness designed to bridge the gap between Large Language Models (LLMs) and professional 3D modeling. It enables developers to leverage AI agents like Claude Code or Codex to generate precise parametric geometry via code, providing real-time preview and iteration capabilities. 🛠️ 核心功能 / Key Features 與 Codex 或 Claude Code 等代理...

Orca Skills: 開源賦能,加速雲安全工作流

加速雲安全工作流 - Orca Skills 整合多種工具與插件,打造高效安全環境 | Accelerate Cloud Security Workflows with Orca Skills, a Suite of Tools and Plugins 🔎 工具速覽 / AT A GLANCE Category 开源安全工具 Pricing 开源免费 BestFor 雲安全团队、DevSecOps工程师 GitHub Stars ⭐ 33 🚀 引言 / Introduction 在雲端環境中,確保安全不僅是一種技術挑戰,更是對工作效率的一場考驗。Orca Skills 是一款專為加速 Orca 云平台安全工作流而設計的开源插件工具包。透過整合多種工具和插件,幫助我們在複雜的安全環境下保持高效率。 🛠️ 核心功能 / Key Features Orca Skills 提供了一系列功能強大的安全工作流工具,包括警報處理、影響分析、配置源頭追溯等。這些工具不僅能夠幫助我們快速定位問題,還能確保在解決問題時避免造成不必要的後續問題。 💡 技術亮點 / Tech Highlights Orca Skills 真正的亮點在于其開放性與兼容性。它不僅支持Anthropic Claude Code、Cursor 和 Codex,還能夠與 MCP(Orca Security)集成,為安全團隊提供全面的支持。 📦 快速上手 / Quick Start 要快速開始使用 Orca Skills,首先需要安裝所需環境和工具。接下來,配置 MCP 以實現與 Orca Cloud Platform 的集成。最後,測試各個技能插件以確保其正常運行。 中英文對照: 要快速開始使用 Orca Skills 首先需要安裝所需環境和工具 接著配置 MCP 以實現與 Orca 云平台的集成 最...