> For the complete documentation index, see [llms.txt](https://camelgemonion.gitbook.io/docker/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://camelgemonion.gitbook.io/docker/docker-overview/docker-wang-luo/host-mo-shi.md).

# host模式

host模式可以让docker与宿主机共享同一个网络命名空间。它将容器的网络直接绑定到宿主机的网络上，但是这样一来主机网络与容器网络之间不再具有隔离性。

我们通过启动一个nginx容器来理解什么是host模式。启动一个nginx容器将它直接绑定到宿主机的80端口。从网络的角度来看，这与nginx进程直接在 Docker 主机上运行而不是在容器中运行是相同级别的隔离。但是，所有其他方面(如存储、进程空间和用户命名空间)，nginx进程与主机是隔离的。

### 执行步骤

1. 创建和启动一个容器，然后作为独立的一个进程运行。--rm选项表示一旦容器退出或停止就移除容器。-d表示使容器在后台运行。

```
docker run --rm -d --network host --name my_nginx nginx
```

&#x20;  2\. 通过<http://localhost:80/来访问nginx。>

&#x20;  3\. 使用下面的命令检查docker的网络栈：

* 检查所有的网络接口然后没有新的接口被创建。

  ```
  ip addr show
  ```
* 使用netstat来检查哪个进程被绑定到80端口。这里需要使用sudo，因为进程由Dokcer守护进程的用户拥有否则我们无法看到它的名字或PID。

  ```
  sudo netstat -tulpn | grep :80
  ```

&#x20;   4\. 停止容器，它会被自动删除，因为我们指定了--rm选项。

```
docker container stop my_nginx
```


---

# 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:

```
GET https://camelgemonion.gitbook.io/docker/docker-overview/docker-wang-luo/host-mo-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
