|
1 | | -FROM python:3.7 |
| 1 | +# Stage 1: Build stage (dependencies and compilation) |
| 2 | +FROM python:3.12-slim as build |
2 | 3 |
|
3 | 4 | # Create the required folders |
4 | 5 | RUN mkdir -p /webapp/models |
5 | 6 |
|
6 | | -# Copy everything |
| 7 | +# Copy the application code |
7 | 8 | COPY . /webapp |
8 | 9 |
|
| 10 | +# Install dependencies for building (git, etc.) |
| 11 | +RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 12 | + git \ |
| 13 | + build-essential \ |
| 14 | + apt-utils \ |
| 15 | + cron \ |
| 16 | + sqlite3 \ |
| 17 | + libsqlite3-dev |
| 18 | + |
| 19 | +# Install Python dependencies |
| 20 | +ARG USE_CPU_TORCH=false |
| 21 | +# NOTE: Allow building without GPU so as to lower image size (disabled by default) |
| 22 | +RUN pip install -U pip && if [ "$USE_CPU_TORCH" = "true" ]; then \ |
| 23 | + pip install -r /webapp/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu/; \ |
| 24 | + else \ |
| 25 | + pip install -r /webapp/requirements.txt; \ |
| 26 | + fi |
| 27 | + |
| 28 | +# Get the spacy model (for later copy) |
| 29 | +RUN python -m spacy download en_core_web_md |
| 30 | + |
| 31 | +# Stage 2: Final (production) image |
| 32 | +FROM python:3.12-slim as final |
| 33 | + |
| 34 | +# Install runtime dependencies (you don’t need git in production) |
| 35 | +RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 36 | + cron \ |
| 37 | + sqlite3 \ |
| 38 | + libsqlite3-dev && apt-get autoremove |
| 39 | + |
| 40 | +# Create the required folders (if not created already) |
| 41 | +RUN mkdir -p /webapp/models && mkdir -p /medcat_data |
| 42 | + |
| 43 | +# Copy only necessary files from build stage |
| 44 | +COPY --from=build /webapp /webapp |
| 45 | +# COPY --from=build /root/.cache /root/.cache # Copy pip cache if needed |
| 46 | + |
| 47 | +# Copy Python site-packages (installed by pip) from build stage |
| 48 | +COPY --from=build /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages |
| 49 | + |
| 50 | +# Set environment variables |
9 | 51 | ENV VOCAB_URL=https://medcat.rosalind.kcl.ac.uk/media/vocab.dat |
10 | 52 | ENV CDB_URL=https://medcat.rosalind.kcl.ac.uk/media/cdb-medmen-v1.dat |
11 | 53 |
|
12 | 54 | ENV CDB_PATH=/webapp/models/cdb.dat |
13 | 55 | ENV VOCAB_PATH=/webapp/models/vocab.dat |
14 | 56 |
|
15 | | -# Create the data directory |
16 | | -RUN mkdir -p /medcat_data |
17 | | - |
18 | 57 | # Set the pythonpath |
19 | 58 | WORKDIR /webapp |
20 | 59 |
|
21 | | -RUN pip install -r requirements.txt |
22 | | - |
23 | | -# Get the spacy model |
24 | | -RUN python -m spacy download en_core_web_md |
| 60 | +# Create the db backup cron job (copied from your setup) |
| 61 | +COPY etc/cron.d/db-backup-cron /etc/cron.d/db-backup-cron |
| 62 | +RUN chmod 0644 /etc/cron.d/db-backup-cron && crontab /etc/cron.d/db-backup-cron |
25 | 63 |
|
26 | | -# Build the db |
| 64 | +# Run migrations and collect static (could be in entrypoint script) |
27 | 65 | RUN python manage.py makemigrations && \ |
28 | 66 | python manage.py makemigrations demo && \ |
29 | 67 | python manage.py migrate && \ |
30 | 68 | python manage.py migrate demo && \ |
31 | 69 | python manage.py collectstatic --noinput |
32 | | - |
33 | | -# Create the db backup cron job |
34 | | -RUN apt-get update && apt-get install -y --no-install-recommends apt-utils cron sqlite3 libsqlite3-dev |
35 | | -COPY etc/cron.d/db-backup-cron /etc/cron.d/db-backup-cron |
36 | | -RUN chmod 0644 /etc/cron.d/db-backup-cron |
37 | | -RUN crontab /etc/cron.d/db-backup-cron |
|
0 commit comments