name: home-databases

services:
  mysql:
    image: mysql:${MYSQL_VERSION:-8.4}
    container_name: home_mysql
    restart: unless-stopped
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    ports:
      - "${MYSQL_PORT:-4100}:3306"
    volumes:
      - /volume1/docker/docker_projects/home_database/mysql:/var/lib/mysql
    networks:
      - database_network
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "mysqladmin ping -h 127.0.0.1 -uroot -p$$MYSQL_ROOT_PASSWORD --silent"
        ]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s


  postgres:
    image: postgres:${POSTGRES_VERSION:-18}
    container_name: home_postgres
    restart: unless-stopped
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "${POSTGRES_PORT:-4200}:5432"
    volumes:
      # PostgreSQL 18+ 建议挂载到上一级目录
      - /volume1/docker/docker_projects/home_database/postgres:/var/lib/postgresql
    networks:
      - database_network
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"
        ]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 20s


  redis:
    image: redis:${REDIS_VERSION:-8.6.5-alpine}
    container_name: home_redis
    restart: unless-stopped
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      REDIS_PASSWORD: ${REDIS_PASSWORD}
    command:
      - redis-server
      - --requirepass
      - ${REDIS_PASSWORD}
      - --appendonly
      - "yes"
      - --appendfsync
      - everysec
      - --save
      - "900"
      - "1"
      - --save
      - "300"
      - "10"
      - --save
      - "60"
      - "10000"
    ports:
      - "${REDIS_PORT:-4300}:6379"
    volumes:
      - /volume1/docker/docker_projects/home_database/redis:/data
    networks:
      - database_network
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "redis-cli -a \"$$REDIS_PASSWORD\" --no-auth-warning ping | grep PONG"
        ]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 10s


networks:
  database_network:
    name: home_database_network
    driver: bridge