# This is a sample docker-compose file that can be used to run Joplin Server
# along with a PostgreSQL server.
#
# Update the following fields in the stanza below:
#
# POSTGRES_USER
# POSTGRES_PASSWORD
# APP_BASE_URL
#
# APP_BASE_URL: This is the base public URL where the service will be running.
#	- If Joplin Server needs to be accessible over the internet, configure APP_BASE_URL as follows: https://example.com/joplin. 
#	- If Joplin Server does not need to be accessible over the internet, set the APP_BASE_URL to your server's hostname. 
#     For Example: http://[hostname]:22300. The base URL can include the port.
# APP_PORT: The local port on which the Docker container will listen. 
#	- This would typically be mapped to port to 443 (TLS) with a reverse proxy.
#	- If Joplin Server does not need to be accessible over the internet, the port can be mapped to 22300.

networks:
    app-network:
    transcribe-network:
    shared-network:

services:
    db:
        image: postgres:16
        profiles:
            - full
            - server
        volumes:
            - ./data/postgres:/var/lib/postgresql/data
        networks:
            - app-network
        ports:
            - "5432:5432"
        restart: unless-stopped
        environment:
            - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
            - POSTGRES_USER=${POSTGRES_USER}
            - POSTGRES_DB=${POSTGRES_DATABASE}
    app:
        image: joplin/server:latest
        profiles:
            - full
            - server
        depends_on:
            - db
            - transcribe
        ports:
            - "22300:22300"
        networks:
            - app-network
            - shared-network
        restart: unless-stopped
        environment:
            - APP_PORT=22300
            - APP_BASE_URL=${APP_BASE_URL}
            - DB_CLIENT=pg
            - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
            - POSTGRES_DATABASE=${POSTGRES_DATABASE}
            - POSTGRES_USER=${POSTGRES_USER}
            - POSTGRES_PORT=${POSTGRES_PORT}
            - POSTGRES_HOST=db
            - TRANSCRIBE_API_KEY=${TRANSCRIBE_API_KEY}
            - TRANSCRIBE_BASE_URL=http://transcribe:4567
            - TRANSCRIBE_ENABLED=${TRANSCRIBE_ENABLED}
    transcribe-db:
        image: postgres:16
        profiles:
            - full
        volumes:
            - ./data/transcribe-postgres:/var/lib/postgresql/data
        networks:
            - transcribe-network
        ports:
            - "${QUEUE_DATABASE_PORT}:5432"
        restart: unless-stopped
        environment:
            - POSTGRES_PASSWORD=${QUEUE_DATABASE_PASSWORD}
            - POSTGRES_USER=${QUEUE_DATABASE_USER}
            - POSTGRES_DB=${QUEUE_DATABASE_NAME}
        command: -p ${QUEUE_DATABASE_PORT}
    transcribe:
        image: joplin/transcribe:latest
        profiles:
            - full
        volumes:
            - ${HTR_CLI_IMAGES_FOLDER}:/app/packages/transcribe/images
            - ${HTR_CLI_MODELS_FOLDER}:/opt/models:ro
        depends_on:
            - transcribe-db
        ports:
            - "4567:4567"
        networks:
            - transcribe-network
            - shared-network
        restart: unless-stopped
        # Security: limit resources to prevent runaway processes
        deploy:
            resources:
                limits:
                    memory: 16G
                    cpus: '4'
        # Security: read-only root filesystem with only images folder writable
        read_only: true
        tmpfs:
            - /tmp
        environment:
            - APP_PORT=4567
            - DB_CLIENT=pg
            - QUEUE_DATABASE_NAME=${QUEUE_DATABASE_NAME}
            - QUEUE_DATABASE_USER=${QUEUE_DATABASE_USER}
            - QUEUE_DATABASE_PASSWORD=${QUEUE_DATABASE_PASSWORD}
            - QUEUE_DATABASE_PORT=${QUEUE_DATABASE_PORT}
            - QUEUE_DATABASE_HOST=transcribe-db
            - API_KEY=${TRANSCRIBE_API_KEY}
            - HTR_CLI_IMAGES_FOLDER=/app/packages/transcribe/images
            - HTR_CLI_MODELS_FOLDER=/opt/models

