Cloud Native 26 min read

Docker Container Networking: Modes, Configuration, and Network Namespace Management

This article provides a comprehensive guide to Docker container networking, covering the default network drivers, the four Docker network modes (bridge, container, host, none), Linux network namespace commands, veth pair creation, inter‑namespace communication, and practical configuration examples such as port mapping, custom bridges, and DNS settings.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Docker Container Networking: Modes, Configuration, and Network Namespace Management

Docker容器网络

Docker在安装后自动提供三种网络(bridge、host、none),可以使用 docker network ls 命令查看。

[root@localhost ~]# docker network ls
NETWORK ID   NAME    DRIVER   SCOPE
cd97bb997b84 bridge  bridge   local
0a04824fc9b6 host    host     local
4dcb8fbdb599 none    null     local

Docker使用Linux桥接,在宿主机上创建虚拟网桥 docker0 ,容器启动时会从该网桥的子网分配 IP(Container‑IP),并将桥接设为默认网关,实现同宿主机内容器之间的直接通信。

Docker的四种网络模式

bridge模式

Docker进程启动时在宿主机上创建 docker0 虚拟网桥,容器通过 veth 对接入该桥。默认网络模式,不需要显式指定 --network 参数。

使用 docker run -p 时,Docker 在 iptables 中添加 DNAT 规则实现端口转发,可通过 iptables -t nat -vnL 查看。

container模式

容器共享已有容器的 Network Namespace ,不创建独立网卡,IP、端口范围相同,仅网络共享,文件系统仍隔离。

host模式

容器直接使用宿主机的网络命名空间,拥有宿主机的 IP 与端口,网络性能最佳,但端口冲突风险高,网络隔离性差。

none模式

容器拥有独立的 Network Namespace ,但不进行任何网络配置,仅保留 lo 回环接口,适用于需要完全隔离网络的场景。

Linux内核实现网络命名空间的创建与管理

使用 ip netns 命令创建、列出、删除和操作网络命名空间。

# ip netns add ns0
# ip netns list
ns0

创建的命名空间会出现在 /var/run/netns/ 目录。

在命名空间中执行命令使用 ip netns exec 。

# ip netns exec ns0 ip addr
1: lo: <LOOPBACK> ...

默认只创建 lo 回环接口,需要手动启用:

# ip netns exec ns0 ip link set lo up
# ip netns exec ns0 ping 127.0.0.1

veth pair的创建与跨命名空间通信

创建一对虚拟以太网接口:

# ip link add type veth
4: veth0@veth1: ...
5: veth1@veth0: ...

将 veth0 加入 ns0 , veth1 加入 ns1 ,并为其分配 IP:

# ip netns exec ns0 ip link set veth0 up
# ip netns exec ns0 ip addr add 192.0.0.1/24 dev veth0
# ip netns exec ns1 ip link set veth1 up
# ip netns exec ns1 ip addr add 192.0.0.2/24 dev veth1

随后可以在两个命名空间之间 ping 通:

# ip netns exec ns1 ping 192.0.0.1
# ip netns exec ns0 ping 192.0.0.2

容器常用网络操作示例

查看容器主机名: # docker run -it --name t1 --network bridge --rm busybox / # hostname 48cb45a0b2e7

注入自定义主机名: # docker run -it --name t1 --network bridge --hostname ljl --rm busybox / # hostname ljl

手动指定 DNS: # docker run -it --name t1 --network bridge --dns 114.114.114.114 --rm busybox / # cat /etc/resolv.conf nameserver 114.114.114.114

端口映射示例: # docker run -dit --name web1 -p 192.168.203.138::80 httpd # docker port web1 80/tcp -> 192.168.203.138:49153

自定义桥接网络

编辑 /etc/docker/daemon.json 设置自定义 bridge IP 段:

{
  "registry-mirrors": ["https://4hygggbu.mirror.aliyuncs.com/"],
  "bip": "192.168.1.5/24"
}

创建自定义网络并使用:

# docker network create ljl -d bridge
# docker run -it --name b1 --network ljl busybox
/ # ifconfig

通过以上命令可以灵活管理 Docker 容器的网络模式、IP 分配、端口映射以及跨命名空间通信,满足不同的开发、测试和生产环境需求。

DockerIPTablesContainer NetworkingNetwork NamespaceVethBridge ModeHost Mode
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.