# Based on: https://github.com/astral-sh/uv-docker-example/blob/main/multistage.Dockerfile

# First, build the application in the `/app` directory.
FROM ghcr.io/astral-sh/uv:python{{ python_version }}-bookworm-slim AS builder
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy

# 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

WORKDIR /app

COPY --from=oss python_modules ../dagster/python_modules
COPY --from=internal dagster-cloud/python_modules/dagster-cloud ../dagster-internal/dagster-cloud/python_modules/dagster-cloud
COPY --from=internal dagster-cloud/python_modules/dagster-cloud-cli ../dagster-internal/dagster-cloud/python_modules/dagster-cloud-cli

RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --frozen --no-install-project --no-dev
ADD . /app
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev --package {{ package_arg }}


# Then, use a final image without uv
FROM python:{{ python_version }}-slim-bookworm
# It is important to use the image that matches the builder, as the path to the
# Python executable must be the same.

# Copy the application from the builder
COPY --from=builder /app /app

# And also the editable installs
COPY --from=builder /dagster /dagster
COPY --from=builder /dagster-internal /dagster-internal

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

WORKDIR /app

# Make sure dagster-cloud is installed. Fail early here if not.
RUN if ! dagster-cloud --version; then \
        echo "Could not find the dagster-cloud package.  Make sure you include the dagster-cloud package in your project."; \
        exit 1; \
    fi