version: '3.8'

services:
  mysql:
    image: mysql:8.0
    container_name: nestjs_mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: nestjs_db
      MYSQL_USER: nestjs_user
      MYSQL_PASSWORD: nestjs_password
    ports:
      - '3306:3306'
    volumes:
      - mysql_data:/var/lib/mysql
      - ./mysql-init:/docker-entrypoint-initdb.d
    networks:
      - nestjs_network
    healthcheck:
      test: ['CMD', 'mysqladmin', 'ping', '-h', 'localhost']
      timeout: 20s
      retries: 10

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: nestjs_app
    restart: unless-stopped
    ports:
      - '3000:3000'
    environment:
      NODE_ENV: development
      PORT: 3000
      DB_HOST: mysql
      DB_PORT: 3306
      DB_USERNAME: nestjs_user
      DB_PASSWORD: nestjs_password
      DB_DATABASE: nestjs_db
    depends_on:
      mysql:
        condition: service_healthy
    volumes:
      - .:/app
      - /app/node_modules
    networks:
      - nestjs_network
    command: npm run start:dev

volumes:
  mysql_data:
    driver: local

networks:
  nestjs_network:
    driver: bridge
