Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Docker Single-Node Deployment of Nacos (with/without MySQL Support)

Tech 1

Using Server MySQL Version

1. Pull Nacos Image

To fetch the Nacos Docker image (use the latest version by default, or specify a version like 2.0.3):

docker pull nacos/nacos-server  # Latest version
# Optional: Specify version (e.g., 2.0.3)
# docker pull nacos/nacos-server:2.0.3

2. Set Up MySQL (Skip if Already Installed)

2.1 Pull MySQL Image

docker pull mysql:latest

2.2 Run MySQL Container

docker run -d \
  --name mysql-instance \
  -p 3306:3306 \
  -e TZ=Asia/Shanghai \
  -e MYSQL_ROOT_PASSWORD=my-secret-pw \
  mysql:latest \
  --character-set-server=utf8mb4 \
  --collation-server=utf8mb4_unicode_ci

2.3 Create Nacos Database and Tables

Connect to the MySQL container and execute the SQL schema (offficial Nacos tables):

docker exec -it mysql-instance mysql -uroot -pmy-secret-pw

Once inside the MySQL shell, run the following (truncated for clarity; full schema includes tables like config_info, tenant_info, etc.):

CREATE DATABASE IF NOT EXISTS nacos_config;
USE nacos_config;

-- Table: config_info
CREATE TABLE `config_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `data_id` varchar(255) NOT NULL COMMENT 'Data ID',
  `group_id` varchar(255) DEFAULT NULL,
  `content` longtext NOT NULL COMMENT 'Content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'MD5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation Time',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification Time',
  `src_user` text COMMENT 'Source User',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'Source IP',
  `app_name` varchar(128) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant Field',
  `c_desc` varchar(256) DEFAULT NULL,
  `c_use` varchar(64) DEFAULT NULL,
  `effect` varchar(64) DEFAULT NULL,
  `type` varchar(64) DEFAULT NULL,
  `c_schema` text,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Config Info';

-- Additional Nacos tables (config_info_aggr, config_info_beta, etc.) go here

-- User/Role/Permission Tables
CREATE TABLE `users` (
  `username` varchar(50) NOT NULL PRIMARY KEY,
  `password` varchar(500) NOT NULL,
  `enabled` boolean NOT NULL
);

CREATE TABLE `roles` (
  `username` varchar(50) NOT NULL,
  `role` varchar(50) NOT NULL,
  UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
);

INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);
INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');

3. Create Nacos Configuration/Log Directories

mkdir -p /root/nacos/conf  # Configuration directory
mkdir -p /root/nacos/logs  # Log directory (optional)

4. Configure Nacos application.properties

Edit /root/nacos/conf/application.properties to connect to the MySQL database:

# Nacos Server Settings
server.servlet.contextPath=/nacos
server.port=8848

# MySQL Datasource Configuration
spring.datasource.platform=mysql
db.num=1
db.url=jdbc:mysql://<server-ip>:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=my-secret-pw

# Connection Pool (HikariCP)
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2

# Naming/CMDB/Metrics Configurations (truncated)
nacos.naming.empty-service.auto-clean=true
nacos.naming.empty-service.clean.initial-delay-ms=50000
nacos.naming.empty-service.clean.period-time-ms=30000

# ... (other Nacos configurations)

5. Run Nacos Container

docker run --name nacos-server \
  -p 8848:8848 -p 9848:9848 -p 9849:9849 \
  --privileged=true \
  --restart=always \
  -e JVM_XMS=128m \
  -e JVM_XMX=128m \
  -e MODE=standalone \
  -e PREFER_HOST_MODE=hostname \
  -e NACOS_SERVER_IP=<server-ip> \
  -v /root/nacos/logs:/home/nacos/logs \
  -v /root/nacos/conf/application.properties:/home/nacos/conf/application.properties \
  -d \
  nacos/nacos-server

6. Access Nacos Console

Navigate to http://<server-ip>:8848/nacos (default credentials: nacos/nacos).

Without Server MySQL (Embedded Database)

1. Pull Nacos Image

docker pull nacos/nacos-server

2. Run Nacos Container (Standalone Mode)

docker run --name nacos-standalone \
  -p 8848:8848 -p 9848:9848 -p 9849:9849 \
  --privileged=true \
  --restart=always \
  -e JVM_XMS=128m \
  -e JVM_XMX=128m \
  -e MODE=standalone \
  -e PREFER_HOST_MODE=hostname \
  -e NACOS_SERVER_IP=<server-ip> \
  -d \
  nacos/nacos-server

3. Access Nacos Console

Same as above: http://<server-ip>:8848/nacos (credentials: nacos/nacos).

Tags: dockernacos

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.