> For the complete documentation index, see [llms.txt](https://philipzheng.gitbook.io/docker_practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://philipzheng.gitbook.io/docker_practice/security/daemon_sec.md).

# 伺服端防護

執行一個容器或應用程式的核心是透過 Docker 服務端。Docker 服務的執行目前需要 root 權限，因此其安全性十分關鍵。

首先，確保只有可信的使用者才可以存取 Docker 服務。Docker 允許使用者在主機和容器間共享檔案夾，同時不需要限制容器的存取權限，這就容易讓容器突破資源限制。例如，惡意使用者啟動容器的時候將主機的根目錄`/`映射到容器的 `/host` 目錄中，那麽容器理論上就可以對主機的檔案系統進行任意修改了。這聽起來很瘋狂？但是事實上幾乎所有虛擬化系統都允許類似的資源共享，而沒法禁止使用者共享主機根檔案系統到虛擬機系統。

這將會造成很嚴重的安全後果。因此，當提供容器建立服務時（例如透過一個 web 伺服器），要更加注意進行參數的安全檢查，防止惡意的使用者用特定參數來建立一些破壞性的容器

為了加強對服務端的保護，Docker 的 REST API（客戶端用來跟服務端通訊）在 0.5.2 之後使用本地的 Unix socket機制替代了原先綁定在 127.0.0.1 上的 TCP socket，因為後者容易遭受跨站腳本攻擊。現在使用者使用 Unix 權限檢查來加強socket的存取安全。

使用者仍可以利用 HTTP 提供 REST API 存取。建議使用安全機制，確保只有可信的網路或 VPN，或憑證保護機制（例如受保護的 stunnel 和 SSL 認證）下的存取可以進行。此外，還可以使用 HTTPS 和憑證來加強保護。

最近改進的 Linux 命名空間機制將可以實作使用非 root 使用者來執行全功能的容器。這將從根本上解決了容器和主機之間共享檔案系統而引起的安全問題。

終極目標是改進 2 個重要的安全特性：

* 將容器的 root 使用者映射到本地主機上的非 root 使用者，減輕容器和主機之間因權限提升而引起的安全問題；
* 允許 Docker 服務端在非 root 權限下執行，利用安全可靠的子行程來代理執行需要特權權限的操作。這些子行程將只允許在限定範圍內進行操作，例如僅僅負責虛擬網路設定或檔案系統管理、設定操作等。

最後，建議採用專用的伺服器來執行 Docker 和相關的管理服務（例如管理服務比如 ssh 監控和程式監控、管理工具 nrpe、collectd 等）。其它的業務服務都放到容器中去執行。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://philipzheng.gitbook.io/docker_practice/security/daemon_sec.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
