Docker网络的配置
本文最后更新于 1735 天前,其中的信息可能已经有所发展或是发生改变。

Docker网络的配置

Docker 允许通过外部访问容器或容器互联的方式来提供网络服务。

当 Docker 启动时,会自动在主机上创建一个 docker0 虚拟网桥,实际上是 Linux 的一个 bridge,可以理解为一个软件交换机。它会在挂载到它的网口之间进行转发。

外部访问容器

从前面的笔记中,可通过docker run -p hostPort:containerPort或者 docker run -P来映射访问端口

Docker容器的网络互联

[root@gzbsc001 ~]# docker network --help
Usage:  docker network COMMAND
Commands:
  connect     Connect a container to a network # 将容器连接到网络
  create      Create a network # 创建一个网络
  disconnect  Disconnect a container from a network # 断开容器与网络的连接
  inspect     Display detailed information on one or more networks # 显示一个或多个网络的详细信息
  ls          List networks # 列表
  prune       Remove all unused networks # 删除所有未使用的网络 (少用)
  rm          Remove one or more networks # 移除一个或多个网络

新建bridge网络

不指定网络驱动时默认创建的bridge网络

docker network create

如:docker network create -d bridge my-net

docker network create -d bridge --subnet 172.19.0.0/16 my-net-2

docker network create -d bridge --subnet 172.20.0.0/16 --gateway 172.20.0.1 my-net-3

-d 参数指定 Docker 网络类型,有 bridge overlay

查看所有的docker网络

docker network ls

[root@gzbsc001 ~]# docker network ls
NETWORK ID     NAME       DRIVER    SCOPE
56df0cb3ffa8   bridge     bridge    local
ea121359f489   host       host      local
f2a1bdfffdf1   my-net     bridge    local
490201c283d3   my-net-2   bridge    local
a77d21b3ec4a   none       null      local

网络类型:

  • bridge : 桥接(docker 默认)
  • none: 不配置网络
  • host : 和宿主机共享网络
  • container : 容器内网络连通

查看网络内部信息

docker network inspect

如:docker network inspect my-net

容器使用指定的网络

docker run --net or docker run --network

--net=bridge|none|container:NAME_or_ID|host or --network=bridge|none|container:NAME_or_ID|host

运行一个容器并连接到新建的 my-net 网络

docker run -it --name centos01 --network my-net centos:7.9.2009 /bin/bash

再运行一个容器并加入到 my-net 网络

docker run -it --name centos02 --network my-net centos:7.9.2009 /bin/bash

再运行一个容器不指定网络.则默认是bridge

docker run -it --name centos03 centos:7.9.2009 /bin/bash

等价于:docker run -it --name centos03 --network bridge centos:7.9.2009 /bin/bash

下面通过 ping 来证明 centos01 容器和 centos02 容器建立了互联关系。但与centos03无法建立

[root@a7f13c9f9a53 /]# 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.104 ms
64 bytes from centos01.my-net (172.18.0.2): icmp_seq=2 ttl=64 time=0.095 ms

[root@0cad8d800329 /]# 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.170 ms
64 bytes from centos02.my-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.092 ms

如果需要打通 centos01,centos02centos03互通,需要使用docker network connect

网络互通

docker network connect

[root@gzbsc001 ~]# docker network connect --help

Usage:  docker network connect [OPTIONS] NETWORK CONTAINER
Options:
      --alias strings           Add network-scoped alias for the container
      --driver-opt strings      driver options for the network
      --ip string               IPv4 address (e.g., 172.30.100.104)
      --ip6 string              IPv6 address (e.g., 2001:db8::33)
      --link list               Add link to another container
      --link-local-ip strings   Add a link-local address for the container

centos03 加入到 my-net:

docker network connect my-net centos03

因此这三个容器的网络情况:

docker inspect centos01 # my-net
docker inspect centos02 # my-net
docker inspect centos03 # bridge,my-net

断开网络与移除网络

docker network disconnect

[root@gzbsc001 ~]# docker network disconnect --help

Usage:  docker network disconnect [OPTIONS] NETWORK CONTAINER
Options:
  -f, --force   Force the container to disconnect from a network

centos03容器从my-net网络中断开

docker network disconnect my-net centos03

配置 DNS

配置全部容器的 DNS ,可以在 /etc/docker/daemon.json 文件中增加以下内容来设置。

{
  "dns" : [
    "114.114.114.114",
    "8.8.8.8"
  ]
}

配置完,需要重启 Docker 才能生效。

查看容器的 DNS 是否生效可以使用以下命令,它会输出容器的 DNS 信息:

docker run -it centos:7.9.2009 cat etc/resolv.conf

扩展:Docker查看容器IP地址

1.查看Docker的底层信息。

docker inspect 容器名称or docker inspect 容器id

所有可以根据底层信息查询:

docker inspect 容器id --format='{{.NetworkSettings.IPAddress}}'

docker inspect 容器名称 --format='{{.NetworkSettings.IPAddress}}'

[root@gzbsc001 ~]# docker inspect ebd4070698cb --format='{{.NetworkSettings.IPAddress}}'
172.17.0.2

2.进入容器,安装net-tools

docker exec -it ebd4070698cb

yum install net-tools

[root@ebd4070698cb /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 13751  bytes 20258574 (19.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9737  bytes 699665 (683.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 18  bytes 1452 (1.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 1452 (1.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇