〓 参考:
https://nacos.io/docs/latest/quickstart/quick-start-docker/?source=wuyi
https://github.com/nacos-group/nacos-docker
https://nacos.io/docs/latest/manual/admin/deployment/deployment-standalone/?source=wuyi
https://nacos.io/docs/latest/manual/admin/deployment/deployment-overview/?source=wuyi
1.在宿主机创建相关文件的映射挂载目录
● nacos - 挂载目录
mkdir -p /opt/docker/nacos/{data,logs}
sudo chmod -R 777 /opt/docker/nacos/{data,logs}
● mysql - 挂载目录
mkdir -p /opt/docker/mysql/{conf,data,logs,init}
sudo chmod -R 777 /opt/docker/mysql/{conf,data,logs,init}
2.下载nacos官方提供的mysql数据库初始化脚本并存放到init脚本目录下
● 下载 https://github.com/alibaba/nacos/blob/2.2.3/distribution/conf/mysql-schema.sql
● 将 mysql-schema.sql 存放在上面创建的 /opt/docker/mysql/init 挂载目录下
3.编写 docker-compose-nacos-mysql.yml 编排文件
version: '3.8'
services:
nacos-server:
image: nacos/nacos-server:v2.2.3
container_name: nacos-standalone-server
restart: unless-stopped
privileged: true
ports:
- "8848:8848"
- "9848:9848"
volumes:
- /opt/docker/nacos/logs:/home/nacos/logs
- /opt/docker/nacos/data:/home/nacos/data
- /etc/localtime:/etc/localtime:ro
environment:
NACOS_REPLICAS: 1
NACOS_SERVER_PORT: 8848
MODE: standalone
PREFER_HOST_MODE: hostname
SPRING_DATASOURCE_PLATFORM: mysql # 数据源平台为 MySQL
MYSQL_SERVICE_HOST: mysql # 或者使用IP地址,例如:192.168.110.20
MYSQL_SERVICE_PORT: 3306
MYSQL_SERVICE_DB_NAME: nacos_config # nacos 使用的数据库名
MYSQL_SERVICE_USER: nacos # nacos 服务访问 mysql 数据库的用户名
MYSQL_SERVICE_PASSWORD: nacos123 # nacos 服务访问 mysql 数据库的用户名对应密码
MYSQL_SERVICE_DB_PARAM: "characterEncoding=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai"
NACOS_AUTH_ENABLE: true # 开启鉴权功能
NACOS_AUTH_IDENTITY_KEY: 2222
NACOS_AUTH_IDENTITY_VALUE: 2xxx
NACOS_AUTH_TOKEN: SecretKey012345678901234567890123456789012345678901234567890123456789
NACOS_AUTH_TOKEN_EXPIRE_SECONDS: 18000
JAVA_OPT_EXT: "-Xmx256M -Xms256M -Xmn128M"
networks:
nacos:
ipv4_address: 172.60.0.10
depends_on:
mysql:
condition: service_healthy
mysql:
image: mysql:8.0.40
container_name: mysql-nacos-server
restart: unless-stopped
privileged: true
ports:
- "3306:3306"
volumes: # 映射配置 {宿主机目录:容器内目录}
- /opt/docker/mysql/data:/var/lib/mysql
- /opt/docker/mysql/logs:/var/log/mysql
# - /opt/nacos_docker/mysql/conf/my.cnf:/etc/mysql/my.cnf -- 根据需要编写自定义my.cnf配置文件
- /opt/docker/mysql/init/mysql-schema.sql:/docker-entrypoint-initdb.d/mysql-schema.sql
- /etc/localtime:/etc/localtime:ro
environment:
MYSQL_ROOT_PASSWORD: Root@666
MYSQL_DATABASE: nacos_config # 启动后会创建的数据库
MYSQL_USER: nacos # nacos_config 数据库的访问用户名
MYSQL_PASSWORD: nacos123 # nacos_config 数据库的访问用户名对应密码
LANG: C.UTF-8
command:
--max_connections=1024
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-authentication-plugin=mysql_native_password
--lower_case_table_names=1
--max_allowed_packet=128M
--explicit_defaults_for_timestamp=true
networks:
nacos:
ipv4_address: 172.60.0.12
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
interval: 10s
timeout: 10s
retries: 3
networks: # 自定义 docker 网络
nacos:
driver: bridge
ipam:
config:
- subnet: 172.60.0.0/24 # 定义网络注意不要出现子网冲突
gateway: 172.60.0.1
〓 校验配置格式是否有误
$ docker-compose -f docker-compose-nacos-mysql.yml config
〓 几点说明
● 网络设置要注意网段,子网冲突会报错:Error response from daemon: Pool overlaps with other one on this address space
● docker-entrypoint-initdb.d 目录:这是一个docker安装MySQL服务的特殊目录,用于执行一些初始化数据库的脚本任务
4.拉取镜像
docker pull nacos/nacos-server:v2.2.3
docker pull mysql:8.0.40
5.创建并启动容器
$ docker-compose -f docker-compose-nacos-mysql.yml up -d
● 其他运维命令
$ docker-compose -f docker-compose-nacos-mysql.yml down
$ docker-compose -f docker-compose-nacos-mysql.yml stop {容器名 | ID}
$ docker-compose -f docker-compose-nacos-mysql.yml start {容器名 | ID}
● 查看容器 IP 地址分配是否正确
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql-nacos-server
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nacos-standalone-server
6.访问确认安装成果
ip:/nacos/index.html
访问业务正常显示,则搭建成功,Over !