對于容器來說,無論是網橋、overlay、macvlan 還是自定義的網絡插件,它都沒有關于所連接配接的網絡類型的資訊,它隻能看到具有ip位址、網關、路由表、DNS服務和其他網絡詳細資訊的網絡接口。
預設的情況下,在使用 docker run 或是 docker create 時,容器不會暴露任何的端口,為了使容器端口對外生效,可以使用 --publish 或是 -p 參數,這會在容器裡建立一個防火牆規則,将容器的端口映射到Docker容器主控端的端口。
-p 8080:80 | 通過tcp協定将容器的80端口映射到主控端的8080 |
-p 192.168.136.133:8080:80 | 通過tcp協定将容器的80端口映射到主控端ip為192.168.136.133網卡的8080,隻能通過 192.168.136.133:8080 通路 |
-p 8080:80/udp | 通過udp協定将容器的80端口映射到主控端的8080 |
-p 8080:80/tcp -p 8080:80/udp | 定義兩種協定都是容器的80端口映射主控端的8080 |
容器會通過綁定的網絡位址池中擷取一個有效的ip位址,docker daemon 扮演了一個DHCP 伺服器,每種網絡類型也會有它的一個子網範圍。
在啟動一個容器時,可以通過 --network 參數,為容器指定一個網絡。也可以通過 docker network connect 将一個容器連接配接到兩個網絡中,同樣也可以在啟動時,通過 --ip 來為容器指定ip。
如下建立兩個網絡 net01 和 net02
[root@localhost image]# docker network inspect net01
[root@localhost image]# docker network inspect net02
對應的網段是 net01 172.21.0.0/16 和 net02 172.22.0.0/16
建立兩個容器,分别使用這兩個網絡
docker run -d --name nginx01 --network net01 nginx // => 配置設定的ip是 172.21.0.2
docker run -d --name nginx02 --network net02 nginx // => 配置設定的ip是 172.22.0.2
這兩個容器間是不能通的,因為使用的網絡不同,ip網絡也不同
可以通過 docker network connect 進行網絡連接配接
docker network connect net02 nginx01
docker inspect nginx01
"Networks": {
"net01": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"14bd7d215f60"
],
"NetworkID": "d7c07c483e80f1be3e8aa15a2d559745e74ad53cfeca011e463c2e9b68840a54",
"EndpointID": "829f9503073f97ac1851f065adc522fb56f1326e3035b3802a12ebf9f497ab73",
"Gateway": "172.21.0.1",
"IPAddress": "172.21.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:15:00:02",
"DriverOpts": null
},
"net02": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"14bd7d215f60"
],
"NetworkID": "0c91bcc6dca2405fed059da4ab410bec9213d0ee0dfc70cfdb0d9d282d3316fd",
"EndpointID": "69468fc887e2cf063351725d63efde1d455a0c4e9c75d82e6f5374901640020f",
"Gateway": "172.22.0.1",
"IPAddress": "172.22.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:16:00:03",
"DriverOpts": {}
}
}
則在 nginx01 通路 172.22.0.2 是可以通的
通過 docker network disconnect 可以取消一個容器和網絡的連接配接
# 将剛才的nginx01 從net02 中移除
docker network disconnect net02 nginx01
再檢視nginx01的網絡資料,已沒有 net02
也可以通過 --ip 來手動指定一個ip
"Networks": {
"net01": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"14bd7d215f60"
],
"NetworkID": "d7c07c483e80f1be3e8aa15a2d559745e74ad53cfeca011e463c2e9b68840a54",
"EndpointID": "829f9503073f97ac1851f065adc522fb56f1326e3035b3802a12ebf9f497ab73",
"Gateway": "172.21.0.1",
"IPAddress": "172.21.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:15:00:02",
"DriverOpts": null
}
容器連接配接網絡時,手動指定ip
docker network connect --ip 172.22.0.12 net02 nginx01
傳回錯誤資訊(因為在建立network時,沒有指定--subnet ,是由 docker配置設定的,這個網絡不允許手動指定ip)
[root@localhost image]# docker network connect --ip 172.22.0.12 net02 nginx01
Error response from daemon: user specified IP address is supported only when connecting to networks with user configured subnets
需要重新建立這個網絡,通過 --subnet 172.22.0.0/16 --gateway 172.22.0.1
[root@localhost image]# docker network create -d bridge --subnet 172.22.0.0/16 --gateway 172.22.0.1 net02
再進行指定ip的連接配接時
[root@localhost image]# docker network connect --ip 172.22.0.12 net02 nginx01 # 正常操作
[root@localhost image]# docker network inspect net02
"Containers": {
"14bd7d215f60b0d7fd09f8f8e891b15310cd3de2f246b79b61fed54d0029402c": {
"Name": "nginx01",
"EndpointID": "6b350dbfaaf4843283e5cae5be933ebead43329eb12e3dfe6049d071fb2657b3",
"MacAddress": "02:42:ac:16:00:0c",
"IPv4Address": "172.22.0.12/16", # 為指定的ip了
"IPv6Address": ""
},
"564726dab854ad56fae26732ec28b50d47c7578207cfa0808d4bc00279a020e8": {
"Name": "nginx02",
"EndpointID": "394bcf3e3d5252a8f997b1ce45995802a7801f3751b35222433c30961276f1e2",
"MacAddress": "02:42:ac:16:00:02",
"IPv4Address": "172.22.0.2/16",
"IPv6Address": ""
}
},
同樣,預設容器的hostname是容器的id
[root@localhost image]# docker exec -it nginx01 /bin/bash
root@14bd7d215f60:/# hostname
14bd7d215f60
可以通過 --hostname 參數來指定
[root@localhost image]# docker run -d --name nginx03 --hostname nginx03 nginx
1902520b9623eaec2e9e177546e0072676e1d2a8c4d5a689142dcd6f0ebe0263
[root@localhost image]# docker exec -it nginx03 /bin/bash
root@nginx03:/# hostname
nginx03
docker 容器裡預設也是繼承主控端的DNS配置。預設會從主控端複制一份到容器。使用自定義的網絡時,會使用内嵌的DNS伺服器,内嵌的伺服器會轉發到在宿主上去尋找DNS伺服器。
在 /etc/hosts 自定義的host配置,不會被容器繼承。可以通過 --add-host 參數将其他的host 加入到容器的 /etc/hosts檔案裡
如未指定 --add-host時,nginx03容器的 /etc/hosts 如下:
root@nginx03:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
172.17.0.3 nginx03
添加 --add-host=nginx02:172.21.0.2 --add-host=nginx01:172.22.0.2 (隻是舉例,是通路不了)
[root@localhost image]# docker run -d --name nginx03 --hostname nginx03 --add-host=nginx02:172.21.0.2 --add-host=nginx01:172.22.0.2 nginx
f3fde021876ae12c6f96efcfcd98ddea519772309924d61b8d53bf2113a5c4a4
[root@localhost image]# docker exec -it nginx03 /bin/bash
root@nginx03:/# cat /etc/hosts
127.0.0.1 localhost
172.21.0.2 nginx02
172.22.0.2 nginx01
172.17.0.3 nginx03
也可以在啟動容器時,通過參數 --dns 來指定dns伺服器的 ip, --dns-search 指定搜尋的非完整的hostname,分别對應 resolv.conf 檔案中的 nameserver 與 search 字段