docker-compose network 网络 作者:马育民 • 2020-07-27 10:56 • 阅读:10608 # 介绍 由 https://www.malaoshi.top/show_1EF5xCV2BIyn.html 可知,通过docker-compose创建容器时,也会创建新网络,网络名由docker生成 # 指定新网络名 ``` version: '3' services: tomcat1: image: tomcat # 镜像 container_name: tomcat1 # 指定容器名称 networks: - mynet ports: # bridge网络模式,指定映射端口 - "8080:8080" # network_mode: "host" # 指定网络模式 restart: always # 当docker服务启动时,容器总是跟着启动 networks: mynet: ``` 启动 ``` docker-compose up -d ``` 执行结果: ``` Creating network "docker_tomcat_mynet" with the default driver Creating tomcat2 ... done ``` 执行时会创建网络,网络名是:`docker_tomcat_mynet`,容器使用该网络,容器之间网络通信,可以通过容器名通信 ### 查看网络列表 ``` docker network ls ``` 执行结果: ``` NETWORK ID NAME DRIVER SCOPE 2b55b4d8cb35 bridge bridge local 693583264ce3 docker_tomcat_mynet bridge local de8e17860cc8 host host local c25c6222ce03 none null local ``` `docker_tomcat_mynet` 就是刚才创建的网络 ### 查看网络详细信息 ``` docker network inspect docker_tomcat_mynet ``` 执行结果如下: ``` [ { "Name": "docker_tomcat_mynet", "Id": "693583264ce3063ec8f6ce1b8e6e6aae8a257d0008d18a77563368ff26636a4b", "Created": "2020-07-27T11:56:53.06880692+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "192.168.48.0/20", "Gateway": "192.168.48.1" } ] }, "Internal": false, "Attachable": true, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "6524db462d97deb4bb99fa14e596f23c12089c15a488116427763d7a877030a6": { "Name": "tomcat2", "EndpointID": "1315ac70329bc7618d54b9a208235dbbdd8ca1c9c4514db2f539dfea052c4a33", "MacAddress": "02:42:c0:a8:30:02", "IPv4Address": "192.168.48.2/20", "IPv6Address": "" } }, "Options": {}, "Labels": { "com.docker.compose.network": "mynet", "com.docker.compose.project": "docker_tomcat", "com.docker.compose.version": "1.26.2" } } ] ``` 该网络的网段是:`"Subnet": "192.168.48.0/20",`,由docker生成 # 指定ip网段 ``` version: '3' services: tomcat1: image: tomcat # 镜像 container_name: tomcat1 # 指定容器名称 networks: - mynet ports: # bridge网络模式,指定映射端口 - "8080:8080" # network_mode: "host" # 指定网络模式 restart: always # 当docker服务启动时,容器总是跟着启动 networks: mynet: driver: bridge ipam: config: - subnet: 172.50.0.0/16 ``` ### 查看网络列表 ``` docker network ls ``` 执行结果: ``` 2b55b4d8cb35 bridge bridge local b4b0ddc79d72 docker_tomcat_mynet bridge local de8e17860cc8 host host local c25c6222ce03 none null local 31a1c1f34ee1 testnet bridge local ``` `docker_tomcat_mynet` 就是刚才创建的网络 ### 查看网络详细信息 ``` docker network inspect docker_tomcat_mynet ``` 执行结果: ``` [ { "Name": "docker_tomcat_mynet", "Id": "b4b0ddc79d72e84efd42a2d63a3ea06107a8a3fd1533bcf61d956400619e4305", "Created": "2020-07-27T11:45:41.83651529+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.50.0.0/16" } ] }, "Internal": false, "Attachable": true, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "ac3d4ea16b1c5e081ecddef48ab8e0bae042292840318e6afd692668cd632ee7": { "Name": "tomcat2", "EndpointID": "0b59ab2da3429852a17067d7b015fb918398b38645784d26bcbcd95ae9e467c6", "MacAddress": "02:42:ac:32:00:02", "IPv4Address": "172.50.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": { "com.docker.compose.network": "mynet", "com.docker.compose.project": "docker_tomcat", "com.docker.compose.version": "1.26.2" } } ] ``` 查看该网络的网段,与上面设置的是一致的 # 使用默认的bridge网络 ``` version: '3' services: tomcat1: image: tomcat # 镜像 container_name: tomcat1 # 指定容器名称 network_mode: bridge ports: # bridge网络模式,指定映射端口 - "8080:8080" # network_mode: "host" # 指定网络模式 restart: always # 当docker服务启动时,容器总是跟着启动 ``` 由于使用的是默认的bridge网络,只能通过ip通信,不能通过容器名通信 # 使用已存在的自定义网络 创建网络 ``` docker network create --subnet=172.18.0.0/16 mynetwork ``` yml内容: ``` version: '3' services: tomcat1: image: tomcat # 镜像 container_name: tomcat1 # 指定容器名称 networks: - default ports: # bridge网络模式,指定映射端口 - "8080:8080" # network_mode: "host" # 指定网络模式 restart: always # 当docker服务启动时,容器总是跟着启动 networks: default: external: name: mynetwork ``` 使用 mynetwork网络的容器,可通过 ip 或 容器名 通信 ### 指定容器ip ``` version: '3' services: tomcat1: image: tomcat # 镜像 container_name: tomcat3 # 指定容器名称 networks: default: ipv4_address: 172.18.0.31 ports: # bridge网络模式,指定映射端口 - "8080:8080" # network_mode: "host" # 指定网络模式 restart: always # 当docker服务启动时,容器总是跟着启动 networks: default: external: name: testnet ``` 原文出处:http://malaoshi.top/show_1EF5xnHp0lVY.html