File size: 2,340 Bytes
cbf01bb
 
 
 
 
 
1390cf5
cbf01bb
 
 
 
 
8c9e9d0
 
cbf01bb
3b920ca
cbf01bb
 
 
 
 
 
 
 
 
3b920ca
 
 
 
 
 
 
1390cf5
 
 
8c9e9d0
 
 
1390cf5
 
cbf01bb
8c9e9d0
 
cbf01bb
1390cf5
 
cbf01bb
 
 
b3c0f0d
 
 
 
 
 
1390cf5
 
 
cbf01bb
 
 
 
 
1390cf5
 
cbf01bb
1390cf5
cbf01bb
 
1390cf5
f234852
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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