对于容器来说,无论是网桥、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 字段