docker 容器网络基础

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

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/

posted @   harrychinese  阅读(467)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2011-04-25 如何去重一个Oracle表
点击右上角即可分享
微信分享提示