基于docker-compose 运行,主要是自定义了network 的ipam 通过docker-compose 方便的管理网络
参考配置
version: "3"
services:
mysql:
image: mysql:5.7.16
restart: always
volumes:
- ./mysql:/var/lib/mysql
ports:
- 3306:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION --lower_case_table_names=1
networks:
- app_net
environment:
MYSQL_ROOT_PASSWORD: <root-password>
MYSQL_USER: <user>
MYSQL_PASSWORD: <pass>
MYSQL_DATABASE: <dbname>
TZ: Asia/Shanghai
networks:
app_net:
ipam:
driver: default
config:
- subnet: "192.19.238.0/24"
说明
以上通过配置networks 以及ipam 的子网,服务启动的时候添加制定的network 就可以了,不然docker 可能会自己创建网络造成网络冲突,同时如果我们
对于容器制定了多个网络那么就可以模拟多网卡的场景,如下
version: "3"
services:
mysql:
image: mysql:5.6
restart: always
volumes:
- ./mysql:/var/lib/mysql
ports:
- 3316:3306
command: --sql_mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION --lower_case_table_names=1
networks:
- app_net
- app_net2
environment:
MYSQL_ROOT_PASSWORD: <password>
TZ: Asia/Shanghai
networks:
app_net:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
app_net2:
ipam:
driver: default
config:
- subnet: "171.16.238.0/24"
效果
参考资料
https://docs.docker.com/compose/compose-file/compose-file-v3/#ipv4_address-ipv6_address
https://docs.docker.com/compose/compose-file/compose-file-v3/#network-configuration-reference