自学内容网 自学内容网

Docker笔记-Docker网络详解

Docker笔记-Docker网络详解

1、查看网络

$ ip addr

docker01中:

[root@33acc58bfa50 /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
48: eth0@if49: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.5/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

docker02中:

[root@3499fc3a4a5b /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
50: eth0@if51: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:06 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.6/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

2、两个docker容器之间也是可以ping通的

docker分配的地址和linux主机是在一个网段的,docker主机和linux主机是桥接模式。

$ docker run -d -P --name nginx01 nginx
[root@zsx work]# docker run -d -P --name nginx01 nginx
cd053a258fae3e95b8e711549f708e2717cfb28f1eeab0c5923d5f1422eafbcb
$ docker run -d -P --name nginx02 --link nginx01 nginx
[root@zsx work]# docker run -d -P --name nginx02 --link nginx01 nginx
a8e7aa2559c5294c4adafb191c7467a1dcae0f3d2b8efe2e780c8ea70881b075
# 直接通过启动容器的名字容器相互去ping是ping不通的,可以在启动容器的时候加上--link参数
$ docker exec -it nginx02 ping nginx01
[root@zsx work]# docker exec -it nginx02 ping nginx01
PING nginx01 (172.17.0.2) 56(84) bytes of data.
64 bytes from nginx01 (172.17.0.2): icmp_seq=1 ttl=64 time=0.099 ms
64 bytes from nginx01 (172.17.0.2): icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from nginx01 (172.17.0.2): icmp_seq=3 ttl=64 time=0.064 ms
64 bytes from nginx01 (172.17.0.2): icmp_seq=4 ttl=64 time=0.074 ms
64 bytes from nginx01 (172.17.0.2): icmp_seq=5 ttl=64 time=0.049 ms
64 bytes from nginx01 (172.17.0.2): icmp_seq=6 ttl=64 time=0.046 ms
64 bytes from nginx01 (172.17.0.2): icmp_seq=7 ttl=64 time=0.053 ms
64 bytes from nginx01 (172.17.0.2): icmp_seq=8 ttl=64 time=0.049 ms
# 其实就是在nginx02的hosts文件中写入了nginx01的ip地址映射
$ docker exec -it nginx02 cat /etc/hosts
[root@zsx work]# docker exec -it nginx02 /bin/bash
root@a8e7aa2559c5:/# cat /etc/hosts
127.0.0.1localhost
::1localhost ip6-localhost ip6-loopback
fe00::0ip6-localnet
ff00::0ip6-mcastprefix
ff02::1ip6-allnodes
ff02::2ip6-allrouters
172.17.0.2nginx01 cd053a258fae
172.17.0.3a8e7aa2559c5
# 查看网络
[root@zsx work]# docker network ls
NETWORK ID     NAME                        DRIVER    SCOPE
22a861c9adb3   bridge                      bridge    local
3ec11aa11df0   composetest_default         bridge    local
be8412b8bc14   demodockercompose_default   bridge    local
87f220cd32d2   host                        host      local
b13fcbc0d0af   my_wordpress_default        bridge    local
d61d5b0273fb   none                        null      local
9e3b5ce80e32   test-net                    bridge    local
$ docker network inspect 网络ID
$ docker network inspect 87f220cd32d2
root@zsx work]# docker network inspect 87f220cd32d2
[
    {
        "Name": "host",
        "Id": "87f220cd32d2299b9220eba6262c8115bec76afdb8bcbbcf4d358547ef5f6256",
        "Created": "2021-11-14T20:16:30.775849942+08:00",
        "Scope": "local",
        "Driver": "host",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": []
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

3、自定义网络

# 默认是桥接模式
$ docker run -d -P --name nginx03 nginx
$ docker run -d -P --name nginx04 --net bridge nginx
[root@zsx work]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                     NAMES
baab14f0aee1   nginx     "/docker-entrypoint.…"   6 seconds ago    Up 5 seconds    0.0.0.0:49160->80/tcp, :::49160->80/tcp   nginx04
2ebb70384047   nginx     "/docker-entrypoint.…"   15 seconds ago   Up 14 seconds   0.0.0.0:49159->80/tcp, :::49159->80/tcp   nginx03
a8e7aa2559c5   nginx     "/docker-entrypoint.…"   14 minutes ago   Up 14 minutes   0.0.0.0:49158->80/tcp, :::49158->80/tcp   nginx02
cd053a258fae   nginx     "/docker-entrypoint.…"   14 minutes ago   Up 14 minutes   0.0.0.0:49157->80/tcp, :::49157->80/tcp   nginx01
# 自定义一个网络
$ docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
t@zsx work]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
e38e354f6dadc2142b4d2325e5ca4d7afbfb6532df5f5dca6b8352a6e728eef
$ docker network ls
[root@zsx work]# docker network ls
NETWORK ID     NAME                        DRIVER    SCOPE
22a861c9adb3   bridge                      bridge    local
3ec11aa11df0   composetest_default         bridge    local
be8412b8bc14   demodockercompose_default   bridge    local
87f220cd32d2   host                        host      local
b13fcbc0d0af   my_wordpress_default        bridge    local
e38e354f6dad   mynet                       bridge    local
d61d5b0273fb   none                        null      local
9e3b5ce80e32   test-net                    bridge    local
$ docker network inspect mynet
[root@zsx work]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "e38e354f6dadc2142b4d2325e5ca4d7afbfb6532df5f5dca6b8352a6e728eef2",
        "Created": "2021-11-18T21:17:35.768427499+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
# 自己创建网络
$ docker run -d -P --name nginx05 --net mynet nginx
$ docker run -d -P --name nginx06 --net mynet nginx
$ docker exec -it nginx05 ping 192.168.0.3
[root@zsx work]# docker exec -it nginx05 ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.071 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.046 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from 192.168.0.3: icmp_seq=4 ttl=64 time=0.050 ms
64 bytes from 192.168.0.3: icmp_seq=5 ttl=64 time=0.090 ms
64 bytes from 192.168.0.3: icmp_seq=6 ttl=64 time=0.051 ms
64 bytes from 192.168.0.3: icmp_seq=7 ttl=64 time=0.060 ms
64 bytes from 192.168.0.3: icmp_seq=8 ttl=64 time=0.054 ms
$ docker exec -it nginx05 ping nginx06
[root@zsx work]# docker exec -it nginx05 ping nginx06
PING nginx06 (192.168.0.3) 56(84) bytes of data.
64 bytes from nginx06.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.059 ms
64 bytes from nginx06.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.067 ms
64 bytes from nginx06.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from nginx06.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.050 ms
64 bytes from nginx06.mynet (192.168.0.3): icmp_seq=5 ttl=64 time=0.052 ms

4、网络连通

$ docker run -d -P --name nginx07 nginx
[root@zsx work]# docker run -d -P --name nginx07 nginx
a7f3e360c27b2e749933ee7df5f54e5e0ab6a3ebef73028f6e46b15253c1e51b
# 将网络和容器连通
$ docker network connect mynet nginx07
$ docker exec -it nginx05 ping nginx07
[root@zsx work]# docker exec -it nginx05 ping nginx07
PING nginx07 (192.168.0.4) 56(84) bytes of data.
64 bytes from nginx07.mynet (192.168.0.4): icmp_seq=1 ttl=64 time=0.092 ms
64 bytes from nginx07.mynet (192.168.0.4): icmp_seq=2 ttl=64 time=0.165 ms
64 bytes from nginx07.mynet (192.168.0.4): icmp_seq=3 ttl=64 time=0.144 ms
64 bytes from nginx07.mynet (192.168.0.4): icmp_seq=4 ttl=64 time=0.146 ms

原文地址:https://blog.csdn.net/qq_30614345/article/details/136972560

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!