0Pricing
Docker & Kubernetes for Developers · 课时

配置容器网络

创建和管理自定义 Docker 网络,以有效隔离并连接您的服务。

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

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

Why Custom Docker Networks?

Docker provides default networks, but for complex applications, custom networks are key. They offer better isolation and controlled communication between your services.

Think of them as private virtual LANs just for your containers, giving you more control over how they interact.

Default Networks: Bridge, Host, None

When you run a container without specifying a network, it usually attaches to the bridge network by default. This allows containers to talk to each other and the host.

  • Bridge: Default, isolated.
  • Host: Container shares host's network stack.
  • None: Isolated, no network access.

While useful, the default bridge network can be limiting for multi-service applications.

Crafting Your Own Network

To create a custom network, you use the docker network create command. The most common type is a bridge network, which acts like a virtual switch.

It provides better isolation and easier service discovery by name for containers connected to it.

docker network create my_app_network

Running Containers on Custom Networks

You can connect a new container to a custom network right when you start it using the --network flag. This is the most common way to get your services talking from the start.

Containers on the same custom bridge network can communicate with each other using their container names as hostnames.

docker run -d --name web_server --network my_app_network nginx:latest

Demo: Web Server on Custom Network

Let's create a custom network and run an Nginx web server on it. We'll publish port 8080 to access it from our host.

First, create the network, then run the container:

docker network create my_web_network
docker run -d --name my_nginx --network my_web_network -p 8080:80 nginx:latest

Connecting Existing Containers

What if you have a container already running and want to connect it to a new custom network? You can use the docker network connect command.

This is useful when you want to dynamically add a container to a network without restarting it.

docker network connect my_app_network existing_container_name

Demo: Add to an Existing Network

Let's say you have a simple Alpine container running. We'll connect it to our previously created my_web_network to allow it to communicate with my_nginx.

First, run a container, then connect it:

docker run -d --name my_alpine alpine sleep 3600
docker network connect my_web_network my_alpine

Peeking into Network Details

To understand what's happening inside your networks, use docker network inspect. This command shows detailed information, including connected containers and their IP addresses.

It's crucial for debugging network issues and verifying connections.

docker network inspect my_web_network

Cleaning Up Your Networks

When containers are no longer needed on a network, you can disconnect them. When a network is no longer in use, it's good practice to remove it to keep your Docker environment tidy.

  • docker network disconnect [network] [container]
  • docker network rm [network_name]

Remember to stop and remove all connected containers before removing a network.

docker network disconnect my_web_network my_alpine
docker network rm my_web_network

Network Configuration Quiz

Let's check your understanding of configuring Docker networks.

Recap: Custom Network Power

In this lesson, you learned how to create and manage custom Docker networks. These networks provide essential isolation and allow your containers to communicate securely by name.

  • Used docker network create to make custom networks.
  • Connected containers with --network or docker network connect.
  • Inspected networks using docker network inspect.
  • Cleaned up with docker network disconnect and docker network rm.

Mastering custom networks is vital for building robust multi-container applications.

常见问题解答

「配置容器网络」课时是免费的吗?

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

「配置容器网络」这节课中我会学到什么?

创建和管理自定义 Docker 网络,以有效隔离并连接您的服务。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

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

「配置容器网络」课时需要多长时间?

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

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

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

此课程中的所有课时

  1. 了解 Docker 网络类型
  2. 配置容器网络
  3. 使用卷实现数据持久化
  4. 使用 Docker Compose 网络连接容器
← 返回 Docker & Kubernetes for Developers