-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathDockerfile
More file actions
90 lines (70 loc) · 2.85 KB
/
Copy pathDockerfile
File metadata and controls
90 lines (70 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
ARG UV_MAJOR_VERSION=0.10
ARG PYTHON_VERSION=3.12
ARG DEBIAN_VERSION=trixie
# Build the virtual environment using UV
FROM ghcr.io/astral-sh/uv:${UV_MAJOR_VERSION}-python${PYTHON_VERSION}-${DEBIAN_VERSION}-slim AS builder
# Redeclare ARG after FROM to make it available in this stage
ARG UV_COMPILE_BYTECODE=1
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
ENV DEBIAN_FRONTEND=noninteractive
ENV UV_COMPILE_BYTECODE=${UV_COMPILE_BYTECODE}
ENV UV_LINK_MODE=copy
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq-dev python3-dev gcc git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Sync dependencies without installing the project itself (creates .venv)
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-install-project
# Ensure subsequent commands use the virtual environment
ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"
# Copy application code (including .git for version detection)
COPY pyproject.toml uv.lock README.md ./
COPY flexmeasures ./flexmeasures
COPY .git ./.git
COPY .flaskenv wsgi.py ./
# Install FlexMeasures itself in the virtual environment
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen
# Install gunicorn separately since it's not a dependency of the project
RUN --mount=type=cache,target=/root/.cache/uv \
uv pip install gunicorn==25.0.3
# Use a separate runtime image to run the code
FROM python:${PYTHON_VERSION}-slim-${DEBIAN_VERSION} AS runtime
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
ENV DEBIAN_FRONTEND=noninteractive
# Install runtime dependencies only
# libgomp1 is required by lightgmb to open a shared object file for parallel computation
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 coinor-cbc libgomp1 curl && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"
# Copy virtual environment from builder
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
# Copy application code
COPY --from=builder /app/flexmeasures ./flexmeasures
COPY --from=builder /app/.flaskenv /app/wsgi.py ./
# Set environment variables to optimize Python
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
EXPOSE 5000
# Gunicorn configuration:
# - worker-tmp-dir is set to /dev/shm instead of /tmp (default) to avoid stalls from Docker overlay filesystem
# http://docs.gunicorn.org/en/latest/faq.html#how-do-i-avoid-gunicorn-excessively-blocking-in-os-fchmod
# - Using 2 workers to avoid health check timeouts when another request is taking a long time
CMD ["gunicorn", \
"--bind", "0.0.0.0:5000", \
"--worker-tmp-dir", "/dev/shm", \
"--workers", "2", \
"--threads", "4", \
"wsgi:application"]