File size: 4,082 Bytes
c5c767d
 
 
 
 
626ee71
c5c767d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d86eab1
c5c767d
 
 
d86eab1
c5c767d
 
f4a554d
c5c767d
 
d86eab1
c5c767d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c436684
 
 
8aaf661
 
 
730a622
f137595
 
730a622
 
9682108
730a622
f137595
 
730a622
8aaf661
9682108
c5c767d
 
 
 
c436684
9682108
c436684
 
 
 
 
 
 
 
c5c767d
 
d86eab1
c5c767d
 
c436684
9682108
c5c767d
 
 
 
 
c436684
3ed3654
c5c767d
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
### ------
### 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"]