version: "3"
services:
  nextjs-app: # https://sofia.mwitnessing.com/
    hostname: jwpw-app # jwpw-nextjs-app-1
    image: docker.d-popov.com/jwpw:latest
    deploy:
      replicas: 2
    #ports:
    #  - "3000:3000"
    volumes:
      - /mnt/docker_volumes/pw/app/public/content/uploads/:/app/public/content/uploads
      - /mnt/docker_volumes/pw/app/logs:/app/logs
    environment:
      - NODE_ENV=production
      - TZ=Europe/Sofia
      - DATABASE=mysql://jwpwsofia:dwxhns9p9vp248V39xJyRthUsZ2gR9@mariadb:3306/jwpwsofia
      #- DATABASE=postgres://jwpwsofia:dwxhns9p9vp248V39xJyRthUsZ2gR9@mariadb:3306/jwpwsofia
      - UPDATE_CODE_FROM_GIT=true # Set to true to pull latest code from Git
      - GIT_BRANCH=production
      - GIT_USERNAME=deploy
      - GIT_PASSWORD=L3Kr2R438u4F7
      - ADMIN_PASSWORD=changeme
    command: sh -c " cd /app && npm install && npx next build && npm run prod; tail -f /dev/null"
    #command: sh -c " cd /app && tail -f /dev/null"
    tty: true
    stdin_open: true
    restart: always
    networks:
      - default
      - infrastructure_default
  mariadb:
    hostname: mariadb
    image: mysql #bitnami/mariadb:latest #mariadb:10.4
    volumes:
      - /mnt/docker_volumes/pw/data/mysql:/var/lib/mysql
    environment:
      # MARIADB_ROOT_PASSWORD: dwxhns9p9vp248V39xJyRthUsZ2gR9
      MYSQL_ROOT_PASSWORD: i4966cWBtP3xJ7BLsbsgo93C8Q5262
      MYSQL_DATABASE: jwpwsofia
      MYSQL_USER: jwpwsofia
      MYSQL_PASSWORD: dwxhns9p9vp248V39xJyRthUsZ2gR9
    networks:
      - default
      - infrastructure_default
  mariadb_backup:
    image: alpine:latest
    volumes:
      - /mnt/docker_volumes/pw/data/backup:/backup
    #  - ./gdrive_service_account.json:/root/.gdrive_service_account.json
    environment:
      MYSQL_USER: jwpwsofia
      MYSQL_PASSWORD: dwxhns9p9vp248V39xJyRthUsZ2gR9
      MYSQL_DATABASE: jwpwsofia
      MYSQL_HOST: mariadb
      # GOOGLE_DRIVE_FOLDER_ID: your_google_drive_folder_id
    entrypoint: /bin/sh -c
    networks:
      - infrastructure_default
    command: |
      "apk update && \
      apk add --no-cache mariadb-client mariadb-connector-c && \
      echo '0 2 * * * mysqldump -h $$MYSQL_HOST -P 3306 -u$$MYSQL_USER -p$$MYSQL_PASSWORD $$MYSQL_DATABASE > /backup/$$(date +\\%Y-\\%m-\\%d-\\%H\\%M\\%S)-$$MYSQL_DATABASE.sql' > /etc/crontabs/root && \
      crond -f -d 8"
      # wget -q https://github.com/prasmussen/gdrive/releases/download/2.1.0/gdrive-linux-x64 -O /usr/bin/gdrive && \
      # chmod +x /usr/bin/gdrive && \
      # gdrive about --service-account /root/.gdrive_service_account.json && \
      # echo '0 * * * * /usr/bin/mysqldump -h $$MYSQL_HOST -u$$MYSQL_USER -p$$MYSQL_PASSWORD $$MYSQL_DATABASE | gzip > /backup/$$(date +\\%Y-\\%m-\\%d-\\%H\\%M\\%S)-$$MYSQL_DATABASE.sql.gz && gdrive upload --parent $$GOOGLE_DRIVE_FOLDER_ID --service-account /root/.gdrive_service_account.json /backup/$$(date +\\%Y-\\%m-\\%d-\\%H\\%M\\%S)-$$MYSQL_DATABASE.sql.gz' > /etc/crontabs/root && crond -f -d 8 \
      # echo '0 7 * * * rclone sync /backup nextcloud:/mwitnessing --delete-excluded ' >> /etc/crontabs/root && \"
networks:
  infrastructure_default:
    external: true
