准备
需安装mysql并创建nacos数据库。
没有安装mysql?看这篇文章:【Docker基础二】Docker安装Mysql8-CSDN博客
建表语句地址:建表语句(nacos github)
下载镜像
可用版本:发布 ·阿里巴巴/NACOS (github.com)
bash
# 查看镜像
docker search nacos
# 获取最新版本镜像
# docker pull nacos/nacos-server
# 获取指定版本镜像
docker pull nacos/nacos-server:v2.2.1
# 查看本地镜像
docker images
创建挂载目录
bash
# 创建挂载目录
mkdir -p /usr/local/software/docker/nacos/conf
mkdir -p /usr/local/software/docker/nacos/logs
mkdir -p /usr/local/software/docker/nacos/data
启动容器
先直接启动一个容器
bash
docker run -itd \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=127.0.0.1 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=gusy1234 \
-e MYSQL_SERVICE_DB_NAME=nacos_db \
-p 8848:8848 \
--name nacos \
--restart=always \
nacos/nacos-server:v2.2.1
命令解释:
|-------------------------------------|----------------------|
| -e MODE=standalone | 单节点模式 |
| -e SPRING_DATASOURCE_PLATFORM=mysql | 使用mysql数据库连接方式 |
| -e MYSQL_SERVICE_HOST=127.0.0.1 | 指定数据库地址 |
| -e MYSQL_SERVICE_PORT=3306 | 数据库端口 |
| -e MYSQL_SERVICE_USER=root | 数据库用户名 |
| -e MYSQL_SERVICE_PASSWORD=gusy1234 | 数据库密码 |
| -e MYSQL_SERVICE_DB_NAME=nacos_db | 数据库名称 |
| -p 8848:8848 | 端口映射,语法 -p 主机端口:容器端口 |
| --restart=always | 任意时候重启容器,开机就能自动启动容器 |
| --name nacos | 容器名称 |
宿主机文件映射
拷贝容器配置文件到宿主机
bash
docker cp nacos:/home/nacos/conf/nacos-logback.xml /usr/local/software/docker/nacos/conf/
docker cp nacos:/home/nacos/conf/application.properties /usr/local/software/docker/nacos/conf/
修改applicaltion.properties文件,注意:需要给${}中的内容添加默认值
bash
# spring
server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
server.contextPath=/nacos
server.port=${NACOS_APPLICATION_PORT:8848}
server.tomcat.accesslog.max-days=30
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
server.tomcat.accesslog.enabled=${TOMCAT_ACCESSLOG_ENABLED:false}
# default current work dir
server.tomcat.basedir=file:.
#*************** Config Module Related Configurations ***************#
### Deprecated configuration property, it is recommended to use `spring.sql.init.platform` replaced.
#spring.datasource.platform=${SPRING_DATASOURCE_PLATFORM:}
# 修改此行,将SPRING_DATASOURCE_PLATFORM的默认值""改为mysql
spring.sql.init.platform=${SPRING_DATASOURCE_PLATFORM:mysql}
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=${MYSQL_DATABASE_NUM:1}
# 修改此行,添加MYSQL_SERVICE_HOST的默认值为127.0.0.1,MYSQL_SERVICE_DB_NAME的默认值为nacos_db
db.url.0=jdbc:mysql://${MYSQL_SERVICE_HOST:127.0.0.1}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME:nacos_db}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false}
db.user.0=${MYSQL_SERVICE_USER:root}
db.password.0=${MYSQL_SERVICE_PASSWORD:gusy1234}
# 多数据源
# db.url.1=jdbc:mysql://${MYSQL_SERVICE_HOST:127.0.0.1}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME:nacos_db}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false}
# db.user.1=${MYSQL_SERVICE_USER:root}
# db.password.1=${MYSQL_SERVICE_PASSWORD:gusy1234}
### The auth system to use, currently only 'nacos' and 'ldap' is supported:
nacos.core.auth.system.type=${NACOS_AUTH_SYSTEM_TYPE:nacos}
### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds:
nacos.core.auth.plugin.nacos.token.expire.seconds=${NACOS_AUTH_TOKEN_EXPIRE_SECONDS:18000}
### The default token:
nacos.core.auth.plugin.nacos.token.secret.key=${NACOS_AUTH_TOKEN:SecretKey012345678901234567890123456789012345678901234567890123456789}
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=${NACOS_AUTH_CACHE_ENABLE:false}
nacos.core.auth.enable.userAgentAuthWhite=${NACOS_AUTH_USER_AGENT_AUTH_WHITE_ENABLE:false}
nacos.core.auth.server.identity.key=${NACOS_AUTH_IDENTITY_KEY:serverIdentity}
nacos.core.auth.server.identity.value=${NACOS_AUTH_IDENTITY_VALUE:security}
## spring security config
### turn off security
nacos.security.ignore.urls=${NACOS_SECURITY_IGNORE_URLS:/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**}
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
重新启动容器
删除原容器
bash
docker stop nacos
docker rm nacos
重新运行容器,
bash
docker run -itd \
-e MODE=standalone \
-v /usr/local/software/docker/nacos/conf:/home/nacos/conf \
-v /usr/local/software/docker/nacos/logs:/home/nacos/logs \
-v /usr/local/software/docker/nacos/data:/home/nacos/data \
-p 8848:8848 \
--name nacos \
--restart=always \
nacos/nacos-server:v2.2.1
查看是否正常启动
bash
docker ps
# 如果未启动成功,使用下面语句查看报错日志
docker logs nacos
查看连通性
访问 http://localhost:8848/nacos 默认用户名/密码:nacos/nacos