Skip to content

Commit

Permalink
fix(containerfile): Finish changing containerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
devsjc committed Oct 25, 2024
1 parent d7dc7a3 commit 3bc32ff
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Build a virtualenv using uv
FROM python:3.12-slim-bookworm AS build-venv
# Build a virtualenv using miniconda
# * Conda creates a completely isolated environment,
# including all required shared libraries, enabling
# just putting the virtualenv into a distroless image
# without having to faff around with linking all
# the filelist (including for each dependency) of
# https://packages.debian.org/trixie/libpython3.12-dev, e.g.
#
# echo "Copying symlinked python binary into venv" && \
# cp --remove-destination /usr/local/bin/python3.12 /venv/bin/python && \
# echo "Copying libpython package into venv" && \
# cp -r /usr/local/lib/* /venv/lib/ && \
# cp -r /usr/local/include/python3.12/* /venv/include/ && \
# mkdir -p /venv/lib/aarch64-linux-gnu/ && \
# cp -r /usr/lib/aarch64-linux-gnu/* /venv/lib/aarch64-linux-gnu/ && \
# mkdir -p /venv/include/aarch64-linux-gnu/ && \
# cp -r /usr/include/aarch64-linux-gnu/* /venv/include/aarch64-linux-gnu/ && \

FROM quay.io/condaforge/miniforge3:latest AS build-venv

COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

Expand All @@ -12,40 +29,34 @@ COPY pyproject.toml /_lock/

# Synchronize DEPENDENCIES without the application itself.
# This layer is cached until uv.lock or pyproject.toml change.
# Also copy the symlinked python and shared libraries into the virtualenv
# Delete any unwanted parts of the installed packages to reduce size
RUN --mount=type=cache,target=/root/.cache \
apt install gcc && \
echo "Creating virtualenv at /venv" && \
conda create -qy -p /venv python=3.12 numcodecs && \
echo "Installing dependencies into /venv" && \
cd /_lock && \
mkdir src && \
uv sync --no-dev --no-install-project
uv sync --no-dev --no-install-project && \
echo "Optimizing /venv site-packages" && \
rm -r /venv/lib/python3.12/site-packages/**/tests && \
rm -r /venv/lib/python3.12/site-packages/**/_*cache*


# Then install the application itself
# * Copy over the required shared libraries into the venv
# * Delete the test and cache folders from installed packages
# * Delete the test and cache folders from installed packages to reduce size
COPY . /src
RUN --mount=type=cache,target=/root/.cache \
uv pip install --no-deps --python=$UV_PROJECT_ENVIRONMENT /src && \
cp -r /usr/local/lib/python3.12 /venv/lib/ && \
cp --remove-destination /usr/local/bin/python3.12 /venv/bin/python && \
cp /usr/local/lib/libpython3.12.so.1.0 /venv/lib/ && \
rm -r /venv/lib/python3.12/site-packages/**/tests && \
rm -r /venv/lib/python3.12/site-packages/**/_*cache* && \
rm /venv/lib/python3.12/site-packages/**/libscipy_*.so || true
uv pip install --no-deps --python=$UV_PROJECT_ENVIRONMENT /src

# cp --remove-destination /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1 /lib/ && \
# Copy the virtualenv into a distroless image
# * These are small images that only contain the runtime dependencies
FROM gcr.io/distroless/python3-debian11
WORKDIR /app
COPY --from=build-venv /venv /venv
COPY --from=build-venv /usr/lib /usr/lib
COPY --from=build-venv /lib /lib

ENV PATH=/venv/bin:$PATH \
RAWDIR=/work/raw \
ENV RAWDIR=/work/raw \
ZARRDIR=/work/data \
ECCODES_DEFINITION_PATH=/venv/share/eccodes/definitions \
PYTHONHOME=/venv
ECCODES_DEFINITION_PATH=/venv/share/eccodes/definitions

ENTRYPOINT ["/venv/bin/nwp-consumer-cli"]
VOLUME /work
Expand Down

0 comments on commit 3bc32ff

Please sign in to comment.