Spaces:
Running
Running
### ------ | |
### The builder image, used to build the virtual environment | |
### ------ | |
# Start with a minimal Python 3.11 base image based on Debian Buster | |
FROM python:3.12.7-slim | |
### ------ | |
### Set up user | |
### ------ | |
RUN useradd -m -u 1000 user | |
### ------ | |
### Set up Python environment variables | |
### ------ | |
# Ensures Python output is sent directly to the terminal (no buffering) | |
ENV PYTHONUNBUFFERED=1 \ | |
# Prevents Python from writing .pyc files (compiled bytecode) | |
PYTHONDONTWRITEBYTECODE=1 \ | |
### pip configuration to optimize the installation process | |
# Do not cache pip packages | |
PIP_NO_CACHE_DIR=off \ | |
# Disable pip version checking | |
PIP_DISABLE_PIP_VERSION_CHECK=on \ | |
# Set a longer timeout for pip commands (100 seconds) | |
PIP_DEFAULT_TIMEOUT=100 \ | |
### poetry configuration to install and manage dependencies | |
# Set the version of Poetry to use | |
POETRY_VERSION=1.4.2 \ | |
# Define the location where Poetry will be installed | |
POETRY_HOME="/opt/poetry" \ | |
#POETRY_HOME="/home/user/.local" \ | |
# Create the virtual environment inside the project folder | |
POETRY_VIRTUALENVS_IN_PROJECT=true \ | |
# Ensure Poetry creates virtual environments for the project | |
POETRY_VIRTUALENVS_CREATE=1 \ | |
# Prevent Poetry from asking for interactive input during install | |
POETRY_NO_INTERACTION=1 \ | |
### Define paths for the virtual environment and Python setup | |
# Define where Python setup files will reside | |
PYSETUP_PATH="/opt/pysetup" \ | |
# Define where the virtual environment will be created | |
VENV_PATH="/opt/pysetup/.venv" \ | |
# Set Hugging Face to offline mode to avoid network access | |
HF_HUB_OFFLINE=0 \ | |
# NLTK | |
NLTK_DATA="/home/user/nltk/" \ | |
# Store files | |
DATA_DIR="/api/data/" \ | |
# Set the custom Poetry configuration directory | |
POETRY_CONFIG_DIR="$POETRY_HOME/.config/pypoetry" \ | |
# Crypt | |
PASSLIB_BUILTIN_BCRYPT="enabled" | |
# Add Poetry and virtual environment to the system PATH (Note add after declared ENV) | |
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" | |
### ------ | |
### Package | |
### ------ | |
# Update package lists and install required build dependencies | |
RUN apt-get update \ | |
&& apt-get install --no-install-recommends -y \ | |
# Install curl to download files from the internet | |
curl \ | |
# Install essential build tools (for building Poetry and other packages) | |
build-essential \ | |
wget \ | |
git \ | |
openssh-client \ | |
&& curl -sSL https://install.python-poetry.org | python3 - \ | |
# Remove build tools after installation to reduce image size | |
&& apt-get purge -y --auto-remove build-essential \ | |
# Clean up apt cache to minimize image size | |
&& apt-get clean \ | |
# Remove leftover lists to further reduce image size | |
&& rm -rf /var/lib/apt/lists/* | |
WORKDIR / | |
### ------ | |
### Copy from Repo | |
### ------ | |
RUN mkdir -p /langworkflow | |
RUN --mount=type=secret,id=TAG_WORKFLOW,mode=0444,required=true \ | |
--mount=type=secret,id=URL_WORKFLOW,mode=0444,required=true \ | |
git clone --branch $(cat /run/secrets/TAG_WORKFLOW) $(cat /run/secrets/URL_WORKFLOW) /langworkflow | |
RUN mkdir -p /api | |
RUN --mount=type=secret,id=TAG_PROJECT,mode=0444,required=true \ | |
--mount=type=secret,id=URL_PROJECT,mode=0444,required=true \ | |
git clone --branch $(cat /run/secrets/TAG_PROJECT) $(cat /run/secrets/URL_PROJECT) /api | |
### ------ | |
### Create DIRs | |
### ------ | |
# Create the custom Poetry configuration directory and set permissions | |
RUN mkdir -p $POETRY_CONFIG_DIR && \ | |
# Create the NTLK folder | |
mkdir -p $NLTK_DATA && \ | |
# Create the folder store files | |
mkdir -p $DATA_DIR && \ | |
chown -R user:user $POETRY_CONFIG_DIR && \ | |
chown -R user:user $NLTK_DATA && \ | |
chown -R user:user $DATA_DIR && \ | |
chmod -R 777 $DATA_DIR | |
### ------ | |
### Install Libs | |
### ------ | |
WORKDIR /api | |
RUN poetry install --no-root && rm -rf $POETRY_CACHE_DIR | |
### ------ | |
### Command to run | |
### ------ | |
USER user | |
# Expose the port FastAPI will run on | |
EXPOSE 7860 | |
# This command starts the API using Poetry's virtual environment | |
CMD ["poetry", "run", "api"] | |