Build Axiom Docker image

July 28, 2024 (1mo ago)

Fully integrated Web App: axiom.punitarani.com

Building the docker image for Axiom to run the API and backend on my server.

Initial Dockerfile

FROM python:3.12-slim
 
WORKDIR /app
 
# Copy the pyproject.toml and poetry.lock files to the container
COPY pyproject.toml poetry.lock* /app/
 
# Install Poetry and dependencies
RUN pip install poetry
RUN poetry config virtualenvs.create false && poetry install --no-dev --no-interaction --no-ansi
 
# Copy the rest of the application code
COPY . /app
 
# Make port available to the world outside this container
EXPOSE 8123
 
# Run the application
CMD ["uvicorn", "server.main:app", "--host", "0.0.0.0", "--port", "8123"]

hdf5 install error

7.439   running build_ext
7.439   Loading library to get build settings and version: libhdf5.so
7.439   error: Unable to load dependency HDF5, make sure HDF5 is installed properly
7.439   Library dirs checked: []
7.439   error: libhdf5.so: cannot open shared object file: No such file or directory
7.439   
7.439 
7.439   at /usr/local/lib/python3.12/site-packages/poetry/installation/chef.py:164 in _prepare
7.441       160│ 
7.441       161│                 error = ChefBuildError("\n\n".join(message_parts))
7.441       162│ 
7.441       163│             if error is not None:
7.441     → 164│                 raise error from None
7.441       165│ 
7.441       166│             return path
7.441       167│ 
7.441       168│     def _prepare_sdist(self, archive: Path, destination: Path | None = None) -> Path:
7.441 
7.441 Note: This error originates from the build backend, and is likely not a problem with poetry but with h5py (3.11.0) not supporting PEP 517 builds. You can verify this by running 'pip wheel --no-cache-dir --use-pep517 "h5py (==3.11.0)"'.
7.441 
------
Dockerfile:10
--------------------
   8 |     # Install Poetry and dependencies
   9 |     RUN pip install poetry
  10 | >>> RUN poetry config virtualenvs.create false && poetry install --no-dev --no-interaction --no-ansi
  11 |     
  12 |     # Copy the rest of the application code
--------------------
ERROR: failed to solve: process "/bin/sh -c poetry config virtualenvs.create false && poetry install --no-dev --no-interaction --no-ansi" did not complete successfully: exit code: 1

Solution

Use python-3.12-slim-bullseye instead of python-3.12-slim.

Then, also install the libhdf5-dev package.

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libhdf5-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

Final working Dockerfile

FROM python:3.12-slim-bullseye
 
WORKDIR /app
 
# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libhdf5-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*
 
# Copy the pyproject.toml and poetry.lock files to the container
COPY pyproject.toml poetry.lock* /app/
 
# Install Poetry and dependencies
RUN pip install poetry
RUN poetry config virtualenvs.create false && poetry install --no-dev --no-interaction --no-ansi
 
# Copy the rest of the application code
COPY . /app
 
# Make port available to the world outside this container
EXPOSE 8123
 
# Run the application
CMD ["uvicorn", "server.main:app", "--host", "0.0.0.0", "--port", "8123"]

Build the image

Turns out you can't simply build a Docker image on your local machine. You need to use buildx to build a multi-platform image.

docker buildx build --platform linux/amd64 -t punitarani/axiom --push .

For now, I'll just build it for linux amd64 to run on my laptop server.

This is for later, but why is it over 10GB?