# Dockerfile for {project} Odoo {odoo_version}.
# Generated by `odoo-deploy build docker|podman`. The image is
# ENVIRONMENT-AGNOSTIC: no staging/prod differences are baked in — the
# environment is configured at container start from env vars (entrypoint)
# supplied by the compose file + .env of the environment being deployed.
# No hook framework, no doodbalib. Single env→config entrypoint.

# Fully-qualified names: podman does not resolve short names by default.
FROM docker.io/curlimages/curl:8.21.0 AS downloader
ARG TARGETARCH
ARG WKHTMLTOPDF_VERSION=0.12.6.1
ARG WKHTMLTOPDF_AMD64_CHECKSUM='98ba0d157b50d36f23bd0dedf4c0aa28c7b0c50fcdcdc54aa5b6bbba81a3941d'
ARG WKHTMLTOPDF_ARM64_CHECKSUM="b6606157b27c13e044d0abbe670301f88de4e1782afca4f9c06a5817f3e03a9c"
ARG WKHTMLTOPDF_URL="https://github.com/wkhtmltopdf/packaging/releases/download/${{WKHTMLTOPDF_VERSION}}-3/wkhtmltox_${{WKHTMLTOPDF_VERSION}}-3.bookworm_${{TARGETARCH}}.deb"
ARG PAPER_MUCHER_VERSION="v0.7.1"
ARG PAPER_MUCHER_URL="https://github.com/odoo/paper-muncher/releases/download/${{PAPER_MUCHER_VERSION}}/paper-muncher_${{PAPER_MUCHER_VERSION}}_bookworm_${{TARGETARCH}}.deb"
WORKDIR /downloads
# paper-muncher (py3o PDF rendering) is amd64-only; wkhtmltopdf is both.
RUN if [ "$TARGETARCH" = "arm64" ]; then \
        WKHTMLTOPDF_CHECKSUM=$WKHTMLTOPDF_ARM64_CHECKSUM; \
    elif [ "$TARGETARCH" = "amd64" ]; then \
        WKHTMLTOPDF_CHECKSUM=$WKHTMLTOPDF_AMD64_CHECKSUM; \
        curl -SLo papermuncher.deb ${{PAPER_MUCHER_URL}}; \
    else \
        echo "Unsupported architecture: $TARGETARCH" >&2; \
        exit 1; \
    fi \
    && curl -SLo wkhtmltox.deb ${{WKHTMLTOPDF_URL}} \
    && echo "Downloading wkhtmltopdf from: ${{WKHTMLTOPDF_URL}}" \
    && echo "Expected wkhtmltox checksum: ${{WKHTMLTOPDF_CHECKSUM}}" \
    && echo "Computed wkhtmltox checksum: $(sha256sum wkhtmltox.deb | awk '{{ print $1 }}')" \
    && echo "${{WKHTMLTOPDF_CHECKSUM}} wkhtmltox.deb" | sha256sum -c -

FROM docker.io/library/python:{python_version}-slim-bookworm AS base
ARG TARGETARCH

# Create the Odoo user FIRST, so the COPYs below can set ownership directly
# with --chown. A later `chown -R /opt/odoo` would rewrite the metadata of
# every file and duplicate the whole source tree (~3 GB for a full Odoo
# project) into a single image layer. /opt/odoo itself must be owned by odoo:
# the entrypoint creates /opt/odoo/etc/odoo.conf there at container start.
#
# UID/GID default to 1000 (the conventional "odoo" uid) so a CI-built image
# is server-compatible (the deploy's log-dir ownership assumes 1000). Local
# dev builds pass the host user's uid/gid (see resolve_build_ids) so the
# container runs as the builder — bind mounts are writable and `ps` shows
# the right user instead of whoever happens to own uid 1000 on the host.
ARG UID=1000
ARG GID=1000
RUN groupadd -g ${{GID}} odoo && \
    useradd -l -md /home/odoo -s /bin/false -u ${{UID}} -g ${{GID}} odoo && \
    install -d -o odoo -g odoo /opt/odoo /var/lib/odoo

# Runtime OS dependencies (minimal — extend as needed via files/), kept in
# their own small layer. The build-only toolchain (gcc + -dev headers) is
# installed and purged together with the venv build below inside ONE layer,
# so it never occupies a layer of its own (a package purged in a LATER layer
# still counts towards the image size).
RUN apt-get update && apt-get install -y --no-install-recommends \
        fonts-liberation2 \
        postgresql-client poppler-utils \
    && rm -rf /var/lib/apt/lists/*

# wkhtmltopdf + paper-muncher (PDF reports; ported from the doodba image).
COPY --from=downloader /downloads /downloads
RUN apt-get update \
    && apt-get install -y --no-install-recommends /downloads/wkhtmltox.deb \
    && if [ "$TARGETARCH" = "amd64" ] && [ -f /downloads/papermuncher.deb ]; then \
        apt-get install -y --no-install-recommends /downloads/papermuncher.deb; \
    fi \
    && rm -rf /downloads /var/lib/apt/lists/*

WORKDIR /opt/odoo

# Copy the aggregated source + addons + the project requirements. addons/
# entries are relative symlinks into ../parts/... (staged that way by
# odoo-deploy), so the image holds ONE copy of the sources — COPY preserves
# symlinks inside a copied directory and they resolve in-image.
COPY --chown=odoo:odoo parts/ /opt/odoo/parts/
COPY --chown=odoo:odoo addons/ /opt/odoo/addons/
COPY --chown=odoo:odoo requirements.txt /opt/odoo/requirements.txt
{files_copy}

# Create the venv INSIDE the image with the image's own Python. The host venv
# must NOT be copied: its lib/pythonX.Y directory is tied to the host
# interpreter version and its console-script shebangs embed the host venv
# path — copying only works when host Python == image Python, and otherwise
# site-packages become invisible (e.g. "ModuleNotFoundError: No module named
# 'babel'" when the host venv was 3.13 but the image is python:3.12-slim).
# The official python images already ship Python headers in /usr/local.
# The build toolchain (gcc + -dev headers, needed because psycopg2 and
# python-ldap have no wheels and compile against them) is installed and
# purged in this SAME layer: everything it produced is already compiled into
# the venv, and a shipped image must not contain a compiler. NOTE: explicit
# package list, NO globs like 'gcc*' (that would match gcc-12-base, which
# libgcc-s1 needs — the resolver then fails) and NO `apt-get autoremove`
# (it would drop the runtime libraries the compiled extensions link against:
# libpq5, libldap-2.5-0, libsasl2-2, libxml2, libxslt1.1, libjpeg62-turbo,
# zlib1g — all pulled in as deps of the -dev packages and kept here).
# Bookworm-based, so gcc-12/cpp-12 are the right names.
RUN apt-get update && apt-get install -y --no-install-recommends \
        gcc \
        libxml2-dev libxslt1-dev libldap2-dev libsasl2-dev \
        libpq-dev libjpeg-dev zlib1g-dev \
    && python3 -m venv /opt/odoo/.venv \
    && /opt/odoo/.venv/bin/pip install --no-cache-dir \
        {pip_requirements} \
    && apt-get purge -y gcc gcc-12 cpp cpp-12 binutils binutils-x86-64-linux-gnu libbinutils 'lib*-dev' zlib1g-dev \
    && rm -rf /var/lib/apt/lists/*

# Write the env→config entrypoint — AFTER the expensive venv layer, so
# entrypoint tweaks don't invalidate the pip-install cache.
COPY --chown=odoo:odoo entrypoint.py /opt/odoo/entrypoint.py
RUN chmod +x /opt/odoo/entrypoint.py

# Embed the deploy assets (compose stacks + .env templates — no secrets) so a
# server can deploy from the registry alone, without a repo checkout:
#   docker create --name a <image> && docker cp a:/opt/odoo-deploy-assets/. DIR
# or `odoo-deploy deploy --from-image <image>`.
COPY --chown=odoo:odoo deploy-assets/ /opt/odoo-deploy-assets/

VOLUME ["/var/lib/odoo"]
USER odoo

# Neutral runtime defaults only — every environment-specific value
# (WITHOUT_DEMO, ADMIN_PASSWORD, PGDATABASE, SMTP_*, …) comes from env vars
# set by the compose file + .env of the environment at container start;
# the entrypoint writes them into odoo.conf. PYTHONPATH makes `import odoo`
# work for bare-python tools (click-odoo-*, preparedb, …) — odoo itself is
# the source tree in parts/odoo, not pip-installed.
# /opt/paper-muncher/bin is where the paper-muncher deb installs its binary.
ENV PATH="/opt/odoo/.venv/bin:/opt/paper-muncher/bin:$PATH" \
    PYTHONPATH="/opt/odoo/parts/odoo" \
    ODOO_RC="/opt/odoo/etc/odoo.conf"

ENTRYPOINT ["/opt/odoo/entrypoint.py"]
CMD []
