seanpedrickcase commited on
Commit
25bc108
·
1 Parent(s): b3d51df

Revised Dockerfile structure for lambda vs gradio builds

Browse files
Files changed (2) hide show
  1. Dockerfile +53 -45
  2. entrypoint.sh +3 -3
Dockerfile CHANGED
@@ -28,8 +28,10 @@ RUN if [ "$INSTALL_PADDLEOCR" = "True" ]; then \
28
  pip install --verbose --no-cache-dir --target=/install paddleocr==3.3.0 paddlepaddle==3.2.0; \
29
  fi
30
 
31
- # Stage 2: Final runtime image
32
- FROM public.ecr.aws/docker/library/python:3.12.11-slim-trixie
 
 
33
 
34
  # Set build-time and runtime environment variable for whether to run in Gradio mode or Lambda mode
35
  ARG APP_MODE=gradio
@@ -39,20 +41,12 @@ ENV APP_MODE=${APP_MODE}
39
  ARG RUN_FASTAPI=0
40
  ENV RUN_FASTAPI=${RUN_FASTAPI}
41
 
42
- # Install runtime dependencies
43
- RUN apt-get update \
44
- && apt-get upgrade -y \
45
- && apt-get install -y --no-install-recommends \
46
- tesseract-ocr \
47
- poppler-utils \
48
- libgl1 \
49
- libglib2.0-0 \
50
- && apt-get clean \
51
- && rm -rf /var/lib/apt/lists/*
52
 
53
- # Create non-root user
54
  ENV APP_HOME=/home/user
55
- RUN useradd -m -u 1000 user
56
 
57
  # Set env variables for Gradio & other apps
58
  ENV GRADIO_TEMP_DIR=/tmp/gradio_tmp/ \
@@ -67,7 +61,47 @@ ENV GRADIO_TEMP_DIR=/tmp/gradio_tmp/ \
67
  XDG_CACHE_HOME=/tmp/xdg_cache/user_1000 \
68
  TESSERACT_DATA_FOLDER=/usr/share/tessdata \
69
  GRADIO_SERVER_NAME=0.0.0.0 \
70
- GRADIO_SERVER_PORT=7860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  # Create the base application directory and set its ownership
73
  RUN mkdir -p ${APP_HOME}/app && chown user:user ${APP_HOME}/app
@@ -111,29 +145,12 @@ RUN mkdir -p /tmp/gradio_tmp /tmp/tld /tmp/matplotlib_cache /tmp /var/tmp ${XDG_
111
  && chown user:user /usr/share/tessdata \
112
  && chmod 755 /usr/share/tessdata
113
 
114
- # Copy installed packages from builder stage
115
- COPY --from=builder /install /usr/local/lib/python3.12/site-packages/
116
-
117
- # Copy installed CLI binaries (e.g. uvicorn)
118
- COPY --from=builder /install/bin /usr/local/bin/
119
-
120
- # Copy app code
121
- COPY . $APP_HOME/app
122
-
123
  # Fix ownership if needed
124
- RUN if [ "$APP_MODE" = "gradio" ]; then chown -R user:user /home/user/app; fi
125
-
126
- # Copy entrypoint and fix line endings + permissions
127
- COPY entrypoint.sh ${APP_HOME}/app/entrypoint.sh
128
- RUN chmod 755 ${APP_HOME}/app/entrypoint.sh \
129
- && sed -i 's/\r$//' ${APP_HOME}/app/entrypoint.sh \
130
- && if [ "$APP_MODE" = "gradio" ]; then chown user:user ${APP_HOME}/app/entrypoint.sh; fi
131
 
132
  # Set permissions for Python executable
133
  RUN chmod 755 /usr/local/bin/python
134
 
135
- WORKDIR $APP_HOME/app
136
-
137
  # Declare volumes (NOTE: runtime mounts will override permissions — handle with care)
138
  VOLUME ["/tmp/matplotlib_cache"]
139
  VOLUME ["/tmp/gradio_tmp"]
@@ -150,18 +167,9 @@ VOLUME ["/usr/share/tessdata"]
150
  VOLUME ["/tmp"]
151
  VOLUME ["/var/tmp"]
152
 
153
- EXPOSE $GRADIO_SERVER_PORT
154
 
155
- # Set runtime environment
156
- ENV PATH=$APP_HOME/.local/bin:$PATH \
157
- PYTHONPATH=$APP_HOME/app \
158
- PYTHONUNBUFFERED=1 \
159
- PYTHONDONTWRITEBYTECODE=1 \
160
- GRADIO_ALLOW_FLAGGING=never \
161
- GRADIO_NUM_PORTS=1 \
162
- GRADIO_ANALYTICS_ENABLED=False \
163
- DEFAULT_CONCURRENCY_LIMIT=3
164
 
165
  ENTRYPOINT ["/home/user/app/entrypoint.sh"]
166
-
167
- CMD ["lambda_entrypoint.lambda_handler"]
 
28
  pip install --verbose --no-cache-dir --target=/install paddleocr==3.3.0 paddlepaddle==3.2.0; \
29
  fi
30
 
31
+ # ===================================================================
32
+ # Stage 2: A common 'base' for both Lambda and Gradio
33
+ # ===================================================================
34
+ FROM public.ecr.aws/docker/library/python:3.12.11-slim-trixie AS base
35
 
36
  # Set build-time and runtime environment variable for whether to run in Gradio mode or Lambda mode
37
  ARG APP_MODE=gradio
 
41
  ARG RUN_FASTAPI=0
42
  ENV RUN_FASTAPI=${RUN_FASTAPI}
43
 
44
+ # Install runtime system dependencies
45
+ RUN apt-get update && apt-get install -y --no-install-recommends \
46
+ tesseract-ocr poppler-utils libgl1 libglib2.0-0 \
47
+ && apt-get clean && rm -rf /var/lib/apt/lists/*
 
 
 
 
 
 
48
 
 
49
  ENV APP_HOME=/home/user
 
50
 
51
  # Set env variables for Gradio & other apps
52
  ENV GRADIO_TEMP_DIR=/tmp/gradio_tmp/ \
 
61
  XDG_CACHE_HOME=/tmp/xdg_cache/user_1000 \
62
  TESSERACT_DATA_FOLDER=/usr/share/tessdata \
63
  GRADIO_SERVER_NAME=0.0.0.0 \
64
+ GRADIO_SERVER_PORT=7860 \
65
+ PATH=$APP_HOME/.local/bin:$PATH \
66
+ PYTHONPATH=$APP_HOME/app \
67
+ PYTHONUNBUFFERED=1 \
68
+ PYTHONDONTWRITEBYTECODE=1 \
69
+ GRADIO_ALLOW_FLAGGING=never \
70
+ GRADIO_NUM_PORTS=1 \
71
+ GRADIO_ANALYTICS_ENABLED=False \
72
+ DEFAULT_CONCURRENCY_LIMIT=3
73
+
74
+ # Copy Python packages from the builder stage
75
+ COPY --from=builder /install /usr/local/lib/python3.12/site-packages/
76
+ COPY --from=builder /install/bin /usr/local/bin/
77
+
78
+ # Copy your application code and entrypoint
79
+ COPY . ${APP_HOME}/app
80
+ COPY entrypoint.sh ${APP_HOME}/app/entrypoint.sh
81
+ # Fix line endings and set execute permissions
82
+ RUN sed -i 's/\r$//' ${APP_HOME}/app/entrypoint.sh \
83
+ && chmod +x ${APP_HOME}/app/entrypoint.sh
84
+
85
+ WORKDIR ${APP_HOME}/app
86
+
87
+ # ===================================================================
88
+ # FINAL Stage 3: The Lambda Image (runs as root for simplicity)
89
+ # ===================================================================
90
+ FROM base AS lambda
91
+ # Set runtime ENV for Lambda mode
92
+ ENV APP_MODE=lambda
93
+ ENTRYPOINT ["/home/user/app/entrypoint.sh"]
94
+ CMD ["lambda_entrypoint.lambda_handler"]
95
+
96
+ # ===================================================================
97
+ # FINAL Stage 4: The Gradio Image (runs as a secure, non-root user)
98
+ # ===================================================================
99
+ FROM base AS gradio
100
+ # Set runtime ENV for Gradio mode
101
+ ENV APP_MODE=gradio
102
+
103
+ # Create non-root user
104
+ RUN useradd -m -u 1000 user
105
 
106
  # Create the base application directory and set its ownership
107
  RUN mkdir -p ${APP_HOME}/app && chown user:user ${APP_HOME}/app
 
145
  && chown user:user /usr/share/tessdata \
146
  && chmod 755 /usr/share/tessdata
147
 
 
 
 
 
 
 
 
 
 
148
  # Fix ownership if needed
149
+ RUN chown -R user:user /home/user/app
 
 
 
 
 
 
150
 
151
  # Set permissions for Python executable
152
  RUN chmod 755 /usr/local/bin/python
153
 
 
 
154
  # Declare volumes (NOTE: runtime mounts will override permissions — handle with care)
155
  VOLUME ["/tmp/matplotlib_cache"]
156
  VOLUME ["/tmp/gradio_tmp"]
 
167
  VOLUME ["/tmp"]
168
  VOLUME ["/var/tmp"]
169
 
170
+ USER user
171
 
172
+ EXPOSE $GRADIO_SERVER_PORT
 
 
 
 
 
 
 
 
173
 
174
  ENTRYPOINT ["/home/user/app/entrypoint.sh"]
175
+ CMD ["python", "app.py"]
 
entrypoint.sh CHANGED
@@ -22,12 +22,12 @@ else
22
 
23
  # Start uvicorn server.
24
  echo "Starting with Uvicorn on $GRADIO_SERVER_NAME:$GRADIO_SERVER_PORT"
25
- exec su -s /bin/sh user -c "uvicorn app:app \
26
  --host $GRADIO_SERVER_NAME \
27
  --port $GRADIO_SERVER_PORT \
28
- --proxy-headers"
29
  else
30
  echo "Starting in Gradio mode..."
31
- exec su -s /bin/sh user -c "python app.py"
32
  fi
33
  fi
 
22
 
23
  # Start uvicorn server.
24
  echo "Starting with Uvicorn on $GRADIO_SERVER_NAME:$GRADIO_SERVER_PORT"
25
+ exec uvicorn app:app \
26
  --host $GRADIO_SERVER_NAME \
27
  --port $GRADIO_SERVER_PORT \
28
+ --proxy-headers
29
  else
30
  echo "Starting in Gradio mode..."
31
+ exec python app.py
32
  fi
33
  fi