docker 容器网络基础

时间:2023-03-08 16:49:23

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

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/