services:
  db:
    image: mariadb:10.4

    # Set a fixed container name, so it does not depend on the directory name
    container_name: webwork2_db_1

    volumes:
      - mysql:/var/lib/mysql

      # Set up UTF8MB4 in config file for the container.
      # Needs to be done BEFORE the database is created.
      # *** Some other MariaDB settings were modified in the sample file:
      #        wait_timeout,  interactive_timeout, max_connections, net_read_timeout
      #        + skip-name-resolve
      # Note: It seems different versions of the MariaDB container need
      #       this file in different locations. Put in in both places.
      - './docker-config/db/mariadb.cnf:/etc/mysql/conf.d/mariadb.cnf'
      - './docker-config/db/mariadb.cnf:/etc/mysql/mariadb.cnf'

      # Tuning MariaDB config - sample file
      # The sample file modifies wait_timeout, and adds skip-name-resolve
      #- "./docker-config/db/my.cnf:/etc/mysql/my.cnf"

      # Adjust open file limits for MariaDB/mySQL - sample file
      #- "./docker-config/db/limits.conf:/etc/security/limits.conf"

      # Provides read only access to the host system's /etc/localtime - tested on Linux hosts
      #- "/etc/localtime:/etc/localtime:ro"

      # The ulimits lines were only tested on Linux hosts in conjuction woth the limits.conf file
      #ulimits:
      #  nofile:
      #    soft: 4096
      #    hard: 4096

    restart: always
    environment:
      # When the MariaDB container is first started it will set the
      # the MYSQL_ROOT_PASSWORD if there is no mysql database in the
      # data volume.
      MYSQL_ROOT_PASSWORD: ${WEBWORK_MYSQL_ROOT_PASSWORD}

      # When the MariaDB container is first started it will create
      #    the WW database and WW DB user based on:
      MYSQL_DATABASE: webwork
      MYSQL_USER: ${WEBWORK_DB_USER}
      MYSQL_PASSWORD: ${WEBWORK_DB_PASSWORD}

      # A timezone for the DB server can be set:
      #TZ: zone/city
      # where zone/city must be a valid setting.
      # "/usr/bin/timedatectl list-timezones" on an Ubuntu system with
      # that tool installed will find valid values.
      # See: https://stackoverflow.com/questions/39172652/using-docker-compose-to-set-containers-timezones

  app:
    # Modifying the image name can be helpful to create a new image instead of
    # overwriting the "standard" one during testing.
    image: webwork

    # Set a fixed container name, so it does not depend on the directory name
    container_name: webwork2_app_1

    # Set up the "build:" configuration:
    build:
      # For use/building when docker-compose.yml is in the webwork2 directory
      context: .
      # For use/building when docker-compose.yml is OUTSIDE the webwork2 directory.
      # For example, if multiple hosts use a NFS shared webwork2/ directory, and
      # each one needs customized values in docker-compose.yml.
      # Under typical use, the Dockerfile should not need to be customized per host,
      # but may contain some changes/additions relative to the standard webwork image.
      #
      #  context: /Path_To/webwork2/
      #  dockerfile: /Path_To/Dockerfile
      args:
        # Build args to set the source Git repositories. Here we set the main repositories:
        - WEBWORK2_GIT_URL=https://github.com/openwebwork/webwork2.git
        - PG_GIT_URL=https://github.com/openwebwork/pg.git

        # Build args specifying the branches of webwork2 and pg to install.

        # Exactly one pair of settings should be left uncommented and all the others
        # should be commented out.

        # main branches:
        - WEBWORK2_BRANCH=main
        - PG_BRANCH=main

        # WeBWorK/PG develop branches
        #    (other valid branches can also be used in a similar manner)
        #- WEBWORK2_BRANCH=develop
        #- PG_BRANCH=develop

      # If you would like a 1 stage build process comment out the next line, and just run "docker-compose build".
      dockerfile: DockerfileStage2
      # For the 2 stage build process run
      #     docker build --tag webwork-base:forWW220 -f DockerfileStage1 .
      # and then
      #     docker-compose build
      # When rebuilding to get updated images add the "--no-cache" option to both commands.
      # You can add something like
      #         --build-arg ADDITIONAL_BASE_IMAGE_PACKAGES="nano less"
      # to the stage 1 "docker build" command to include extra OS packages in the base image.
      # That approach makes them (persistent) so available immediately each time a container is
      # started and is thus faster than adding them in when a container is started via the
      # ADD_APT_PACKAGES setting below.

    depends_on:
      - db
      - r

    volumes:
      # ======================================================================

      # If you are using locally modified webwork2 files, then
      # either mount them from the webwork2 tree from which you start Docker:
      #- ".:/opt/webwork/webwork2"
      # OR mount it from a fixed external location
      #- "/path_to/webwork2:/opt/webwork/webwork2"

      # Shared main /pg repository - allows local PG development
      #- "/path_to_shared/pg:/opt/webwork/pg"
      # OR locally modified PG directory
      #- "../pg:/opt/webwork/pg"

      # ======================================================================

      # Sometimes it is helpful to mount certain webwork2/conf files from elsewhere
      # so the main contents of webwork2 can be shared by several hosts (ex. NFS)
      #- "/per_host_conf_path/conf/authen_LTI.conf:/opt/webwork/webwork2/conf/authen_LTI.conf"
      #- "/per_host_conf_path/conf/localOverrides.conf:/opt/webwork/webwork2/conf/localOverrides.conf"
      #- "/per_host_conf_path/conf/site.conf:/opt/webwork/webwork2/conf/site.conf"

      # webwork2 misc LOCAL files - mount live (per host) so NOT in the main webwork2 location
      #- "/per_host_conf_path/htdocs/my_site_info.txt:/opt/webwork/webwork2/htdocs/my_site_info.txt"

      # webwork2 LOCAL logs and htdocs/tmp directories (per host)
      #- "/per_host_data_path/webwork2/logs:/opt/webwork/webwork2/logs"
      #- "/per_host_data_path/webwork2/htdocs/tmp:/opt/webwork/webwork2/htdocs/tmp"

      # By default mount the courses tree in a separate tree outside of webwork2/ as follows:
      - '${COURSES_DIRECTORY_ON_HOST}:/opt/webwork/courses'
      # OR use the following mount if the hosts have different courses on them:
      #- "/per_host_data_path/courses/:/opt/webwork/courses/"

      # ======================================================================

      # Mojolicious webwork2 app configuration.  For production you will need to modify this file.
      # Note that the SSL configuration is done in this file.
      # See https://docs.mojolicious.org/Mojo/Server/Daemon#listen for how to do this.
      #- "/per_host_conf_path/conf/webwork2.mojolicious.yml:/opt/webwork/webwork2/conf/webwork2.mojolicious.yml"

      # SSL certificates (subdirectory with certificate and key)
      # Place your ssl certificate files used in webwork2.mojolicious.yml above in docker-config/ssl,
      # or change the mount location to where your ssl certificate files reside.
      #- "./docker-config/ssl/:/etc/ssl/local"

      # ======================================================================

      # By default the OPL is stored in a named Docker storage volume:
      - oplVolume:/opt/webwork/libraries/webwork-open-problem-library
      #
      # as an alternative, you can comment out the prior option and uncomment the line below to use
      # a local directory containing the OPL
      #- "/path_to/webwork-open-problem-library:/opt/webwork/libraries/webwork-open-problem-library"

      # ======================================================================

    hostname: myhost.mydomain.edu

    ports:
      # For a personal machine
      - '${WEBWORK2_HTTP_PORT_ON_HOST}:8080'

      # For a production machine
      #- "80:8080"
      #- "443:8080"

    # For a production machine
    #restart: always
    stop_signal: SIGWINCH
    stop_grace_period: 30s

    environment:
      # Standard database environment variables needed by WeBWorK:

      # Select which DBD driver to use
      WEBWORK_DB_DRIVER: MariaDB
      # WEBWORK_DB_DRIVER: mysql

      WEBWORK_DB_HOST: db
      WEBWORK_DB_PORT: 3306
      WEBWORK_DB_NAME: webwork

      # These are set in the .env file and import values from there
      WEBWORK_DB_PASSWORD: ${WEBWORK_DB_PASSWORD}
      WEBWORK_DB_USER: ${WEBWORK_DB_USER}

      # =======================================================
      # Local configuration variables:

      # To turn on SSL in the running container
      #SSL: 1

      # Control settings for error message handling by webwork2/lib/WeBWorK.pm
      # The standard behavior is with both of these set to 0
      MIN_HTML_ERRORS: 0
      JSON_ERROR_LOG: 0

      # Change to A4 paper
      #PAPERSIZE: a4

      # Use to build additional locales in the running container at startup. Ex:
      #ADD_LOCALES: "he_IL ISO-8859-8\nhe_IL.UTF-8 UTF-8\n"

      # Extra Ubuntu packages to install during container startup.
      #
      # Uncommenting the ADD_APT_PACKAGES line and put the list of additional
      # Ubuntu packages to install on the line.
      #
      # This is best used when you have a temporary need for additional
      # packages, as using the ADD_APT_PACKAGES option will slow down the
      # container startup time.
      #
      # OS packages you want frequently available are better added using the
      # build time ADDITIONAL_BASE_IMAGE_PACKAGES setting described above.
      #ADD_APT_PACKAGES: nano less

      # The system timezone for the container can be set using
      #SYSTEM_TIMEZONE: zone/city
      # where zone/city must be a valid setting.
      # "/usr/bin/timedatectl list-timezones" on an Ubuntu system with
      # that tool installed will find valid values.

      # =======================================================

      # If you use https below, make sure to set up the certificate and SSL configuration
      # Note if your server uses a non-standard port, that should also be included.
      #WEBWORK_ROOT_URL: https://myhost.mydomain.edu

      #WEBWORK_SMTP_SERVER: smtp.mydomain.edu
      #WEBWORK_SMTP_SENDER: support@mydomain.edu

      #WEBWORK_TIMEZONE: America/New_York

  r:
    image: ubcctlt/rserve
    # The R server need not be available from outside the local Docker network.
    #ports:
    #  - "6311:6311"

  # SimpleSAMLphp Saml2 identity provider for development use only. This is a separate profile from the other services
  # so it doesn't start in normal usage. Use "docker compose --profile saml2dev up" to start, "docker compose --profile
  # saml2dev stop" to stop services, and "docker compose --profile saml2dev down" to stop services and remove
  # containers.
  idp:
    build:
      context: ./docker-config/idp/
    profiles:
      - saml2dev
    ports:
      - '8180:80'
    environment:
      SP_METADATA_URL: 'http://app:8080/webwork2/saml2/metadata'
    # The healthcheck url is SimpleSAMLphp's url for triggering cron jobs. The cron job it triggers will
    # automatically fetch the webwork2 service provider's metadata.
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost/simplesaml/module.php/cron/run/metarefresh/webwork2']
      start_period: 1m
      start_interval: 15s
      interval: 1h
      retries: 1
      timeout: 10s

volumes:
  oplVolume:
    driver: local
    name: webwork2_oplVolume
  mysql:
    driver: local
    name: webwork2_mysql
