Severian commited on
Commit
336f29d
·
verified ·
1 Parent(s): 9f20e45

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +37 -32
Dockerfile CHANGED
@@ -1,45 +1,46 @@
1
  # Base Python image with correct version
2
  FROM python:3.12-slim-bookworm AS base
3
 
4
- WORKDIR /app/api
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- # Install Poetry
7
- ENV POETRY_VERSION=1.8.4
8
- ENV POETRY_HOME=/opt/poetry
9
- ENV POETRY_CACHE_DIR=/tmp/poetry_cache
10
- ENV POETRY_NO_INTERACTION=1
11
- ENV POETRY_VIRTUALENVS_IN_PROJECT=true
12
- ENV POETRY_VIRTUALENVS_CREATE=true
13
 
14
- # Install poetry in a separate layer
15
- RUN pip install --no-cache-dir "poetry==${POETRY_VERSION}"
 
 
 
 
 
16
 
17
- # Create and switch to non-root user early
18
- RUN useradd -m -u 1000 user && \
 
 
 
19
  chown -R user:user /app /opt/poetry /tmp/poetry_cache
20
 
 
21
  USER user
22
 
23
- # Copy dependency files
24
- COPY --chown=user:user api/pyproject.toml api/poetry.lock api/poetry.toml ./
25
 
26
- # Install dependencies
27
  RUN poetry install --no-root --no-dev
28
 
29
- # Switch back to root for system installations
30
- USER root
31
-
32
- # Install system dependencies
33
- RUN apt-get update && \
34
- apt-get install -y postgresql postgresql-contrib curl git gcc python3-dev \
35
- libgmp-dev libmpfr-dev libmpc-dev nodejs npm && \
36
- rm -rf /var/lib/apt/lists/* && \
37
- mkdir -p /var/run/postgresql /var/lib/postgresql/data /data/storage && \
38
- chown -R postgres:postgres /var/run/postgresql /var/lib/postgresql/data && \
39
- chmod 2777 /var/run/postgresql && \
40
- chmod 700 /var/lib/postgresql/data
41
-
42
- # Initialize PostgreSQL
43
  USER postgres
44
  RUN /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/data && \
45
  echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf && \
@@ -47,14 +48,17 @@ RUN /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/data && \
47
 
48
  # Switch back to user
49
  USER user
 
 
50
  ENV HOME=/home/user \
51
  PATH=/home/user/.local/bin:$PATH
52
 
53
- # Copy entrypoint script
54
- COPY api/docker/entrypoint.sh /app/entrypoint.sh
 
55
  RUN chmod +x /app/entrypoint.sh
56
 
57
- # Set environment variables
58
  ENV FLASK_APP=app.py \
59
  EDITION=SELF_HOSTED \
60
  DEPLOY_ENV=PRODUCTION \
@@ -66,6 +70,7 @@ ENV FLASK_APP=app.py \
66
  DB_DATABASE=dify \
67
  MIGRATION_ENABLED=true
68
 
 
69
  EXPOSE 7860
70
 
71
  WORKDIR /app
 
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 \
8
+ POETRY_CACHE_DIR=/tmp/poetry_cache \
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 \
23
+ gcc g++ libc-dev libffi-dev libgmp-dev libmpfr-dev libmpc-dev \
24
+ postgresql postgresql-contrib curl git nodejs npm && \
25
+ rm -rf /var/lib/apt/lists/*
26
 
27
+ # Set up directories and permissions
28
+ RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /data/storage && \
29
+ chown -R postgres:postgres /var/run/postgresql /var/lib/postgresql/data && \
30
+ chmod 2777 /var/run/postgresql && \
31
+ chmod 700 /var/lib/postgresql/data && \
32
  chown -R user:user /app /opt/poetry /tmp/poetry_cache
33
 
34
+ # Switch to user for Poetry operations
35
  USER user
36
 
37
+ # Copy dependency files with correct ownership
38
+ COPY --chown=user pyproject.toml poetry.lock poetry.toml ./
39
 
40
+ # Install Python dependencies
41
  RUN poetry install --no-root --no-dev
42
 
43
+ # Initialize PostgreSQL as postgres user
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  USER postgres
45
  RUN /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/data && \
46
  echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf && \
 
48
 
49
  # Switch back to user
50
  USER user
51
+
52
+ # Set up user environment (HF requirement)
53
  ENV HOME=/home/user \
54
  PATH=/home/user/.local/bin:$PATH
55
 
56
+ # Copy application code and entrypoint
57
+ COPY --chown=user . /app/api/
58
+ COPY --chown=user docker/entrypoint.sh /app/entrypoint.sh
59
  RUN chmod +x /app/entrypoint.sh
60
 
61
+ # Set required environment variables
62
  ENV FLASK_APP=app.py \
63
  EDITION=SELF_HOSTED \
64
  DEPLOY_ENV=PRODUCTION \
 
70
  DB_DATABASE=dify \
71
  MIGRATION_ENABLED=true
72
 
73
+ # Expose HF required port
74
  EXPOSE 7860
75
 
76
  WORKDIR /app