version: "3.9"

services:
  web:
    image: php:8.3-apache
    container_name: blogpostai_web
    ports:
      - "8080:80"
    volumes:
      - ./app:/var/www/html
      - ./logs:/var/log/apache2
    env_file:
      - .env
    environment:
      - TZ=Asia/Taipei
    depends_on:
      - db
    command: >
      bash -c "docker-php-ext-install pdo pdo_mysql mysqli &&
      pecl install apcu && docker-php-ext-enable apcu &&
      a2enmod rewrite && a2enmod headers &&
      echo '<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName localhost
        DocumentRoot /var/www/html/public
        <Directory /var/www/html/public>
          Options -Indexes +FollowSymLinks
          AllowOverride All
          Require all granted
          DirectoryIndex index.php index.html
        </Directory>
        ErrorLog \$${APACHE_LOG_DIR}/error.log
        CustomLog \$${APACHE_LOG_DIR}/access.log combined
      </VirtualHost>' > /etc/apache2/sites-available/000-default.conf &&
      apache2-foreground"
    networks:
      - blogpostai_network

  db:
    image: mysql:8.4
    container_name: blogpostai_db
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=blogpostai_db
      - MYSQL_USER=blogpostai_user
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - TZ=Asia/Taipei
    volumes:
      - db_data:/var/lib/mysql
      - ./sql:/docker-entrypoint-initdb.d
    networks:
      - blogpostai_network

  phpmyadmin:
    image: phpmyadmin:latest
    container_name: blogpostai_pma
    ports:
      - "8081:80"
    environment:
      - PMA_HOST=db
      - PMA_USER=blogpostai_user
      - PMA_PASSWORD=${MYSQL_PASSWORD}
      - UPLOAD_LIMIT=256M
      - PMA_ARBITRARY=0
    depends_on:
      - db
    networks:
      - blogpostai_network

volumes:
  db_data:
    driver: local

networks:
  blogpostai_network:
    driver: bridge
