Administrator
发布于 2025-09-28 / 4 阅读
0
0

docker-compose常用配置

健康检查 healthcheck

test:健康测试
interval:间隔执行测试时间
retries:尝试次数
start_period:开始执行测试的等待时间,等待容器先启动

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3
  start_period: 40s

服务依赖depends_on

表明服务之间的依赖关系

  • service_started:依赖服务启动

  • service_healthy:依赖服务完成健康检查

  • service_completed_successfully:依赖服务完全运行成功

简单方式

等价于service_started

version: '2.2'
services:
  kibana:
    image: docker.elastic.co/kibana/kibana:7.2.0
    container_name: kibana7.2.0
    hostname: kibana
    environment:
     - elasticsearch.hosts=http://elasticsearch:9200
    depends_on: # !依赖服务,不是依赖容器
     - elasticsearch
    restart: always
    ports:
      - "5601:5601"
    # 容器运行时执行命令 exec模式
    command: ["bundle", "exec", "thin", "-p", "3000"]

详细写法

services:
  basic-init:
    container_name: basic-init
    hostname: basic-init
    image: busybox:1.36
    command: >
      sh -c 'until nc -z postgresql 5432 && nc -z redis 6379; do echo waiting for basic service `date`; sleep 3; done;'
  service0:
	depends_on:
      service1:
      	# service0依赖service1启动
        condition: service_started
      service2:
      	# service0依赖service2健康检查完成
        condition: service_healthy
	  basic-init: 
	  	# basic-init依赖service2完全启动
		condition: service_completed_successfully

执行命令command和entrypoint

version: '3'
services:
  myservice:
    image: myimage
    entrypoint: /path/to/my/entrypoint.sh
#   或
#    command: bash -c "/path/to/my/entrypoint.sh"

docker网络使用

docker network create --subnet 172.88.88.0/24 v6aiot

使用指定网络

rabbitmq指定使用v6aiot网络

networks:
  v6aiot:
services:
  rabbitmq:
    image: rabbitmq:3.13.0-rd
    hostname: rabbitmq
    restart: always
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=das@123!
    networks:
      - "v6aiot"

第二种实现方式,在docker-compose.yml文件顶部定位的网络中声明external: true,整个文件所有服务默认使用定义的服务

networks:
  default:
    name: v6aiot
    external: true
services:
  rabbitmq:
    image: rabbitmq:3.13.0-rd
    hostname: rabbitmq
    restart: always
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=das@123!

指定子网内的IP

networks:
  default:
    name: v6aiot
    external: true
services:
 accountmgr-api:
   image: rabbitmq:3.13.0-rd
   networks:
     default:
       ipv4_address: 172.88.88.2

使用指定域名aliases

使用redis.com来连接redis服务名的服务

  redis:
    image: 192.168.100.28:8088/library/redis:6.2.5
    networks:
      v6aiot:
        aliases:
          - redis.com

限制cpu内存deploy

services:
  das-adaptive-station:
    image: 192.168.100.28:8088/scenes/das-adaptive-station:dev-ea38b5d
    restart: on-failure:3
    network_mode: host
    privileged: true
    extra_hosts:
      nacos.rd.das-app.com: 120.78.145.58
      ds-dev.das-app.com: 172.168.70.249
    environment:
      - SPRING_PROFILES_ACTIVE=dev
    volumes:
      - "../volumes/logs/das:/usr/local/logs/das"
      - "../volumes/uploads:/usr/local/qianhai/uploads"
      - "../volumes/padOperation:/usr/local/padOperation"
      - "../volumes/store:/usr/local/store"
      - "../volumes/fonts:/usr/share/fonts"
    deploy:
      resources:
        limits:
          cpus: '4'
          memory: 1024M

docker-compose链接启动多个文件

docker-compose  -f docker-compose.yml  -f platform.yml -f baseapp.yml  -f opendev.yml up -d

创建.env文件

# .env
COMPOSE_PROJECT_NAME=das
COMPOSE_FILE=docker-compose.yml:platform.yml:baseapp.yml:opendev.yml

完整例配置

[root@node226 aiot]# cat .env 
# 项目名
COMPOSE_PROJECT_NAME=das
# yml文件分隔符
COMPOSE_PATH_SEPARATOR=:
# yml文件列表
COMPOSE_FILE=ai-platform.yml:baseapp.yml:docker-compose.yml:ems.yml:fms.yml:instance.yml:ios.yml:iss.yml:opendev.yml:park.yml:platform.yml:ssc.yml:vas.yml
[root@node226 aiot]# cat docker-compose.yml
networks:
  default:
    name: v6aiot
    external: true
  basic-init:
    container_name: basic-init
    hostname: basic-init
    image: busybox:1.36
    command: >
      sh -c 'until nc -z postgres 5432 && nc -z redis 6379 && nc -z tdengine 6041; do echo waiting for basic service `date`; sleep 3; done;'
    depends_on:
      postgres:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
      redis:
        condition: service_started
  postgres:
    image: postgres:15
    restart: always
    shm_size: 4g
    environment:
      - POSTGRES_PASSWORD=das@123!
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD", "su", "-", "postgres", "-c", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    volumes:
      - "/opt/data/appdata/postgres/data:/var/lib/postgresql/data"
      - "/opt/data/appdata/postgres/init:/docker-entrypoint-initdb.d"
      - "/opt/data/appdata/postgres/conf/postgresql.conf:/etc/postgresql/postgresql.conf"
    command: ["-c", "config_file=/etc/postgresql/postgresql.conf"]
    deploy:
      resources:
        limits:
          memory: 8192M
  iot-device-api:
    image: dasaiot/iot-device-api:V700R000C00CP001
    restart: always
    env_file:
      - ./env/aiot.env
    environment:
      SPRING_APPLICATION_NAME: iot-device-api
    healthcheck:
      test: ["CMD", "curl", "-f", "http://127.0.0.1:9090/metrics/health"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    volumes:
      - "/usr/share/zoneinfo:/usr/share/zoneinfo"
      - "./plugins:/app/plugins"
      - "./logs/iot-device-api:/tmp/logs"
    networks:
      default:
        aliases:
          - iot-device-api.default


评论