Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

4.6 KB
Raw Blame
# syntax=docker/dockerfile:1
# check=skip=InvalidDefaultArgInFrom

# Automatically build image using Python version specified in `pyproject.toml`
ARG API_PY_VERSION

# Get the version from the main justfile
ARG OV_UV_VERSION

FROM ghcr.io/astral-sh/uv:${OV_UV_VERSION} AS uv

##################
# Python builder #
##################

# Pin the Debian release: the audiowaveform .deb below is built for one Debian
# release (the `-13` in its name is Debian 13, trixie). Change both together.
FROM docker.io/python:${API_PY_VERSION}-trixie AS builder

# Container optimizations
ENV PYTHONUNBUFFERED=1
ENV PIP_NO_CACHE_DIR=1
ENV PIP_NO_COLOR=1
# Use the image's Python instead of downloading one
ENV UV_PYTHON_DOWNLOADS=never

# Install system packages needed for building Python dependencies
RUN apt-get update \
  && apt-get install -yqq --no-install-recommends python3-dev \
  && apt-get autoremove -y \
  && rm -rf /var/lib/apt/lists/*

COPY --from=uv /uv /bin/uv

# Copy subpackages from additional build-context 'packages'
# hadolint ignore=DL3022
COPY --from=packages openverse-attribution /packages/python/openverse-attribution

# Mirror the repository layout, so that the `../packages` path to
# openverse-attribution in `pyproject.toml` resolves, and keep the virtualenv
# at `/.venv`, where the API stage expects it.
WORKDIR /api
ENV UV_PROJECT_ENVIRONMENT=/.venv

# Copy the Python project manifest and uv lockfile into the container
COPY pyproject.toml uv.lock /api/

# Pass additional arguments when installing Python packages with uv
ARG UV_SYNC_ARGS="--no-editable"

# Install Python dependencies into a new virtualenv at `/.venv`
RUN uv sync --locked $UV_SYNC_ARGS

#######
# API #
#######

FROM docker.io/python:${API_PY_VERSION}-slim-trixie AS api

LABEL org.opencontainers.image.source="https://github.com/WordPress/openverse"

# Container optimizations
ENV PYTHONUNBUFFERED=1
ENV PIP_NO_CACHE_DIR=1
ENV PIP_NO_COLOR=1

# Activate the virtualenv
ENV PATH="/.venv/bin:$PATH"

WORKDIR /api

# Copy virtualenv from the builder image
COPY --from=builder /.venv /.venv

ARG BUILDARCH
ARG AUDIOWAVEFORM_RELEASE=1.10.2
ARG AUDIOWAVEFORM_DEB=audiowaveform_${AUDIOWAVEFORM_RELEASE}-1-13_${BUILDARCH}.deb

# - Install system packages needed for running Python dependencies
# - Create directory for dumping API logs
# - apt --fix-broken install required to install missing dependencies for audiowaveform and audiowaveform itself
#   dpkg -i marks the dependencies for installation, apt-get installs them
#   we need to ignore error output from dpkg -i for this reason, so run
#   audiowaveform afterwards to fail the build if it wasn't installed
RUN apt-get update \
  && apt-get install -yqq --no-install-recommends \
    curl \
    postgresql-client \
  && curl -sLO https://github.com/bbc/audiowaveform/releases/download/${AUDIOWAVEFORM_RELEASE}/${AUDIOWAVEFORM_DEB} \
  && (dpkg -i ${AUDIOWAVEFORM_DEB} || apt-get --fix-broken -y --no-install-recommends install) \
  && audiowaveform --version \
  && apt-get autoremove -y \
  && rm -rf /var/lib/apt/lists/* ${AUDIOWAVEFORM_DEB} \
  && mkdir -p /var/log/openverse_api/openverse_api.log

# Create a folder for placing static files
RUN mkdir /static

# Create a non-root user, and make it the owner of the static dir created above
RUN useradd --create-home ov_user \
  && chown -R ov_user /static
USER ov_user

# Copy subpackages from additional build-context 'packages'
# hadolint ignore=DL3022
COPY --chown=ov_user --from=packages openverse-attribution /packages/python/openverse-attribution/

# Copy code into the final image
COPY --chown=ov_user . /api/

# Collect static assets, these are used by the next stage, `nginx`
RUN env \
  SETUP_ES="False" \
  DJANGO_SECRET_KEY="any string" \
  CANONICAL_DOMAIN="" \
  python manage.py collectstatic

# Add the release version to the docker container
ARG SEMANTIC_VERSION
ENV SEMANTIC_VERSION=$SEMANTIC_VERSION

# Exposes
# - 8000: Dev server for API Django app
EXPOSE 8000

# Wait for ES to accept connections
ENTRYPOINT ["./run.sh"]

CMD ["python", "run.py"]

#########
# NGINX #
#########

FROM docker.io/nginx:1.30.8-alpine AS nginx

LABEL org.opencontainers.image.source="https://github.com/WordPress/openverse"

WORKDIR /app

COPY nginx/nginx.conf.template /etc/nginx/templates/openverse-api.conf.template
COPY nginx/snippets /etc/nginx/snippets

# Copy static files from `api` target
COPY --from=api /static /app/static

# Only environment variables with this prefix will be available in the template
ENV NGINX_ENVSUBST_FILTER="DJANGO_NGINX_"
ENV DJANGO_NGINX_ENVIRONMENT="local"
# Add the release version to the docker container
ARG SEMANTIC_VERSION
ENV DJANGO_NGINX_GIT_REVISION=$SEMANTIC_VERSION