Docker网络与容器互联

@[TOC] # Docker网络与容器互联

资源推荐

Docker官网 配置网络: https://docs.docker.com/network/

Docker网络详解: https://blog.csdn.net/meltsnow/article/details/94490994

Docker四种网络模式: https://www.jianshu.com/p/22a7032bb7bd

使用网络

网络驱动名称官方文档官方教程
bridge(默认)桥接网络https://docs.docker.com/network/bridge/https://docs.docker.com/network/network-tutorial-standalone/
host主机网络https://docs.docker.com/network/host/https://docs.docker.com/network/network-tutorial-host/
overlay覆盖网络https://docs.docker.com/network/overlay/https://docs.docker.com/network/network-tutorial-overlay/
ipvlanIPvlan 网络https://docs.docker.com/network/ipvlan/
macvlanMacvlan 网络https://docs.docker.com/network/macvlan/https://docs.docker.com/network/network-tutorial-macvlan/
none禁用容器网络https://docs.docker.com/network/none/
网络插件第三方网络插件https://docs.docker.com/engine/extend/plugins_services/

桥接网络示意图
17.png
当创建一个 Docker 容器的时候,同时会创建了一对 veth pair 接口(当数据包发送到一个接口时,另外一个接口也可以收到相同的数据包)。这对接口一端在容器内,即 eth0;另一端在本地并被挂载到 docker0 网桥,名称以 veth 开头(例如 vethAQI2QT)。通过这种方式,主机可以跟容器通信,容器之间也可以相互通信。Docker 就创建了在主机和所有容器之间一个虚拟共享网络。

容器互联

docker network

连接容器

# 创建网络
docker network create -d bridge my-net
# 列出所有网络
$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
2b2da88f6307   bridge    bridge    local
c426cb16f1ba   host      host      local
b63cf9acb121   my-net    bridge    local
642abd8a74a7   none      null      local
# 下载centos镜像
docker pull centos
# 新的终端
 docker run -it --rm --name centos01 --network my-net centos bash
# 新的终端
 docker run -it --rm --name centos02 --network my-net centos bash
 
 # 容器列表
docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
296a215ab471   centos    "bash"                   18 seconds ago   Up 16 seconds             centos02
4f546527fae1   centos    "bash"                   42 seconds ago   Up 38 seconds             centos01

 # 在 centos01 容器中 ping centos02
 [root@4f546527fae1 /]# ping centos02
PING centos02 (172.18.0.3) 56(84) bytes of data.
64 bytes from centos02.my-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.066 ms
64 bytes from centos02.my-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.044 ms
 # 在 centos02 容器中 ping centos01
 [root@296a215ab471 /]# ping centos01
PING centos01 (172.18.0.2) 56(84) bytes of data.
64 bytes from centos01.my-net (172.18.0.2): icmp_seq=1 ttl=64 time=0.180 ms
64 bytes from centos01.my-net (172.18.0.2): icmp_seq=2 ttl=64 time=0.053 ms

# 查看hosts
[root@296a215ab471 /]# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3      296a215ab471

配置 DNS

  • mount 看到挂载信息
$ mount
C:/Program Files/Git on / type ntfs (binary,noacl,auto)
C:/Program Files/Git/usr/bin on /bin type ntfs (binary,noacl,auto)
C:/Users/水月情~1/AppData/Local/Temp on /tmp type ntfs (binary,noacl,posix=0,usertemp)
C: on /c type ntfs (binary,noacl,posix=0,user,noumount,auto)
D: on /d type ntfs (binary,noacl,posix=0,user,noumount,auto)
E: on /e type ntfs (binary,noacl,posix=0,user,noumount,auto)
  • 配置全部容器的dns
/etc/docker/daemon.json
{
  "dns" : [
    "114.114.114.114",
    "8.8.8.8"
  ]
}
# 在容器内部 /etc/resolv.conf 可以看到dns
$  docker run -it --rm centos  cat etc/resolv.conf
# DNS requests are forwarded to the host. DHCP DNS options are ignored.
nameserver 192.168.65.5
  • 手动指定容器配置
  1. -h HOSTNAME 或者 --hostname=HOSTNAME 设定容器的主机名,它会被写到容器内的 /etc/hostname 和 /etc/hosts。但它在容器外部看不到,既不会在 docker container ls 中显示,也不会在其他的容器的 /etc/hosts 看到。
  2. –dns=IP_ADDRESS 添加 DNS 服务器到容器的 /etc/resolv.conf 中,让容器用这个服务器来解析所有不在 /etc/hosts 中的主机名。
  3. –dns-search=DOMAIN 设定容器的搜索域,当设定搜索域为 .example.com 时,在搜索一个名为 host 的主机时,DNS 不仅搜索 host,还会搜索 host.example.com。

docker run --lisk (已废弃,建议使用network)

就很可能还在用默认桥接网络,这很不安全,所有容器都没有适度隔离,用自定义网络才比较方便互联隔离。

# 下载镜像
docker pull centos
# centos 使用 ip add 查看本地地址信息

# 建立第一个容器 (终端1)
 docker run -it --rm --name centos01 centos bash
 
 # 建立第二个容器(终端2)
docker run  -d  -it --rm --name centos02 --link centos01 centos  bash

# 进入容器1
docker exec -it centos01 bash
[root@941828a74821 /]# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3      941828a74821
[root@941828a74821 /]# ping 172.17.0.4
PING 172.17.0.4 (172.17.0.4) 56(84) bytes of data.
64 bytes from 172.17.0.4: icmp_seq=1 ttl=64 time=0.059 ms
64 bytes from 172.17.0.4: icmp_seq=2 ttl=64 time=0.039 ms

# 进入容器2
docker exec -it centos02 bash
[root@af7440908cb3 /]# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3      centos01 941828a74821
172.17.0.4      af7440908cb3
[root@af7440908cb3 /]# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.066 ms
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水月情缘雪飞飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值