Docker starts and installs nacos (detailed explanation, the most detailed in the whole network)

foreword

  • Before installation, you need to prepare a mysql. The current installation method is to persist the data in the database. The deployment here is a stand-alone mode.

1. Docker pulls the image

docker pull nacos/nacos-server
  • PS: This is to pull the latest nacos version. If you need to pull other versions, you can add: version number (eg: docker pull nacos/nacos-server:v2.2.0)

2. Mount directory

mkdir -p /mydata/nacos/logs/                      #新建logs目录
mkdir -p /mydata/nacos/conf/						#新建conf目录
  • PS: This step is to add a mapping folder to map the host's files to the nacos container

3. Start nacos and copy the file to the host, close the container

Start the container

docker run -p 8848:8848 --name nacos -d nacos/nacos-server

copy files

docker cp nacos:/home/nacos/logs/ /mydata/nacos/logs/
docker cp nacos:/home/nacos/conf/ /mydata/nacos/conf/

close container

docker rm -f nacos
  • PS: The purpose of starting nacos in this step is to copy the files in nacos to the mount directory, so that we can directly modify the files in the mount directory to map them into the container

4. Create the tables required for nacos in mysql

  • Create a new library in mysql, the name can be customized, here use nacos-config
  • Find the table creation file from github , execute it in the nacos-config library, and create the required table

5. Start nacos again

docker run -d
--name nacos															 
-p 8848:8848  -p 9848:9848 -p 9849:9849
--privileged=true
-e JVM_XMS=256m
-e JVM_XMX=256m
-e MODE=standalone
-v /mydata/nacos/logs/:/home/nacos/logs
-v /mydata/nacos/conf/:/home/nacos/conf/
--restart=always
nacos/nacos-server
  • PS : Copying the above statement fails to execute, you can change the above execution statement into one line, as follows, you can directly copy and execute
docker run -d --name nacos -p 8848:8848  -p 9848:9848 -p 9849:9849 --privileged=true -e JVM_XMS=256m -e JVM_XMX=256m -e MODE=standalone -v /mydata/nacos/logs/:/home/nacos/logs -v /mydata/nacos/conf/:/home/nacos/conf/ --restart=always nacos/nacos-server

Sentence explanation

  1. docker run -d: Start the container -d means to start in the background and return the container id
  2. --name nacos : specify a name for the container
  3. -p 8848:8848 -p 9848:9848 -p 9849:9849: Specify the port mapping, note that the p here cannot be capitalized, the capitalization is a random port mapping
  4. –privileged=true : Expand the permissions in the container, and change the permissions in the container to root permissions. If you don’t add it, it will be ordinary user permissions, and you may not open directory
  5. -e JVM_XMS=256m : memory allocated for jvm startup
  6. -e JVM_XMX=256m : The maximum memory allocated for jvm running
  7. -e MODE=standalone: ​​Use standalone mode (stand-alone mode), the MODE value has two types: cluster (cluster) mode/standalone mode, and MODE must be capitalized
  8. -v /mydata/nacos/logs/:/home/nacos/logs : Mount the /home/nacos/logs directory of the container to /mydata/nacos/logs
  9. -v /mydata/nacos/conf/:/home/nacos/conf/: Mount the /home/nacos/conf directory of the container to /mydata/nacos/conf
  10. –restart=always : When restarting docker, automatically start the relevant container

Precautions

  1. Relevant ports need to be opened on the firewall. If you are a cloud server, open the security group, and provide relevant statements below
## 开放端口8848 9848 9849
firewall-cmd --zone=public --add-port=8848/tcp --permanent
firewall-cmd --zone=public --add-port=9848/tcp --permanent
firewall-cmd --zone=public --add-port=9849/tcp --permanent

## 重启防火墙
firewall-cmd --reload

## 查看所有开启的端口
firewall-cmd --zone=public --list-ports
  • PS: There is a small problem here. After restarting the firewall, you need to restart docker
## 重启docker
systemctl restart docker
  1. The easiest thing to make a mistake here is that the mount directory does not match. You can check whether the directory after -v in your statement is mapped correctly. When the blogger installed it for the first time, there was a logs folder in the logs, and there was also a log folder in the conf. conf folder, resulting in an error

6. Modify the configuration file

  • The main modification is the application.properties file
## 在宿主机中修改application.properties文件
vim /mydata/nacos/conf/application.properties
  • Where the file is modified (modified to your corresponding mysql)
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos-config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=root
  • PS: Because we have copied the logs and conf to our host machine in the third step, then we can directly modify the application.properties file and map it to the container, which can be viewed through the following statement
## 进入到nacos容器里
docker exec -it nacos /bin/bash

## 查看application.properties文件
cat /home/nacos/conf/application.properties

## 退出容器
exit
  • PS: If the file is not modified successfully, it means that the mounting directory mapping is incorrect. Check it carefully. You can use the following method to check whether it is correct
docker inspect --format="{
   
   {json .Mounts}}" nacos

insert image description here

7. Visit the page

http://ip:8848/nacos/index.html

8. Finally

  • Leave a message if you have any questions, I will reply

Guess you like

Origin blog.csdn.net/ilvjiale/article/details/129417768