ai-engine / Dockerfile
Severian's picture
Update Dockerfile
73efd4a verified
raw
history blame
2.68 kB
# Base Python image with correct version
FROM python:3.12-slim-bookworm AS base
# Set up environment variables according to HF guidelines
ENV PYTHONDONTWRITEBYTECODE=1 \
POETRY_VERSION=1.8.4 \
POETRY_HOME=/opt/poetry \
POETRY_CACHE_DIR=/tmp/poetry_cache \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_VIRTUALENVS_CREATE=true \
POETRY_REQUESTS_TIMEOUT=15
# Create non-root user early (HF requirement)
RUN useradd -m -u 1000 user
WORKDIR /app/api
# Install Poetry and dependencies in a single layer
RUN pip install --no-cache-dir "poetry==${POETRY_VERSION}" && \
apt-get update && \
apt-get install -y --no-install-recommends \
gcc g++ libc-dev libffi-dev libgmp-dev libmpfr-dev libmpc-dev \
postgresql postgresql-contrib curl git nodejs npm && \
rm -rf /var/lib/apt/lists/*
# Create a requirements.txt from poetry dependencies
RUN echo "flask==3.0.1\n\
gunicorn==22.0.0\n\
gevent==24.11.1\n\
celery==5.4.0\n\
redis==5.0.3\n\
psycopg2-binary==2.9.6\n\
sqlalchemy==2.0.29\n\
flask-migrate==4.0.5\n\
flask-sqlalchemy==3.1.1" > requirements.txt
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Set up directories and permissions
RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /data/storage && \
chown -R postgres:postgres /var/run/postgresql /var/lib/postgresql/data && \
chmod 2777 /var/run/postgresql && \
chmod 700 /var/lib/postgresql/data && \
chown -R user:user /app /opt/poetry /tmp/poetry_cache
# Switch to user
USER user
# Set up user environment (HF requirement)
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
# Create and copy entrypoint script
RUN echo '#!/bin/bash\n\
set -e\n\
\n\
if [[ "${MIGRATION_ENABLED}" == "true" ]]; then\n\
echo "Running migrations"\n\
flask upgrade-db\n\
fi\n\
\n\
if [[ "${DEBUG}" == "true" ]]; then\n\
exec flask run --host=${DIFY_BIND_ADDRESS:-0.0.0.0} --port=7860 --debug\n\
else\n\
exec gunicorn \\\n\
--bind "0.0.0.0:7860" \\\n\
--workers ${SERVER_WORKER_AMOUNT:-1} \\\n\
--worker-class ${SERVER_WORKER_CLASS:-gevent} \\\n\
--timeout ${GUNICORN_TIMEOUT:-200} \\\n\
--preload \\\n\
app:app\n\
fi' > /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Set required environment variables
ENV FLASK_APP=app.py \
EDITION=SELF_HOSTED \
DEPLOY_ENV=PRODUCTION \
MODE=api \
DB_USERNAME=postgres \
DB_PASSWORD=difyai123456 \
DB_HOST=localhost \
DB_PORT=5432 \
DB_DATABASE=dify \
MIGRATION_ENABLED=true
# Expose HF required port
EXPOSE 7860
WORKDIR /app
ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]