了解 Docker 网络类型
考察 bridge、host 和 overlay 等不同 Docker 网络驱动,以及它们在容器通信中的适用场景。
了解 Docker 网络类型 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Intro to Docker Networks
Welcome to Docker networking! Containers need to talk to each other and the outside world. Docker networks make this possible.
Understanding network types is key to building robust and scalable applications. Let's dive in!
The Default Bridge Network
When you first install Docker, it creates a default bridge network named bridge. Unless specified, all new containers connect to this network.
- Containers on the same default bridge can communicate via IP address.
- They are isolated from the host machine's network.
Inspecting Default Bridge
You can see the default bridge network and its details using Docker commands. Try running these:
docker network ls
docker network inspect bridgeCustom Bridge Networks
While the default bridge works, custom bridge networks offer significant advantages:
- Isolation: Better segmentation for different applications.
- DNS Resolution: Containers can communicate by name, not just IP.
- Portability: Easier to manage complex setups.
Creating a Custom Bridge Network
Let's create our own custom bridge network. This network will be isolated and provide DNS resolution for containers connected to it.
docker network create my-custom-app-netConnect Containers to Custom Network
Now, let's run two containers on our my-custom-app-net and see them talk to each other using their names. This is a huge benefit!
docker run -d --name web1 --network my-custom-app-net nginx
docker run -it --rm --network my-custom-app-net alpine ping -c 2 web1The 'Host' Network Driver
The host network driver is different. It removes network isolation between the container and the Docker host.
A container using the host network shares the host's network stack directly. This means:
- No port mapping is needed.
- The container uses the host's IP address.
Host Network Use Cases
The host network can be useful for specific scenarios:
- Performance: Minimizes networking overhead.
- Monitoring: Accessing host-level network interfaces directly.
However, it reduces isolation and can lead to port conflicts if not managed carefully.
docker run -it --rm --network host alpine shOverlay Networks for Swarm
When you start dealing with multiple Docker hosts (machines), you need a way for containers on different hosts to communicate. This is where overlay networks come in.
Overlay networks are crucial for Docker Swarm, allowing services to communicate seamlessly across an entire cluster.
Overlay Network Advantages
Overlay networks provide:
- Multi-host communication: Containers on different physical machines can talk.
- Service Discovery: Integrated DNS for services across the swarm.
- Load Balancing: Distributes traffic among service replicas.
They abstract away the underlying physical network complexity.
Network Type Quiz
Test your knowledge of Docker network types!
Network Types Recap
Great job! You've learned about the main Docker network types:
- Bridge (Default & Custom): For single-host container communication. Custom bridges add isolation and DNS.
- Host: For containers that need direct access to the host's network stack.
- Overlay: For multi-host container communication in a Docker Swarm.
Choosing the right network type is vital for your application's architecture. Next, we'll learn how to configure them!
常见问题解答
「了解 Docker 网络类型」课时是免费的吗?
是的 — 「了解 Docker 网络类型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。
「了解 Docker 网络类型」这节课中我会学到什么?
考察 bridge、host 和 overlay 等不同 Docker 网络驱动,以及它们在容器通信中的适用场景。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Docker & Kubernetes for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「了解 Docker 网络类型」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?
能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 了解 Docker 网络类型
- 配置容器网络
- 使用卷实现数据持久化
- 使用 Docker Compose 网络连接容器