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

时间:2018-04-06 17:14:13

标签: docker docker-compose

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

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

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

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

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

我使用了健康检查和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命令