Severian commited on
Commit
8a21f91
·
verified ·
1 Parent(s): 570d40a

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +86 -61
Dockerfile CHANGED
@@ -1,81 +1,106 @@
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 && \
47
- echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.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 source code
57
- COPY --chown=user . /app/api/
 
 
 
58
 
59
- # Copy entrypoint
60
- COPY --chown=user /app/api/docker/entrypoint.sh /entrypoint.sh
61
- RUN chmod +x /entrypoint.sh
62
 
63
- # Set required environment variables
64
  ENV FLASK_APP=app.py \
65
  EDITION=SELF_HOSTED \
66
  DEPLOY_ENV=PRODUCTION \
67
- MODE=api \
68
- DB_USERNAME=postgres \
69
- DB_PASSWORD=difyai123456 \
70
- DB_HOST=localhost \
71
- DB_PORT=5432 \
72
- DB_DATABASE=dify \
73
- MIGRATION_ENABLED=true
 
 
74
 
75
- # Expose HF required port
76
- EXPOSE 7860
77
 
78
- WORKDIR /app
 
 
79
 
80
- ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]
81
 
 
 
1
+ # Base stage with shared configuration
2
+ FROM node:20.11-alpine3.19 AS base
3
+
4
+ # Configure build environment
5
+ ENV NODE_OPTIONS="--max_old_space_size=2048" \
6
+ NEXT_TELEMETRY_DISABLED=1 \
7
+ NODE_ENV=production \
8
+ PYTHONDONTWRITEBYTECODE=1 \
9
  POETRY_NO_INTERACTION=1 \
10
+ POETRY_VIRTUALENVS_CREATE=false \
11
+ POETRY_CACHE_DIR=/cache/poetry
 
12
 
13
+ # Web builder stage
14
+ FROM base AS web-builder
15
 
16
+ WORKDIR /app/web
17
+
18
+ # Copy package files first
19
+ COPY web/package.json web/yarn.lock ./
20
+
21
+ # Install build dependencies globally first
22
+ RUN npm install -g code-inspector-plugin autoprefixer postcss tailwindcss
23
+
24
+ # Install project dependencies
25
+ RUN yarn install --frozen-lockfile --network-timeout 300000 && \
26
+ yarn add --dev @types/node @types/react code-inspector-plugin autoprefixer postcss tailwindcss
27
 
28
+ # Copy source files
29
+ COPY web/ .
30
+
31
+ # Build the application with standalone output
32
+ RUN NODE_PATH=/usr/local/lib/node_modules yarn build
33
+
34
+ # Python builder stage
35
+ FROM python:3.10-slim-bookworm AS python-builder
36
+
37
+ # Install build dependencies in a single layer
38
+ RUN apt-get update && \
39
  apt-get install -y --no-install-recommends \
40
+ build-essential \
41
+ && rm -rf /var/lib/apt/lists/*
42
+
43
+ WORKDIR /app/api
 
 
 
 
 
 
 
 
 
44
 
45
+ # Install and configure poetry
46
+ RUN pip install --no-cache-dir poetry
47
 
48
+ # Copy Python files and install dependencies
49
+ COPY api/pyproject.toml api/poetry.lock ./
50
+ RUN poetry config virtualenvs.create false && \
51
+ poetry install --no-dev --no-interaction --no-ansi
52
 
53
+ # Final stage
54
+ FROM python:3.10-slim-bookworm
 
 
 
55
 
56
+ # Set up a new user named "user" with user ID 1000 (required by Hugging Face)
57
+ RUN useradd -m -u 1000 user
58
+
59
+ # Install runtime dependencies in a single layer
60
+ RUN apt-get update && \
61
+ apt-get install -y --no-install-recommends \
62
+ nodejs \
63
+ npm \
64
+ && rm -rf /var/lib/apt/lists/*
65
 
66
+ # Create app directory structure
67
+ WORKDIR /app
68
+ RUN mkdir -p api web && chown -R user:user /app
69
+
70
+ # Copy Python environment and set permissions
71
+ COPY --from=python-builder --chown=user /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
72
+ COPY --chown=user api/ /app/api/
73
 
74
+ # Copy web build artifacts with correct permissions
75
+ COPY --from=web-builder --chown=user /app/web/.next /app/web/.next
76
+ COPY --from=web-builder --chown=user /app/web/public /app/web/public
77
+ COPY --from=web-builder --chown=user /app/web/node_modules /app/web/node_modules
78
+ COPY --from=web-builder --chown=user /app/web/package.json /app/web/package.json
79
 
80
+ # Install gunicorn
81
+ RUN pip install --no-cache-dir gunicorn gevent
 
82
 
83
+ # Set environment variables
84
  ENV FLASK_APP=app.py \
85
  EDITION=SELF_HOSTED \
86
  DEPLOY_ENV=PRODUCTION \
87
+ CONSOLE_API_URL=http://127.0.0.1:7860 \
88
+ CONSOLE_WEB_URL=http://127.0.0.1:3000 \
89
+ SERVICE_API_URL=http://127.0.0.1:7860 \
90
+ APP_WEB_URL=http://127.0.0.1:3000 \
91
+ NODE_ENV=production \
92
+ HOME=/app
93
+
94
+ # Switch to the non-root user
95
+ USER user
96
 
97
+ # Expose port 7860 as required by Hugging Face Spaces
98
+ EXPOSE 7860 3000
99
 
100
+ # Setup entrypoint
101
+ COPY --chown=user docker/entrypoint.sh /app/entrypoint.sh
102
+ RUN chmod +x /app/entrypoint.sh
103
 
104
+ WORKDIR /app
105
 
106
+ CMD ["./entrypoint.sh"]