0Pricing
Linux Networking & TCP/IP for Developers · 课时

Docker 网络模式

探索 Docker 的各种网络驱动程序(桥接、主机、覆盖),了解它们对容器通信的影响

Docker 网络模式 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Containers Need to Talk!

Docker containers are isolated by default, but they often need to communicate with each other or the outside world. Docker provides various network drivers to manage this.

These drivers define how containers connect to each other and to the host machine's network, ensuring proper communication and isolation.

The Standard: Bridge Network

When you don't specify a network, Docker automatically uses the default bridge network. Think of it as a virtual switch that Docker creates on your host machine.

Each container connected to this bridge gets its own IP address and can communicate with other containers on the same bridge using their IP addresses.

Default Bridge in Action

Let's inspect the default bridge network and see a container connected to it. This demonstrates how Docker assigns IP addresses within this virtual network.

Run these commands to observe the default bridge and a container's IP.

docker network ls
docker run -d --name mynginx nginx
docker inspect --format='{{.NetworkSettings.IPAddress}}' mynginx
docker stop mynginx
docker rm mynginx

Sharing Host's Network

The host network mode removes network isolation between the container and the host. The container directly uses the host's network stack.

This means the container's ports are directly exposed on the host's IP address without any port mapping. It offers high performance but significantly reduces isolation.

Host Mode Demo

When a container runs in host mode, it effectively becomes another process on the host's network. Its network interfaces and IP addresses will mirror those of the host machine.

Run this command to see a container's network details in host mode and compare it to your host's network configuration.

docker run --rm --network host alpine ip a
# Compare with your host's `ip a` output!

Spanning Multiple Hosts: Overlay

Overlay networks are designed for multi-host container communication, typically used in Docker Swarm or Kubernetes clusters.

They allow containers on different physical or virtual machines to communicate seamlessly as if they were on the same local network. This is vital for distributed applications.

Customizing Your Bridges

While the default bridge is useful, user-defined bridge networks are best practice. They offer several advantages over the default bridge:

  • Better Isolation: Keeps your application's network separate.
  • Automatic DNS: Containers can find each other by name, not just IP!
  • Portability: Easier to move applications between hosts.

Creating a Custom Bridge

Let's create our own custom bridge network and connect containers to it. Observe how containers on the same user-defined bridge can resolve each other by their container names.

This makes inter-container communication much simpler and more robust.

docker network create myapp_network
docker run -d --name myapp_server --network myapp_network alpine/git sh -c "while true; do echo 'Hello from server!' | nc -l -p 8080; done"
docker run --rm --network myapp_network alpine/git sh -c "apk add --no-cache netcat-openbsd && sleep 2 && nc myapp_server 8080"
docker stop myapp_server
docker rm myapp_server
docker network rm myapp_network

Containers Talking by Name

In the previous example, the client container successfully connected to myapp_server using its container name. This is a key advantage of user-defined bridge networks.

Docker's built-in DNS service automatically resolves container names to their IP addresses within the custom network, simplifying service discovery.

Test Your Knowledge!

Which of the following statements about Docker network modes are TRUE?

Docker Networks: A Quick Review

We've explored key Docker network modes that enable container communication:

  • Bridge: The default, for single-host container communication.
  • Host: Container shares the host's network stack for high performance but less isolation.
  • Overlay: For multi-host communication in clustered environments like Docker Swarm.
  • User-Defined Bridge: Best practice for custom app networks, offering isolation and automatic DNS resolution by name.

Understanding these modes is vital for designing robust and scalable containerized applications.

常见问题解答

「Docker 网络模式」课时是免费的吗?

是的 — 「Docker 网络模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

「Docker 网络模式」这节课中我会学到什么?

探索 Docker 的各种网络驱动程序(桥接、主机、覆盖),了解它们对容器通信的影响 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Networking & TCP/IP for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「Docker 网络模式」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?

能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Docker 网络模式
  2. Kubernetes 网络基础
  3. 容器中的网络策略
  4. Kubernetes 中的服务发现与 DNS
← 返回 Linux Networking & TCP/IP for Developers