基于Docker-Compose 安装Nacos

Common property configuration

name description option
MODE cluster模式/standalone模式 cluster/standalone default cluster
NACOS_SERVERS nacos cluster地址 eg. ip1,ip2,ip3
PREFER_HOST_MODE 是否支持hostname hostname/ip default ip
NACOS_SERVER_PORT nacos服务器端口 default 8848
NACOS_SERVER_IP 多网卡下的自定义nacos服务器IP
SPRING_DATASOURCE_PLATFORM standalone 支持 mysql mysql / empty default empty
MYSQL_MASTER_SERVICE_HOST mysql 主节点host
MYSQL_MASTER_SERVICE_PORT mysql 主节点端口 default : 3306
MYSQL_MASTER_SERVICE_DB_NAME mysql 主节点数据库
MYSQL_MASTER_SERVICE_USER 数据库用户名
MYSQL_MASTER_SERVICE_PASSWORD 数据库密码
MYSQL_SLAVE_SERVICE_HOST mysql从节点host
MYSQL_SLAVE_SERVICE_PORT mysql从节点端口 default :3306
MYSQL_DATABASE_NUM 数据库数量 default :2
JVM_XMS -Xms default :2g
JVM_XMX -Xmx default :2g
JVM_XMN -Xmn default :1g
JVM_MS -XX:MetaspaceSize default :128m
JVM_MMS -XX:MaxMetaspaceSize default :320m
NACOS_DEBUG 开启远程调试 y/n default :n
TOMCAT_ACCESSLOG_ENABLED server.tomcat.accesslog.enabled default :false

Mysql 模式

Mysql模式会将配置文件保存在mysql数据库中,避免因为nacos容器被删除而导致配置文件的丢失。

将以下脚本保存在docker-compose.yaml文件中,执行docker-compose -f docker-compose.yaml up -d命令。注意必须和数据库一起安装

  1. version: '3'
  2. services:
  3. mysql:
  4. image: mysql:5.7
  5. container_name: mysql-test
  6. command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
  7. restart: always
  8. environment:
  9. MYSQL_ROOT_PASSWORD: root
  10. MYSQL_DATABASE: nacos
  11. ports:
  12. - 3306:3306
  13. volumes:
  14. - /mydata/mysql/data/db:/var/lib/mysql
  15. - /mydata/mysql/data/conf:/etc/mysql/conf.d
  16. - /mydata/mysql/log:/var/log/mysql
  17. privileged: true
  18. nacos-registry:
  19. image: nacos/nacos-server:1.4.0
  20. container_name: nacos-registry-test
  21. volumes:
  22. - /mydata/nacos/logs:/home/nacos/logs
  23. environment:
  24. - MODE=standalone
  25. - SPRING_DATASOURCE_PLATFORM=mysql
  26. - MYSQL_SERVICE_HOST=mysql-test
  27. - MYSQL_SERVICE_DB_NAME=nacos
  28. - MYSQL_SERVICE_PORT=3306
  29. - MYSQL_SERVICE_USER=root
  30. - MYSQL_SERVICE_PASSWORD=root
  31. - management.endpoints.web.exposure.include=*
  32. ports:
  33. - 8848:8848
  34. privileged: true

单机模式 Derby

Derby自带数据库,信息保存在nacos项目中集成的数据库中,不保存在mysql数据库。缺点是删除nacos容器后,配置文件数据会被删除。

将以下脚本保存在docker-compose.yaml文件中,执行docker-compose -f docker-compose.yaml up -d命令

  1. version: '3'
  2. services:
  3. mysql:
  4. image: mysql:5.7
  5. container_name: mysql
  6. command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
  7. restart: always
  8. environment:
  9. MYSQL_ROOT_PASSWORD: root #设置root帐号密码
  10. ports:
  11. - 3306:3306
  12. volumes:
  13. - /mydata/mysql/data/db:/var/lib/mysql #数据文件挂载
  14. - /mydata/mysql/data/conf:/etc/mysql/conf.d #配置文件挂载
  15. - /mydata/mysql/log:/var/log/mysql #日志文件挂载
  16. privileged: true
  17. redis:
  18. image: redis:5
  19. container_name: redis
  20. command: redis-server --appendonly yes
  21. volumes:
  22. - /mydata/redis/data:/data #数据文件挂载
  23. ports:
  24. - 6379:6379
  25. nginx:
  26. image: nginx:1.10
  27. container_name: nginx
  28. volumes:
  29. - /mydata/nginx/nginx.conf:/etc/nginx/nginx.conf #配置文件挂载
  30. - /mydata/nginx/html:/usr/share/nginx/html #静态资源根目录挂载
  31. - /mydata/nginx/log:/var/log/nginx #日志文件挂载
  32. ports:
  33. - 80:80
  34. rabbitmq:
  35. image: rabbitmq:3.7.15-management
  36. container_name: rabbitmq
  37. volumes:
  38. - /mydata/rabbitmq/data:/var/lib/rabbitmq #数据文件挂载
  39. - /mydata/rabbitmq/log:/var/log/rabbitmq #日志文件挂载
  40. ports:
  41. - 5672:5672
  42. - 15672:15672
  43. privileged: true
  44. elasticsearch:
  45. image: elasticsearch:7.6.2
  46. container_name: elasticsearch
  47. user: root
  48. environment:
  49. - "cluster.name=elasticsearch" #设置集群名称为elasticsearch
  50. - "discovery.type=single-node" #以单一节点模式启动
  51. - "ES_JAVA_OPTS=-Xms512m -Xmx512m" #设置使用jvm内存大小
  52. volumes:
  53. - /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
  54. - /mydata/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
  55. ports:
  56. - 9200:9200
  57. - 9300:9300
  58. privileged: true
  59. logstash:
  60. image: logstash:7.6.2
  61. container_name: logstash
  62. environment:
  63. - TZ=Asia/Shanghai
  64. volumes:
  65. - /mydata/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf #挂载logstash的配置文件
  66. depends_on:
  67. - elasticsearch #kibana在elasticsearch启动之后再启动
  68. links:
  69. - elasticsearch:es #可以用es这个域名访问elasticsearch服务
  70. ports:
  71. - 4560:4560
  72. - 4561:4561
  73. - 4562:4562
  74. - 4563:4563
  75. kibana:
  76. image: kibana:7.6.2
  77. container_name: kibana
  78. links:
  79. - elasticsearch:es #可以用es这个域名访问elasticsearch服务
  80. depends_on:
  81. - elasticsearch #kibana在elasticsearch启动之后再启动
  82. environment:
  83. - "elasticsearch.hosts=http://es:9200" #设置访问elasticsearch的地址
  84. ports:
  85. - 5601:5601
  86. privileged: true
  87. mongo:
  88. image: mongo:4.2.5
  89. container_name: mongo
  90. volumes:
  91. - /mydata/mongo/db:/data/db #数据文件挂载
  92. ports:
  93. - 27017:27017
  94. privileged: true
  95. nacos-registry:
  96. image: nacos/nacos-server:1.4.0
  97. container_name: nacos-registry
  98. environment:
  99. - MODE=standalone
  100. ports:
  101. - 8848:8848
  102. privileged: true