QutBioacoustics/baw-server

View on GitHub
docker-compose.yml

Summary

Maintainability
Test Coverage
# now using `docker compose` (over  `docker-compose`) and
# the version is the latest schema defined by the docker compose specification.
# https://github.com/compose-spec/compose-spec/blob/master/spec.md#Requirements-and-optional-attributes

x-gh_actions:
  # trial addtion for github actions
  &gh_actions
  group_add:
    - "116"
    - "1001"

# to closer mock production we mount all shared storage (NFS shares in prod) into the same spot
# in all the containers. In this case /data should always be the internal path for this shared mount.
# Client side we mount to ./data.
# In each case a sub-folder is used to isolated environments.
x-data-mount-dev:
  &data_mount_dev
  type: bind
  source: ./data/development
  target: /data/development
x-data-mount-test:
  &data_mount_test
  type: bind
  source: ./data/test
  target: /data/test

services:
  redis:
    image: redis:6-alpine
    ports:
      - "6379:6379"
    networks:
      - baw_network

  db:
    image: postgres:14
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - baw_network
    environment:
      # never, ever, ever use this is a production setup!
      # Only use this in a dev environment
      POSTGRES_PASSWORD: password

  # Note: if the sftpgo service fails to start because a table already exists,
  # it is because the database has the tables
  # but none of the migration info in it. Delete the tables with
  # `rails baw:truncate_sftpgo_tables`.
  # Error from logs:
  #   Unable to initialize/update the data provider: pq: relation "sftpgo_schema_version" already exists
  upload:
    &upload
    <<: *gh_actions
    # Default command is `sftpgo serve`
    #command: sftpgo serve
    image: drakkan/sftpgo:v2-alpine
    depends_on:
      - db
    environment:
      RAILS_ENV: development
      SFTPGO_DEFAULT_ADMIN_USERNAME: admin
      SFTPGO_DEFAULT_ADMIN_PASSWORD: password
    networks:
      baw_network: null
    ports:
      - "8080:8080"
      - "2022:2022"
      - "8090:8090"
    restart: on-failure
    volumes:
      - type: bind
        source: ./provision/upload/config
        target: /var/lib/sftpgo
        # mount the harvester to do directory - this is where we want to allow uploads
        # the rails app knows about this directory from config (actions/harvest/to_do_path)
      - <<: *data_mount_dev

  upload_test:
    <<: *upload
    environment:
      RAILS_ENV: test
      SFTPGO_DATA_PROVIDER__NAME: baw_local_test
      SFTPGO_DEFAULT_ADMIN_USERNAME: admin
      SFTPGO_DEFAULT_ADMIN_PASSWORD: password
    networks:
      baw_network:
        aliases:
          - upload.test
    ports:
      # note this only changes host mapping, internal port is still 8080 and what is used by the web service's tests
      - "8081:8080"
      - "2023:2022"
      - "8091:8090"
    volumes:
      - type: bind
        source: ./provision/upload/config
        target: /var/lib/sftpgo
      - <<: *data_mount_test

  workers:
    &worker
    <<: *gh_actions
    build:
      context: .
      dockerfile: ./Dockerfile
    command: >-
      rerun -- bin/baw-workers baw:worker:run
    depends_on:
      - db
      - redis
    environment:
      RAILS_ENV: development
    networks:
      - baw_network
    restart: on-failure
    volumes:
      - type: bind
        source: ./
        target: /home/baw_web/baw-server
      - <<: *data_mount_dev
    working_dir: /home/baw_web/baw-server

  workers_test:
    &worker_test
    <<: *worker
    volumes:
      - type: bind
        source: ./
        target: /home/baw_web/baw-server
      - <<: *data_mount_test
    environment:
      RAILS_ENV: test

  scheduler:
    <<: *worker
    command: >-
      rerun -- bin/baw-workers baw:worker:run_scheduler
  scheduler_test:
    <<: *worker_test
    command: >-
      rerun -- bin/baw-workers baw:worker:run_scheduler
    environment:
      RAILS_ENV: test

  web:
    <<: *gh_actions
    build:
      context: .
      dockerfile: ./Dockerfile
    #command: bundle exec rails server
    # I'm thinking this might be a better default command. Currently the compose
    # file is only used for devleopment and we usually don't want a web server
    # just starting - particularly in CI where we need the port for tests
    # TODO: this seems to ignore signals and takes 10 seconds (the default timeout) to stop whenever docker stop is issues
    command: sleep infinity
    init: true
    healthcheck:
      test: test -f .ready
      interval: 5s
      timeout: 3s
      retries: 12
      start_period: 30s
    depends_on:
      - db
      - redis
      - upload
      - upload_test
      - workers
      - workers_test
      - scheduler
      - scheduler_test
    environment:
      RAILS_ENV: development
      MIGRATE_DB: "true"
      # pass through values
      NEXT_VERSION: ~
      CHANGELOG_GITHUB_TOKEN: ~
    user: baw_web
    volumes:
      - type: bind
        source: ./
        target: /home/baw_web/baw-server
      - <<: *data_mount_dev
      - <<: *data_mount_test
    ports:
      - "3000:3000"
      # rdebug-ide port
      - "1234:1234"
      - "26162:26162"
    networks:
      - baw_network
networks:
  baw_network: null
volumes:
  # We're using a volume mount for tmp because it offers better performance
  # than a bind mount. It also does not seem to suffer from cache/metadata
  # problems that bind mounts (see caching in cifs/nfs).
  # The downside to this is that we can no longer access files in /tmp on
  # out hosts for dev work.
  # 2020-09 update: we're now running in WSL2/linux hosts only. The disadvantages
  #  of bind mounts may be negated. Additionally we want to mount sub-directories in
  #  tmp (the default bind location for this volume) in different images which is tricky.
  #  Thus trialing running without volume
  #baw_docker_shared_storage:
  postgres-data: null