# syntax=docker/dockerfile:1.7

# From https://raw.githubusercontent.com/reflex-dev/reflex/refs/heads/main/docker-example/production-one-port/Dockerfile

# This Dockerfile is used to deploy a single-container Reflex app instance
# to services like Render, Railway, Heroku, GCP, and others.

# If the service expects a different port, provide it here (f.e Render expects port 10000)
ARG PORT=8080
ARG PYTHON_VERSION=3.13
# Only set for local/direct access. When TLS is used, the API_URL is assumed to be the same as the frontend.
ARG API_URL
ARG BUILDER_IMAGE=ghcr.io/astral-sh/uv:python${PYTHON_VERSION}-bookworm-slim
ARG RUN_IMAGE=python:${PYTHON_VERSION}-slim-bookworm

# It uses a reverse proxy to serve the frontend statically and proxy to backend
# from a single exposed port, expecting TLS termination to be handled at the
# edge by the given platform.
FROM ${BUILDER_IMAGE} AS builder
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy

# Omit development dependencies
ENV UV_NO_DEV=1

# Disable Python downloads, because we want to use the system interpreter
# across both images. If using a managed Python version, it needs to be
# copied from the build image into the final image; see `standalone.Dockerfile`
# for an example.
ENV UV_PYTHON_DOWNLOADS=0

# Place executables in the environment at the front of the path
ENV PATH="/app/.venv/bin:$PATH"

RUN mkdir -p /app/.web
WORKDIR /app

# Copy dependency manifests first to maximize cache reuse.
# Wildcards make uv.lock/requirements.txt optional while still supporting either flow.
COPY pyproject.toml* uv.lock* requirements.txt* /app/

RUN --mount=type=cache,target=/root/.cache/uv \
    set -eux; \
    if [ -f uv.lock ]; then \
        [ -f pyproject.toml ] || (echo "uv.lock found but pyproject.toml is missing" >&2; exit 1); \
        uv sync --locked --no-install-project; \
    elif [ -f requirements.txt ]; then \
        uv venv /app/.venv; \
        uv pip install --python /app/.venv/bin/python --requirement requirements.txt; \
    else \
        echo "Neither uv.lock nor requirements.txt found in build context" >&2; \
        exit 1; \
    fi

# Install reflex helper utilities like bun/node
COPY rxconfig.py ./
RUN reflex init

# Install pre-cached frontend dependencies (if exist)
COPY *.web/bun.lockb *.web/package.json .web/
RUN if [ -f .web/bun.lockb ]; then cd .web && ~/.local/share/reflex/bun/bin/bun install --frozen-lockfile; fi

# Copy local context to `/app` inside container (see .dockerignore)
COPY . /app
RUN --mount=type=cache,target=/root/.cache/uv \
    set -eux; \
    if [ -f uv.lock ]; then \
        uv sync --locked; \
    fi

ARG PORT API_URL
# Download other npm dependencies and compile frontend
RUN REFLEX_API_URL=${API_URL:-http://localhost:$PORT} reflex export --loglevel debug --frontend-only --no-zip && mv .web/build/client/* /srv/ && rm -rf .web


# Final image with only necessary files
FROM ${RUN_IMAGE}

# Install Caddy and redis server inside image
RUN apt-get update -y && apt-get install -y caddy redis-server && rm -rf /var/lib/apt/lists/*

ARG PORT API_URL
ENV PATH="/app/.venv/bin:$PATH" PORT=$PORT REFLEX_API_URL=${API_URL:-http://localhost:$PORT} REFLEX_REDIS_URL=redis://localhost PYTHONUNBUFFERED=1

WORKDIR /app
COPY --from=builder /app /app
COPY --from=builder /srv /srv

# Needed until Reflex properly passes SIGTERM on backend.
STOPSIGNAL SIGKILL

EXPOSE $PORT

# Apply migrations before starting the backend.
CMD [ -d alembic ] && reflex db migrate; \
    caddy start && \
    redis-server --daemonize yes && \
    exec reflex run --env prod --backend-only
