Severian commited on
Commit
4e4e60f
·
verified ·
1 Parent(s): 7a1cc79

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +27 -25
Dockerfile CHANGED
@@ -1,7 +1,10 @@
1
  # Base Python image with correct version
2
- FROM python:3.12-slim-bookworm AS base
3
 
4
- # Set up environment variables according to HF guidelines
 
 
 
5
  ENV PYTHONDONTWRITEBYTECODE=1 \
6
  POETRY_VERSION=1.8.4 \
7
  POETRY_HOME=/opt/poetry \
@@ -9,14 +12,12 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
9
  POETRY_NO_INTERACTION=1 \
10
  POETRY_VIRTUALENVS_IN_PROJECT=true \
11
  POETRY_VIRTUALENVS_CREATE=true \
12
- POETRY_REQUESTS_TIMEOUT=15
13
-
14
- # Create non-root user early (HF requirement)
15
- RUN useradd -m -u 1000 user
16
 
17
- WORKDIR /app/api
18
 
19
- # Install Poetry and dependencies in a single layer
20
  RUN pip install --no-cache-dir "poetry==${POETRY_VERSION}" && \
21
  apt-get update && \
22
  apt-get install -y --no-install-recommends \
@@ -25,27 +26,28 @@ RUN pip install --no-cache-dir "poetry==${POETRY_VERSION}" && \
25
  curl git nodejs npm && \
26
  rm -rf /var/lib/apt/lists/*
27
 
28
- # Create a requirements.txt from poetry dependencies
29
- RUN echo "flask==3.0.1\n\
30
- gunicorn==22.0.0\n\
31
- gevent==24.11.1\n\
32
- celery==5.4.0\n\
33
- redis==5.0.3\n\
34
- psycopg2-binary==2.9.6\n\
35
- sqlalchemy==2.0.29\n\
36
- flask-migrate==4.0.5\n\
37
- flask-sqlalchemy==3.1.1" > requirements.txt
38
 
39
  # Install Python dependencies
40
- RUN pip install --no-cache-dir -r requirements.txt
 
 
 
 
 
 
 
 
41
 
42
- # Create and set up entrypoint script (before user switch)
43
  RUN echo '#!/bin/bash\n\
44
  set -e\n\
45
  \n\
46
  if [[ "${MIGRATION_ENABLED}" == "true" ]]; then\n\
47
  echo "Running migrations"\n\
48
- flask upgrade-db\n\
49
  fi\n\
50
  \n\
51
  if [[ "${DEBUG}" == "true" ]]; then\n\
@@ -63,11 +65,11 @@ fi' > /entrypoint.sh && \
63
  chown user:user /entrypoint.sh
64
 
65
  # Set up directories and permissions
66
- RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /data/storage /opt/poetry /tmp/poetry_cache && \
67
  chown -R postgres:postgres /var/run/postgresql /var/lib/postgresql/data && \
68
  chmod 2777 /var/run/postgresql && \
69
  chmod 700 /var/lib/postgresql/data && \
70
- chown -R user:user /app /opt/poetry /tmp/poetry_cache
71
 
72
  # Switch to user
73
  USER user
@@ -77,7 +79,7 @@ ENV HOME=/home/user \
77
  PATH=/home/user/.local/bin:$PATH
78
 
79
  # Set required environment variables
80
- ENV FLASK_APP=app.py \
81
  EDITION=SELF_HOSTED \
82
  DEPLOY_ENV=PRODUCTION \
83
  MODE=api \
@@ -91,7 +93,7 @@ ENV FLASK_APP=app.py \
91
  # Expose HF required port
92
  EXPOSE 7860
93
 
94
- WORKDIR /app
95
 
96
  ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]
97
 
 
1
  # Base Python image with correct version
2
+ FROM python:3.12-slim-bookworm
3
 
4
+ # Create non-root user early (HF requirement)
5
+ RUN useradd -m -u 1000 user
6
+
7
+ # Set up environment variables
8
  ENV PYTHONDONTWRITEBYTECODE=1 \
9
  POETRY_VERSION=1.8.4 \
10
  POETRY_HOME=/opt/poetry \
 
12
  POETRY_NO_INTERACTION=1 \
13
  POETRY_VIRTUALENVS_IN_PROJECT=true \
14
  POETRY_VIRTUALENVS_CREATE=true \
15
+ POETRY_REQUESTS_TIMEOUT=15 \
16
+ PYTHONPATH=/app
 
 
17
 
18
+ WORKDIR /app
19
 
20
+ # Install system dependencies
21
  RUN pip install --no-cache-dir "poetry==${POETRY_VERSION}" && \
22
  apt-get update && \
23
  apt-get install -y --no-install-recommends \
 
26
  curl git nodejs npm && \
27
  rm -rf /var/lib/apt/lists/*
28
 
29
+ # Copy application code
30
+ COPY --chown=user api /app/api
31
+ WORKDIR /app/api
 
 
 
 
 
 
 
32
 
33
  # Install Python dependencies
34
+ RUN pip install --no-cache-dir flask==3.0.1 \
35
+ gunicorn==22.0.0 \
36
+ gevent==24.11.1 \
37
+ celery==5.4.0 \
38
+ redis==5.0.3 \
39
+ psycopg2-binary==2.9.6 \
40
+ sqlalchemy==2.0.29 \
41
+ flask-migrate==4.0.5 \
42
+ flask-sqlalchemy==3.1.1
43
 
44
+ # Create and set up entrypoint script
45
  RUN echo '#!/bin/bash\n\
46
  set -e\n\
47
  \n\
48
  if [[ "${MIGRATION_ENABLED}" == "true" ]]; then\n\
49
  echo "Running migrations"\n\
50
+ cd /app/api && flask db upgrade\n\
51
  fi\n\
52
  \n\
53
  if [[ "${DEBUG}" == "true" ]]; then\n\
 
65
  chown user:user /entrypoint.sh
66
 
67
  # Set up directories and permissions
68
+ RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /data/storage && \
69
  chown -R postgres:postgres /var/run/postgresql /var/lib/postgresql/data && \
70
  chmod 2777 /var/run/postgresql && \
71
  chmod 700 /var/lib/postgresql/data && \
72
+ chown -R user:user /app
73
 
74
  # Switch to user
75
  USER user
 
79
  PATH=/home/user/.local/bin:$PATH
80
 
81
  # Set required environment variables
82
+ ENV FLASK_APP=/app/api/app.py \
83
  EDITION=SELF_HOSTED \
84
  DEPLOY_ENV=PRODUCTION \
85
  MODE=api \
 
93
  # Expose HF required port
94
  EXPOSE 7860
95
 
96
+ WORKDIR /app/api
97
 
98
  ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]
99