version: "3"
# docker-compose.yml
#
# A Docker compose placeholder container for the database migration and server.
# Currently intended for the domain test.defacto2.net.
#
# $ docker compose up --build
# $ docker compose up --detach
#
# $ docker compose run webapp --version
# $ docker compose run webapp address
# $ docker compose run webapp config
#
# $ docker compose exec webapp ls -l /root/.config
#
services:
  webapp:
    container_name: defacto2-webapp
    restart: unless-stopped

    # build gets run using the --build flag or build command
    build:
      context:
        . # use the current directory for the build context
        # an inline dockerfile can be used instead of a Dockerfile
      dockerfile_inline: |
        FROM alpine:latest
        RUN mkdir /root/.config
        RUN apk --no-cache add curl unzip
        WORKDIR /tmp
        RUN cd /tmp && curl -L -O https://github.com/Defacto2/server/releases/download/v0.4.0/df2-server_0.4.0_linux.zip
        RUN unzip /tmp/df2-server_0.4.0_linux.zip && mv /tmp/df2-server /usr/local/bin/df2-server
        RUN /usr/local/bin/df2-server --version
        RUN rm /tmp/df2-server_0.4.0_linux.zip /tmp/defacto2.txt
        EXPOSE 1323
        ENTRYPOINT ["df2-server"]
    ports:
      - "${D2_HTTP_PORT}:1323" # HTTP (unencrypted) port that the server listens on

    environment:
      # PS_HOST_NAME value must be kept as host.docker.internal
      # otherwise the server will not be able to connect
      # to the docker containerized postgresql database
      PS_HOST_NAME: "host.docker.internal"
      # Database username used to connect
      PS_USERNAME: root
      # Password for the database username
      PS_PASSWORD: example
      # D2_PRODUCTION_MODE enables production mode to log errors and recover from any panics
      D2_PRODUCTION_MODE: "true"
      # D2_DIR_[NAME] are internal directory volumes and must not be changed
      D2_DOWNLOAD_DIR: "/srv/downloads"
      D2_PREVIEW_DIR: "/srv/screenshots"
      D2_THUMBNAIL_DIR: "/srv/thumbnails"

    extra_hosts:
      - "host.docker.internal:host-gateway" # this is required for the server to connect
        # to the docker containerized postgresql database

    volumes:
      # store the server configuration and logs in a named volume
      # this will be kept and accessible even if the container is removed
      - webapp-config:/root/.config
      # the local directories located on the host are bound to the /opt directory in the container
      # EDIT the .env file to change the location of these directories
      - ${D2_DOWNLOAD_DIR}:/srv/downloads # the directory where the server will store artifact downloads
      - ${D2_PREVIEW_DIR}:/srv/screenshots # the directory where the server will store previews
      - ${D2_THUMBNAIL_DIR}:/srv/thumbnails # the directory where the server will store thumbnails

volumes:
  webapp-config:
# other services to add?
# dns_search:
# domainname: test.defacto2.net
