-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.sql_to_arc
More file actions
126 lines (97 loc) · 4.9 KB
/
Dockerfile.sql_to_arc
File metadata and controls
126 lines (97 loc) · 4.9 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# ---- Package Build Stage ----
FROM python:3.12.12-alpine3.23 AS package-builder
WORKDIR /build
# Copy project files needed for package build
COPY pyproject.toml uv.lock README.md LICENSE ./
COPY middleware ./middleware
# Upgrade pip and install uv
RUN pip install --no-cache-dir --upgrade pip==26.0.1 uv==0.11.2
# Declare build argument for versioning
ARG APP_VERSION=0.0.0
ENV SETUPTOOLS_SCM_PRETEND_VERSION=${APP_VERSION}
ENV SETUPTOOLS_SCM_PRETEND_VERSION_FOR_SQL_TO_ARC=${APP_VERSION}
# We prefer to build a wheel for our package first. This ensures we have a clean,
# distributable artifact that contains only the necessary files.
RUN uv build --package sql_to_arc --wheel
# ---- Binary Build Stage ----
FROM python:3.12.12-alpine3.23 AS binary-builder
# Install build tools for PyInstaller
RUN apk add --no-cache \
build-base=0.5-r3 \
python3-dev=3.12.12-r0 \
libffi-dev=3.5.2-r0 \
openssl-dev=3.5.5-r0 \
cargo=1.91.1-r1 \
git=2.52.0-r0 \
unixodbc-dev=2.3.14-r0 \
curl=8.17.0-r1
# Pre-download Microsoft ODBC Driver 18 for Alpine (kept for runtime stage)
# Use -L to follow redirects
RUN curl -L -O https://download.microsoft.com/download/9dcab408-e0d4-4571-a81a-5a0951e3445f/msodbcsql18_18.6.1.1-1_amd64.apk
WORKDIR /build
# Install uv core tool
RUN pip install --no-cache-dir --upgrade pip==26.0.1 uv==0.11.2
# Declare build argument for versioning
ARG APP_VERSION=0.0.0
# Pretend version for both workspace members to satisfy hatch-vcs
ENV SETUPTOOLS_SCM_PRETEND_VERSION=${APP_VERSION}
ENV SETUPTOOLS_SCM_PRETEND_VERSION_FOR_SQL_TO_ARC=${APP_VERSION}
# Bring in the pre-built wheel and project metadata
COPY --from=package-builder /build/dist/*.whl /tmp/wheels/
COPY pyproject.toml uv.lock README.md LICENSE ./
# We still need the source code because uv sync requires all workspace members (e.g., middleware/sql_to_arc)
# to be physically present to validate the environment against the lockfile.
COPY middleware ./middleware
# Dependency Resolution Strategy:
# 1. We would prefer to install everything as wheels for speed and reliability.
# 2. However, some dependencies (especially git-based ones like api_client) are not available
# as pre-built wheels on PyPI.
# 3. Thus, we use 'uv sync' to create a virtual environment (.venv) and resolve all
# complex dependencies exactly as specified in the uv.lock.
# 4. We scope sync to the sql_to_arc workspace package so uv does not additionally
# build/install the root project (m4-2-sql-to-arc).
RUN uv sync --no-dev --package sql_to_arc
# 4. Finally, for packages like sql_to_arc that exist both as a workspace dependency
# and as a pre-built wheel, we explicitly 'uv pip install' the wheel. This ensures
# we use our optimized, pre-built package instead of the 'editable' source install.
RUN uv pip install /tmp/wheels/*.whl pyinstaller
# 5. FIX: Manually reinstall git dependencies using their INTERNAL names (underscore)
# to ensure proper namespace installation. We do this AFTER installing the wheel
# to ensure the namespace merging happens correctly.
RUN . .venv/bin/activate && \
uv pip install --force-reinstall \
"api_client @ git+https://github.com/fairagro/m4.2_advanced_middleware_api.git@main#subdirectory=middleware/api_client" \
"shared @ git+https://github.com/fairagro/m4.2_advanced_middleware_api.git@main#subdirectory=middleware/shared"
# Build standalone binary using the .venv's context.
# We point PyInstaller to the 'main.py' INSTALLED in site-packages.
# Since we merged everything into site-packages/middleware, we can largely rely on auto-discovery.
# --copy-metadata is still needed because the code checks its own version at runtime.
RUN . .venv/bin/activate && \
.venv/bin/python -m PyInstaller --onedir \
--name sql_to_arc \
--copy-metadata sql_to_arc \
--copy-metadata api_client \
--copy-metadata shared \
.venv/lib/python3.12/site-packages/middleware/sql_to_arc/main.py
# ---- Runtime Stage ----
FROM alpine:3.23.3
WORKDIR /middleware
# Install runtime dependencies for ODBC (MSSQL) and Oracle
# We copy the pre-downloaded Microsoft ODBC driver from the builder stage
# and install it using --allow-untrusted as the public key is not pre-installed in alpine.
COPY --from=binary-builder /msodbcsql18_18.6.1.1-1_amd64.apk /tmp/
RUN apk add --no-cache --upgrade \
unixodbc=2.3.14-r0 \
libstdc++=15.2.0-r2 \
gcompat=1.1.0-r4 \
zlib=1.3.2-r0 && \
apk add --no-cache --allow-untrusted /tmp/msodbcsql18_18.6.1.1-1_amd64.apk && \
rm /tmp/msodbcsql18_*.apk
# Create non-root user and group
RUN addgroup -S sql_to_arc && \
adduser -S -H -G sql_to_arc sql_to_arc
# Copy the entire directory created by --onedir with correct ownership
COPY --chown=sql_to_arc:sql_to_arc --from=binary-builder /build/dist/sql_to_arc /middleware/sql_to_arc
USER sql_to_arc
# Execute the binary inside the directory
CMD ["/middleware/sql_to_arc/sql_to_arc", "-c", "/etc/sql_to_arc/config.yaml"]