Docker Compose怎么保证其他服务在Nacos完全启动后再启动

首先,docker compose有一个关键字depends_on是可以一定程度解决services之间的依赖问题,但是depends_on仅仅只是指定了services的启动顺序,并不能保证,前置service完全启动后,后置service才启动。

此时,需要另一个关键字叫healthcheck

样例

nacos版本为v2.3.0

yaml 复制代码
  mysql_nacos:
    container_name: mysql_nacos
    build:
      context: .
      dockerfile: ./image/mysql/5.7/Dockerfile
    image: example/mysql:5.7
    env_file:
      - ../env/mysql.env
    volumes:
      - ./mysql:/var/lib/mysql
#    ports:
#      - "3307:3306"
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}" ]
      interval: 10s
      timeout: 10s
      retries: 10
        
  nacos1:
    hostname: nacos1
    container_name: nacos1
    image: nacos/nacos-server:${NACOS_VERSION}
    volumes:
      - ./cluster-logs/nacos1:/home/nacos/logs
#    ports:
#      - "7848:7848"
#      - "8845:8848"
#      - "9868:9848"
#      - "9850:9849"
    env_file:
      - ../env/nacos-hostname.env
#    restart: always
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:8848/nacos/v1/console/health/readiness" ]
      interval: 10s
      timeout: 10s
      retries: 10
    depends_on:
      mysql_nacos:
        condition: service_healthy
        
  nginx_nacos:
    hostname: nginx_nacos
    image: nginx:stable
    container_name: nginx_nacos
    volumes:
      - ../nginx_nacos/nginx.conf:/etc/nginx/nginx.conf
      - ../nginx_nacos/conf.d/default.conf:/etc/nginx/conf.d/default.conf
      - ../nginx_nacos/logs:/var/log/nginx
      - ../nginx_nacos/html:/usr/share/nginx/html
    ports:
      - "80:80"
    depends_on:
      nacos1:
        condition: service_healthy

这是一个双层依赖,nginx_nacos依赖于nacos1,同时nacos1依赖于mysql_nacos

mysql_nacos基于 healthcheck:test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}" ]来确认自己的健康状态,只有当test内容执行成功并返回状态码0,才会认为此service已完全执行成功。

同理nacos服务是基于healthcheck:test: [ "CMD", "curl", "-f", "http://localhost:8848/nacos/v1/console/health/readiness" ]来判断健康状态的。

相关推荐
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工3 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
Alsn863 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
酣大智3 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉3 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦3 天前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
2601_961875243 天前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant