docker 容器网络基础

Docker网络详解
本文深入解析Docker的网络模式,包括host、none、bridge及overlay网络的特点与应用场景。阐述了在不同环境下(单host、swarm、compose)容器默认使用的网络类型,并介绍了如何通过Docker命令或docker-compose文件配置自定义网络。

========================

docker缺省自带的网络

========================

host 网络, This enables a container to attach to your host’s network (meaning the configuration inside the container matches the configuration outside the container).

none 网络, This offers a container-specific network stack that lacks a network interface. This container only has a local loopback interface (i.e., no external network interface).

bridge 网络, All Docker installations represent the docker0 network with bridge; Docker connects to bridge by default. Run ifconfig on the Linux host to view the bridge network.

docker network inspect bridge  # find the specific network details
docker network ls # list all networks

========================

容器缺省使用的网络

========================

1.  在单 host 情况下, docker run 启动容器会加入docker系统自带的 bridge 网络,  即进入网桥模式, 这时docker daemon就会扮演 DHCP 服务器, 为容器分配一个和 docker0 同网段的ip, 并连接到 docker0 . 所以在容器中, 总是可以通过 docker0 的ip 访问到 host 主机. 

2. 在 swarm 下, 缺省使用 overlay 网络, 适合于多 host 的容器集群. 

3. 在compose 下, 缺省为 docker-compose.yaml 建立一个 brige 网络, 其中的所有服务都会接入该 bridge 网络, 网络名称由 docker-compose 命令的  -p 或 –project-name 参数指定. 

========================

使用网络

========================

1. 可以使用 docker network 命令增加网络

2. 可以在 docker-compose.yaml 文件中, 增加 network 并使用网络

3. 可以使用命令 docker run --net=network_name  , 在容器启动的时候加入到指定的网络

4. 可以在docker-compose.yaml 文件中, 将 service 加入到已存在的网络中. 

========================

增加网络

========================

增加一个 bridge 网络 

# docker network create --driver bridge my_first_ever_bridge_network

增加一个 overlay 网络

  overlay 网络适合多 host 的 容器集群, overlay 网络需要先在 docker daemon 环境中配置好 consul/etcd/zookeeper 存储. 详细操作见: https://foxutech.com/docker-networking/

========================

dns 和 参考

========================

https://foxutech.com/docker-networking-dns/

https://foxutech.com/docker-networking/

https://foxutech.com/docker-compose-networking/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值