# syntax=docker/dockerfile:1 ARG PYTHON_VERSION=3.11 FROM python:${PYTHON_VERSION}-slim AS python-base ARG TEST_ENV # Set environment variables ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PORT=${PORT:-9090} \ PIP_CACHE_DIR=/.cache \ WORKERS=1 \ THREADS=8 \ PATH="/home/user/.local/bin:$PATH" # Update the base OS (this must run as root) RUN --mount=type=cache,target="/var/cache/apt",sharing=locked \ --mount=type=cache,target="/var/lib/apt/lists",sharing=locked \ set -eux; \ apt-get update; \ apt-get upgrade -y; \ apt install --no-install-recommends -y \ git; \ apt-get autoremove -y # Set up a new user named "user" with user ID 1000 RUN useradd -m -u 1000 user # Switch to the non-root user USER user WORKDIR /home/user/app # Create a writable directory for SQLite RUN mkdir -p /home/user/app/data && chmod 777 /home/user/app/data # Upgrade pip RUN pip install --no-cache-dir --upgrade pip # Install base requirements COPY --chown=user requirements-base.txt . RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ pip install -r requirements-base.txt && \ gunicorn --version # Install custom requirements COPY --chown=user requirements.txt . RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ pip install -r requirements.txt # Download the model during the build process RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ python -c "from transformers import AutoModelForSequenceClassification, AutoTokenizer; \ AutoModelForSequenceClassification.from_pretrained('bert-base-multilingual-cased').save_pretrained('/home/user/app/model'); \ AutoTokenizer.from_pretrained('bert-base-multilingual-cased').save_pretrained('/home/user/app/model')" # Install test requirements if needed COPY --chown=user requirements-test.txt . # Build only when TEST_ENV="true" RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ if [ "$TEST_ENV" = "true" ]; then \ pip install -r requirements-test.txt; \ fi # Copy all application files and set ownership COPY --chown=user . . # Expose the service port EXPOSE 9090 # Command to run the application CMD PYTHONPATH=/home/user/app gunicorn --preload --bind :$PORT --workers $WORKERS --threads $THREADS --timeout 0 _wsgi:app