目录
我这边有两个容器,宿主机ip为 192.168.1.115,一个mysql,一个nacos,部署在主机上,使用的默认网络bridge,想从nacos里面来使用mysql的库,发现不能使用主机的ip,现状:
查看当前网络
docker network ls
是因为容器间网络不互通的问题,
思路为创建一个共用网络,
创建网络
docker network create dev
删除网络
docker network rm mysql_default
version: '3.1'
services:
mysql:
image: mysql:8.0.17
restart: always
container_name: mysql
privileged: true
ports:
- 3306:3306
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 111333
command:
--max_connections=1000
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M
volumes:
- /apps/mysql/config:/etc/mysql
- /apps/mysql/logs:/var/log/mysql
- /apps/mysql/data:/var/lib/mysql
- /apps/mysql/mysql-files:/var/lib/mysql-files/
networks:
- dev
networks:
dev:
external: true
创建一个mysql容器,使用这个dev,启动
查看网络状态
docker inspect dev
mysql,已经挂到了这个网络下
可以直接使用容器名称来代替ip:
db.url.0=jdbc:mysql://mysql:3306/nacos
- version: '3'
- services:
- nacos:
- restart: always
- image: nacos/nacos-server:v2.2.1
- # 容器名称
- container_name: nacos
- # 端口映射
- ports:
- - 8108:8848
- - 9848:9848
- - 9849:9849
- # 容器权限
- privileged: true
- # 参数设置
- environment:
- JVM_XMS: 128m
- JVM_XMX: 128m
- JVM_MS: 64m
- JVM_MMS: 64m
- MODE: standalone
- NACOS_REPLICAS: 1
- PREFER_HOST_MODE: ip
- SPRING_DATASOURCE_PLATFORM: mysql
- MYSQL_SERVICE_HOST: mysql
- MYSQL_SERVICE_DB_NAME: nacos
- MYSQL_SERVICE_PORT: 3306
- MYSQL_SERVICE_USER: root
- MYSQL_SERVICE_PASSWORD: 113355
- volumes:
- - /apps/nacos/logs:/home/nacos/logs
- - /apps/nacos/plugins:/home/nacos/plugins
- - /apps/nacos/data:/home/nacos/data
- - /apps/nacos/config/application.properties:/home/nacos/conf/application.properties
- networks:
- - dev
- networks:
- dev:
- external: true
- ### If use MySQL as datasource:
- spring.datasource.platform=mysql
-
- ### Count of DB:
- db.num=1
-
- ### Connect URL of DB:
- db.url.0=jdbc:mysql://mysql:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
- db.user.0=root
- db.password.0=113355
-
- nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
-
- #
- #nacos.core.auth.enabled=true
- #nacos.core.auth.enable.userAgentAuthWhite=false
- #nacos.core.auth.server.identity.key=自定义的值
- #nacos.core.auth.server.identity.value=自定义的值
启动后效果:
两个容器都已经加入的这个网络,并且可以使用容器名字访问
检查网络状态