@@ -346,9 +346,14 @@ COPY --from=build --chown=nonroot:nonroot /app/prisma /app/prisma
346
346
COPY --from=build --chown=nonroot:nonroot /app/config /app/config
347
347
COPY --from=build --chown=nonroot:nonroot /app/pyproject.toml /app/pyproject.toml
348
348
349
- # Aggressive cleanup and optimization in a single layer
350
- # PERFORMANCE: Single RUN reduces layer count and enables atomic cleanup
351
- # SIZE: Removes unnecessary files to minimize final image size
349
+ # Create convenient symlinks for Python and application binaries
350
+ # USABILITY: Allows running 'python' and 'tux' commands without full paths
351
+ # COMPATIBILITY: Maintains expected command locations for scripts and debugging
352
+ RUN ln -sf /app/.venv/bin/python /usr/local/bin/python && \
353
+ ln -sf /app/.venv/bin/tux /usr/local/bin/tux
354
+
355
+ # Setup directories and permissions before Prisma setup
356
+ # SECURITY: Ensures proper directory structure and permissions
352
357
RUN set -eux; \
353
358
# Fix permissions for virtual environment
354
359
chown -R nonroot:nonroot /app/.venv; \
@@ -358,8 +363,20 @@ RUN set -eux; \
358
363
mkdir -p /home/nonroot/.cache /home/nonroot/.npm; \
359
364
chown -R nonroot:nonroot /app/.cache /app/temp /home/nonroot/.cache /home/nonroot/.npm; \
360
365
# Remove npm cache to reduce scan time and image size
361
- rm -rf /home/nonroot/.npm/_cacache; \
362
- \
366
+ rm -rf /home/nonroot/.npm/_cacache
367
+
368
+ # Switch to non-root user for security and run Prisma setup
369
+ # SECURITY: Application runs with minimal privileges
370
+ # RUNTIME: Ensures Prisma binaries and client are properly configured as nonroot user
371
+ USER nonroot
372
+ RUN /app/.venv/bin/python -m prisma py fetch && \
373
+ /app/.venv/bin/python -m prisma generate
374
+
375
+ # Aggressive cleanup and optimization after Prisma setup
376
+ # PERFORMANCE: Single RUN reduces layer count and enables atomic cleanup
377
+ # SIZE: Removes unnecessary files to minimize final image size but preserves Prisma binaries
378
+ USER root
379
+ RUN set -eux; \
363
380
# VIRTUAL ENVIRONMENT CLEANUP
364
381
# The following operations remove unnecessary files from the Python environment
365
382
# This can reduce the size by 30-50MB without affecting functionality
@@ -368,21 +385,22 @@ RUN set -eux; \
368
385
find /app/.venv -name "*.pyc" -delete; \
369
386
find /app/.venv -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true; \
370
387
\
371
- # Remove test directories from installed packages
388
+ # Remove test directories from installed packages (but preserve prisma binaries)
372
389
# These directories contain test files that are not needed in production
373
390
for test_dir in tests testing "*test*" ; do \
374
- find /app/.venv -name "$test_dir" -type d -exec rm -rf {} + 2>/dev/null || true; \
391
+ find /app/.venv -name "$test_dir" -type d -not -path "*/prisma*" - exec rm -rf {} + 2>/dev/null || true; \
375
392
done; \
376
393
\
377
- # Remove documentation files from installed packages
394
+ # Remove documentation files from installed packages (but preserve prisma docs)
378
395
# These files take up significant space and are not needed in production
379
396
for doc_pattern in "*.md" "*.txt" "*.rst" "LICENSE*" "NOTICE*" "COPYING*" "CHANGELOG*" "README*" "HISTORY*" "AUTHORS*" "CONTRIBUTORS*" ; do \
380
- find /app/.venv -name "$doc_pattern" -delete 2>/dev/null || true; \
397
+ find /app/.venv -name "$doc_pattern" -not -path "*/prisma*" - delete 2>/dev/null || true; \
381
398
done; \
382
399
\
383
400
# Remove large development packages that are not needed in production
384
401
# These packages (pip, setuptools, wheel) are only needed for installing packages
385
- for pkg in pip setuptools wheel pkg_resources; do \
402
+ # NOTE: Preserving packages that Prisma might need
403
+ for pkg in setuptools wheel pkg_resources; do \
386
404
rm -rf /app/.venv/lib/python3.13/site-packages/${pkg}* 2>/dev/null || true; \
387
405
rm -rf /app/.venv/bin/${pkg}* 2>/dev/null || true; \
388
406
done; \
@@ -391,20 +409,13 @@ RUN set -eux; \
391
409
# Compile Python bytecode for performance optimization
392
410
# PERFORMANCE: Pre-compiled bytecode improves startup time
393
411
# Note: Some compilation errors are expected and ignored
394
- /app/.venv/bin/python -m compileall -b -q /app/tux /app/.venv/lib/python3.13/site-packages/ 2>/dev/null || true
395
-
396
- # Create convenient symlinks for Python and application binaries
397
- # USABILITY: Allows running 'python' and 'tux' commands without full paths
398
- # COMPATIBILITY: Maintains expected command locations for scripts and debugging
399
- RUN ln -sf /app/.venv/bin/python /usr/local/bin/python && \
400
- ln -sf /app/.venv/bin/tux /usr/local/bin/tux
412
+ /app/.venv/bin/python -m compileall -b -q /app/tux /app/.venv/lib/python3.13/site-packages/ 2>/dev/null || true; \
413
+ \
414
+ # Switch back to nonroot user for final ownership
415
+ chown -R nonroot:nonroot /app /home/nonroot
401
416
402
- # Switch to non-root user for security and run Prisma setup
403
- # SECURITY: Application runs with minimal privileges
404
- # RUNTIME: Ensures Prisma binaries and client are properly configured as nonroot user
417
+ # Switch back to non-root user for runtime
405
418
USER nonroot
406
- RUN /app/.venv/bin/python -m prisma py fetch && \
407
- /app/.venv/bin/python -m prisma generate
408
419
409
420
# Health check configuration for container orchestration
410
421
# MONITORING: Allows Docker/Kubernetes to monitor application health
0 commit comments