docker-compose部署nacos

 1、docker-compose内容

高版本的nacos使用docker启动,需要将所有的端口放开,仅仅开放8848端口,spring-boot客户端获取nacos配置的时候,可能取到的内容为空。

version: '3'

# 定义自定义网络,确保服务间通信和外部访问
networks:
  seata-network:
    driver: bridge

services:
  mysql:
    image: mysql:8.0.33
    container_name: mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=sample_db
      - MYSQL_DATABASE=nacos_config  # 创建Nacos配置库
    volumes:
      - ./sql:/docker-entrypoint-initdb.d
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-proot"]
      interval: 5s
      timeout: 5s
      retries: 10
    networks:
      - seata-network

  nacos:
    image: nacos/nacos-server:v2.5.1
    container_name: nacos
    ports:
      - "8848:8848"      # Nacos控制台和API端口
      - "9848:9848"      # Nacos客户端gRPC端口
      - "9849:9849"      # Nacos客户端gRPC端口(备用)
      - "7848:7848"      # Nacos配置推送端口(如果需要)
    environment:
      - MODE=standalone
      - MYSQL_SERVICE_HOST=mysql
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_DB_NAME=nacos_config
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=root
    volumes:
      - ./nacos/init.sql:/docker-entrypoint-initdb.d/init.sql  # 初始化Nacos配置
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos/actuator/health"]
      interval: 5s
      timeout: 50s
      retries: 20
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - seata-network

  seata-server:
    image: seataio/seata-server:2.0.0
    container_name: seata-server
    ports:
      - "8091:8091"  # Seata TC服务端口
    volumes:
      - ./seata/config/registry.conf:/seata-server/resources/registry.conf
      - ./seata/config/file.conf:/seata-server/resources/file.conf
    environment:
      - SEATA_IP=0.0.0.0  # 允许所有IP访问
      - SEATA_PORT=8091
      - REGISTRY_TYPE=nacos
      - REGISTRY_NACOS_SERVER_ADDR=nacos:8848
      - REGISTRY_NACOS_GROUP=SEATA_GROUP
      - CONFIG_TYPE=nacos
      - CONFIG_NACOS_SERVER_ADDR=nacos:8848
      - CONFIG_NACOS_GROUP=SEATA_GROUP
    healthcheck:
      test: ["CMD", "sh", "-c", "ps -ef | grep -v grep | grep seata"]
      interval: 5s
      timeout: 50s
      retries: 10
    depends_on:
      nacos:
        condition: service_healthy
    networks:
      - seata-network

  sample-service:
    build: ./sample-service
    container_name: sample-service
    ports:
      - "9090:9090"
    environment:
      - SEATA_HOST=seata-server
      - SEATA_PORT=8091
      - NACOS_SERVER_ADDR=nacos:8848
    depends_on:
      seata-server:
        condition: service_healthy
      nacos:
        condition: service_healthy
    networks:
      - seata-network

2、example-service微服务例子 

你可能感兴趣的:(docker,容器,运维)