previous words
By default, you already know Docker and docker-compose
Nacos version: v2.2.3
MySQL version: 8.2.0
1. Download
- Open
Nacos
official website
Official website address: official website
-
click manual
-
left side
Nacos Docker
-
Clone the project locally
# 克隆项目,如果提示连接不到 github 请自行解决
git clone https://github.com/nacos-group/nacos-docker.git
# 进入项目目录
cd nacos-docker
注意:
If you get an error from git clone here: fatal: unable to access*** github Failure when receiving data from,
please refer to my article to configure the proxy (provided you have high technology, otherwise go to the github warehouse to download the source code or use a third-party github acceleration Service):
Click to jump
- You can see that the project has been downloaded locally
- Use IDE to open the project for easy editing (here I use IDEA to open it)
2. Create a database (taking MySQL8 as an example)
Because I already have a database locally and do not plan to use the nacos-docker project to help me create a database with Docker, so I need to modify its docker-compose.yaml file and manually create a database to store nacos data locally.
-
Find the official MySQL table structure file (see the picture directly)
-
Create database manually (I use Navicat16 operation)
I don’t choose the sorting rule, just let it default.
- Execute the official sql file
-
Put it into Navicat and execute it
-
You can see the created data table
3. Modify project configuration
I configure it locally, so I only configure the stand-alone mode. Please configure multiple cluster modes by yourself and modify the relevant modes.
- Modify
example/standalone-mysql-8.yaml
file
Remove the MySQL part of the configuration, because we no longer need docker to help me deploy the MySQL database.
Code can be copied directly
version: "3.8"
services:
nacos:
image: nacos/nacos-server:${
NACOS_VERSION}
container_name: nacos-standalone-mysql # 这里写你想要的容器名称
env_file:
- ../env/nacos-standlone-mysql.env
volumes:
- ./standalone-logs/:/home/nacos/logs # 这里看你需求修改成你自己的目录映射,这里我演示,不改
ports:
- "8848:8848"
- "9848:9848"
restart: always
network_mode: host # 加上这一行,使用 host 模式,这样子容器内部直接访问 localhost 就是宿主机本身,方便一些,反正端口不冲突
- Modify
env/nacos-standlone-mysql.env
file
Code:
PREFER_HOST_MODE=hostname
MODE=standalone # 单机模式
SPRING_DATASOURCE_PLATFORM=mysql # 使用 MySQL 作为存储
MYSQL_SERVICE_HOST=127.0.0.1 # 本机的话直接使用这个或者 localhost,刚刚设置的 host 网络模式可以直接访问到宿主机
MYSQL_SERVICE_DB_NAME=nacos_config # 存储 Nacos 配置信息的数据库名称
MYSQL_SERVICE_PORT=3306 # 数据库访问端口
MYSQL_SERVICE_USER=root # 访问你数据库的用户,默认用 root 即可,你设置有别的用户也可以
MYSQL_SERVICE_PASSWORD=root # root 密码
# 下面这行可以跟着我写,保险起见,加上时区,官方给的只有 UTC 时区在 .properties 中
MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
- Modify
mysql.env
file
Code:
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=nacos_config
MYSQL_USER=root
MYSQL_PASSWORD=root
LANG=C.UTF-8
- Revise
example/init.d/application.properties
I didn't check carefully whether this file has been loaded (my intuition is that this file is not loaded, and the configuration of this file is used
build/conf/application.properties
. This file refers to externally defined variables and does not need to be modified manually). To be safe, I should modify it.
After modification:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
db.user.0=root
db.password.0=root
- After the above modifications, it is completed
4. Build image & run container
The following is the command to execute in the project root directory
# 执行以下命令,会以standalone-mysql-8.yaml中定义的容器信息进行创建并运行容器
docker-compose -f example/standalone-mysql-8.yaml up # 为了方便看控制台,我们用这个演示
# 或者
docker-compose -f example/standalone-mysql-8.yaml up -d # 后台启动
After executing the command, the container deployment is successful:
View the container:
I am not using DockerDesktop here, so the interface is different. Mine is OrbStack, which saves resources and is faster than DockerDesktop. Except that the interface is not that beautiful and the port mapping is not as intuitive as DockerDesktop, everything else is fine.
Friends who want to know more can go to the official website: https://orbstack.dev/ or click on the official website link to jump
- Note: OrbStack currently does not support Windows platform
Browser access address: http://localhost:8848/nacos
Or click directly to jump to the address
Let’s create a new namespace test
View the database:
5. Complete
At this point, we have completed deploying the Nacos container using Docker. If it helps you, don’t forget to like it~