Skip to content

Commit d04cc7c

Browse files
committed
chore(Dockerfile): reorganize and optimize Dockerfile for better security and performance
Reorganizes the Dockerfile to improve security by ensuring the application runs with minimal privileges. Adds symlinks for Python and application binaries to enhance usability and maintain compatibility. Introduces a separate step for setting up Prisma with non-root user privileges, ensuring proper configuration. Preserves necessary Prisma files during cleanup to maintain functionality while reducing image size. This restructuring enhances the security posture and performance of the Docker image by minimizing unnecessary files and ensuring proper permissions.
1 parent c7460aa commit d04cc7c

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

Dockerfile

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,14 @@ COPY --from=build --chown=nonroot:nonroot /app/prisma /app/prisma
346346
COPY --from=build --chown=nonroot:nonroot /app/config /app/config
347347
COPY --from=build --chown=nonroot:nonroot /app/pyproject.toml /app/pyproject.toml
348348

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
352357
RUN set -eux; \
353358
# Fix permissions for virtual environment
354359
chown -R nonroot:nonroot /app/.venv; \
@@ -358,8 +363,20 @@ RUN set -eux; \
358363
mkdir -p /home/nonroot/.cache /home/nonroot/.npm; \
359364
chown -R nonroot:nonroot /app/.cache /app/temp /home/nonroot/.cache /home/nonroot/.npm; \
360365
# 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; \
363380
# VIRTUAL ENVIRONMENT CLEANUP
364381
# The following operations remove unnecessary files from the Python environment
365382
# This can reduce the size by 30-50MB without affecting functionality
@@ -368,21 +385,22 @@ RUN set -eux; \
368385
find /app/.venv -name "*.pyc" -delete; \
369386
find /app/.venv -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true; \
370387
\
371-
# Remove test directories from installed packages
388+
# Remove test directories from installed packages (but preserve prisma binaries)
372389
# These directories contain test files that are not needed in production
373390
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; \
375392
done; \
376393
\
377-
# Remove documentation files from installed packages
394+
# Remove documentation files from installed packages (but preserve prisma docs)
378395
# These files take up significant space and are not needed in production
379396
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; \
381398
done; \
382399
\
383400
# Remove large development packages that are not needed in production
384401
# 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 \
386404
rm -rf /app/.venv/lib/python3.13/site-packages/${pkg}* 2>/dev/null || true; \
387405
rm -rf /app/.venv/bin/${pkg}* 2>/dev/null || true; \
388406
done; \
@@ -391,20 +409,13 @@ RUN set -eux; \
391409
# Compile Python bytecode for performance optimization
392410
# PERFORMANCE: Pre-compiled bytecode improves startup time
393411
# 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
401416

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
405418
USER nonroot
406-
RUN /app/.venv/bin/python -m prisma py fetch && \
407-
/app/.venv/bin/python -m prisma generate
408419

409420
# Health check configuration for container orchestration
410421
# MONITORING: Allows Docker/Kubernetes to monitor application health

0 commit comments

Comments
 (0)