mgbam commited on
Commit
e8816ce
·
verified ·
1 Parent(s): cd06c05

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +41 -33
Dockerfile CHANGED
@@ -10,10 +10,7 @@ ENV DEBIAN_FRONTEND=noninteractive
10
  ENV LANG C.UTF-8
11
  ENV LC_ALL C.UTF-8
12
 
13
- # Set the working directory in the container
14
- WORKDIR /app
15
-
16
- # Install system dependencies
17
  RUN apt-get update && apt-get install -y --no-install-recommends \
18
  ffmpeg \
19
  imagemagick \
@@ -24,7 +21,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
24
  libglib2.0-0 \
25
  && rm -rf /var/lib/apt/lists/*
26
 
27
- # Modify ImageMagick policy.xml
28
  RUN if [ -f /etc/ImageMagick-6/policy.xml ]; then \
29
  XML_FILE="/etc/ImageMagick-6/policy.xml"; \
30
  echo "INFO: Modifying ImageMagick policy at $XML_FILE (v6) for MoviePy compatibility." ; \
@@ -33,7 +30,7 @@ RUN if [ -f /etc/ImageMagick-6/policy.xml ]; then \
33
  echo "INFO: Modifying ImageMagick policy at $XML_FILE (v7) for MoviePy compatibility." ; \
34
  else \
35
  XML_FILE=""; \
36
- echo "WARNING: ImageMagick policy.xml not found in /etc/ImageMagick-[67]/. MoviePy TextClip might fail." ; \
37
  fi && \
38
  if [ -n "$XML_FILE" ] && [ -f "$XML_FILE" ]; then \
39
  sed -i 's/<policy domain="path" rights="none" pattern="@\*"\/>/<!-- <policy domain="path" rights="none" pattern="@\*" \/> -->/' "$XML_FILE" && \
@@ -46,44 +43,55 @@ RUN if [ -f /etc/ImageMagick-6/policy.xml ]; then \
46
  echo "INFO: ImageMagick policy modifications applied to $XML_FILE." ; \
47
  fi
48
 
49
- # Create a non-root user and group
50
- RUN groupadd -r appgroup && useradd --no-log-init -r -g appgroup -u 1000 appuser
51
- RUN mkdir -p /home/appuser/.cache/pip && chown -R appuser:appgroup /home/appuser
 
 
 
52
 
53
- # Set Streamlit home directory to be writable by appuser
54
- ENV STREAMLIT_HOME=/home/appuser/.streamlit
55
- RUN mkdir -p $STREAMLIT_HOME && chown -R appuser:appgroup $STREAMLIT_HOME
56
 
57
- # Copy the requirements file first
58
- COPY --chown=appuser:appgroup requirements.txt .
59
 
60
- # Install Python dependencies as the non-root user
 
 
61
  USER appuser
62
  RUN pip install --no-cache-dir --upgrade pip && \
63
- pip install --user --no-cache-dir -r requirements.txt # Added --user flag
64
 
65
- # Add user's local bin to PATH. This should be done after pip install as appuser
66
  ENV PATH="/home/appuser/.local/bin:${PATH}"
67
 
68
- # Switch back to root temporarily for copying application files and setting permissions
69
- USER root
70
- COPY . . # This copies to /app
71
- RUN chown -R appuser:appgroup /app # Ensure /app is owned by appuser
72
 
73
- # Create runtime directories as root, then chown to appuser
74
- RUN mkdir -p /app/temp_cinegen_media && chown -R appuser:appgroup /app/temp_cinegen_media
75
- RUN mkdir -p /app/assets/fonts && chown -R appuser:appgroup /app/assets/fonts
76
- RUN if [ -d "/app/assets/fonts" ] && [ "$(ls -A /app/assets/fonts)" ]; then \
77
- mkdir -p /usr/local/share/fonts/truetype/cinegen_custom && \
78
- cp /app/assets/fonts/*.*tf /usr/local/share/fonts/truetype/cinegen_custom/ 2>/dev/null || true && \
79
- fc-cache -fv && \
80
- echo "INFO: Copied custom fonts and refreshed font cache."; \
81
- else \
82
- echo "INFO: No custom fonts found in /app/assets/fonts to copy system-wide." ; \
83
- fi
84
 
85
- # Switch to the non-root user for running the application
86
  USER appuser
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  # Expose the port Streamlit runs on
89
  EXPOSE 8501
 
10
  ENV LANG C.UTF-8
11
  ENV LC_ALL C.UTF-8
12
 
13
+ # Install system dependencies (as root)
 
 
 
14
  RUN apt-get update && apt-get install -y --no-install-recommends \
15
  ffmpeg \
16
  imagemagick \
 
21
  libglib2.0-0 \
22
  && rm -rf /var/lib/apt/lists/*
23
 
24
+ # Modify ImageMagick policy.xml (as root)
25
  RUN if [ -f /etc/ImageMagick-6/policy.xml ]; then \
26
  XML_FILE="/etc/ImageMagick-6/policy.xml"; \
27
  echo "INFO: Modifying ImageMagick policy at $XML_FILE (v6) for MoviePy compatibility." ; \
 
30
  echo "INFO: Modifying ImageMagick policy at $XML_FILE (v7) for MoviePy compatibility." ; \
31
  else \
32
  XML_FILE=""; \
33
+ echo "WARNING: ImageMagick policy.xml not found. MoviePy TextClip might fail." ; \
34
  fi && \
35
  if [ -n "$XML_FILE" ] && [ -f "$XML_FILE" ]; then \
36
  sed -i 's/<policy domain="path" rights="none" pattern="@\*"\/>/<!-- <policy domain="path" rights="none" pattern="@\*" \/> -->/' "$XML_FILE" && \
 
43
  echo "INFO: ImageMagick policy modifications applied to $XML_FILE." ; \
44
  fi
45
 
46
+ # Create a non-root user and group, create home, .cache, and .streamlit dirs
47
+ RUN groupadd -r appgroup --gid 1000 && \
48
+ useradd --no-log-init -r -g appgroup -u 1000 --create-home --shell /bin/bash appuser && \
49
+ mkdir -p /home/appuser/.cache/pip && \
50
+ mkdir -p /home/appuser/.streamlit && \
51
+ chown -R appuser:appgroup /home/appuser
52
 
53
+ # Set Streamlit home directory (already created and chowned)
54
+ ENV STREAMLIT_HOME=/home/appuser/.streamlit
 
55
 
56
+ # Set the working directory in the container (owned by root initially, will be chowned)
57
+ WORKDIR /app
58
 
59
+ # Copy requirements.txt and install dependencies AS APPUSER
60
+ # First, copy just requirements.txt and chown its destination so appuser can write to /app (temporarily for this step)
61
+ COPY --chown=appuser:appgroup requirements.txt .
62
  USER appuser
63
  RUN pip install --no-cache-dir --upgrade pip && \
64
+ pip install --user --no-cache-dir -r requirements.txt # --user installs to ~/.local
65
 
66
+ # Add user's local bin to PATH
67
  ENV PATH="/home/appuser/.local/bin:${PATH}"
68
 
69
+ # Copy the rest of the application code AS APPUSER
70
+ # WORKDIR /app is still in effect, appuser should have rights to write here if /app was chowned.
71
+ # However, to be absolutely safe, we copy to a location appuser definitely owns, or chown /app after copy by root.
72
+ # Let's stick to copying as root then chowning all of /app.
73
 
74
+ USER root
75
+ COPY . .
76
+ RUN chown -R appuser:appgroup /app
 
 
 
 
 
 
 
 
77
 
78
+ # Create runtime directories AS APPUSER (now that /app is owned by appuser)
79
  USER appuser
80
+ RUN mkdir -p /app/temp_cinegen_media
81
+ RUN mkdir -p /app/assets/fonts # This directory should exist from COPY, but ensure it.
82
+
83
+ # Copy custom fonts to system location (as root, if needed by MoviePy's ImageMagick backend directly)
84
+ # This step is optional if Pillow direct font path loading is sufficient.
85
+ # USER root
86
+ # RUN if [ -d "/app/assets/fonts" ] && [ "$(ls -A /app/assets/fonts)" ]; then \
87
+ # mkdir -p /usr/local/share/fonts/truetype/cinegen_custom && \
88
+ # cp /app/assets/fonts/*.*tf /usr/local/share/fonts/truetype/cinegen_custom/ 2>/dev/null || true && \
89
+ # fc-cache -fv && \
90
+ # echo "INFO: Copied custom fonts and refreshed font cache (as root)."; \
91
+ # else \
92
+ # echo "INFO: No custom fonts found in /app/assets/fonts to copy system-wide." ; \
93
+ # fi
94
+ # USER appuser # Switch back to appuser for runtime
95
 
96
  # Expose the port Streamlit runs on
97
  EXPOSE 8501