Docker编写 – 在启动许多服务时防止CPU抖动

我正在使用Docker compose来调出大约15个
Java容器.当一次启动一个时,每个容器大约需要8秒钟才能启动.一起启动所有容器会使CPU最大化,并且比通过shell脚本顺序启动服务要慢.

容器有一个HEALTHCHECK,所以我知道当容器状态健康时它们已完全初始化.

我想限制并行启动的容器数量.

我已经尝试设置COMPOSE_PARALLEL_LIMIT但它似乎在启动第一个服务后冻结.我也不认为它正在等待容器进入“健康”状态.

怎么能实现这一目标?

最佳答案 我使用过healthchecks和depends_on

version: '2.1'
services:

  artifactory-8081:
    hostname: artifactory-8081
    image: docker.bintray.io/jfrog/artifactory-pro:6.3.0
    container_name: artifactory-8081
    volumes:
      - ./data/artifactory-8081:/var/opt/jfrog/artifactory
      - ./data/artifactory-8081-extra:/artifactory_extra_conf
    ports:
     - 8081:8081
    environment:
     - EXTRA_JAVA_OPTIONS=-Xms512m -Xmx1g -Xss256k -XX:+UseG1GC
    restart: always
    healthcheck:
      test: ["CMD", "curl", "-f", "-s", "http://localhost:8081/artifactory/api/system/ping"]
      interval: 30s
      timeout: 5s
      retries: 20

  artifactory-8082:
    hostname: artifactory-8082
    image: docker.bintray.io/jfrog/artifactory-pro:6.3.0
    container_name: artifactory-8082
    volumes:
      - ./data/artifactory-8082:/var/opt/jfrog/artifactory
      - ./data/artifactory-8082-extra:/artifactory_extra_conf
    ports:
     - 8082:8081
    environment:
     - EXTRA_JAVA_OPTIONS=-Xms512m -Xmx1g -Xss256k -XX:+UseG1GC
    restart: always
    healthcheck:
      test: ["CMD", "curl", "-f", "-s", "http://localhost:8081/artifactory/api/system/ping"]
      interval: 30s
      timeout: 5s
      retries: 20
    depends_on:
      artifactory-8081:
        condition: service_healthy

  artifactory-8083:
    hostname: artifactory-8083
    image: docker.bintray.io/jfrog/artifactory-pro:6.3.0
    container_name: artifactory-8083
    volumes:
      - ./data/artifactory-8083:/var/opt/jfrog/artifactory
      - ./data/artifactory-8083-extra:/artifactory_extra_conf
    ports:
     - 8083:8081
    environment:
     - EXTRA_JAVA_OPTIONS=-Xms512m -Xmx1g -Xss256k -XX:+UseG1GC
    restart: always
    healthcheck:
      test: ["CMD", "curl", "-f", "-s", "http://localhost:8081/artifactory/api/system/ping"]
      interval: 30s
      timeout: 5s
      retries: 20
    depends_on:
      artifactory-8082:
        condition: service_healthy

使用这样的docker-compose.yml,它将逐个容器地启动.你必须使用docker-compose 2.1而不是3.x,因为它们在depends_on中被删除了对条件的支持.您还必须使用自己的healthchecks命令

点赞