seanpedrickcase commited on
Commit
0fd155c
·
1 Parent(s): 7d5387e

Changed Dockerfile to multi-stage build to further reduce size

Browse files
Files changed (1) hide show
  1. Dockerfile +40 -52
Dockerfile CHANGED
@@ -1,80 +1,68 @@
1
- # First stage: build dependencies
2
- #FROM public.ecr.aws/docker/library/python:3.11.9-slim-bookworm
3
- FROM python:3.11.9-slim-bookworm
4
 
5
  # Install Lambda web adapter in case you want to run with with an AWS Lamba function URL (not essential if not using Lambda)
6
  #COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.3 /lambda-adapter /opt/extensions/lambda-adapter
7
 
8
- # Install wget, curl, and build-essential
9
- RUN apt-get update && apt-get install -y \
10
- && rm -rf /var/lib/apt/lists/*
11
-
12
- #wget \
13
- #curl \
14
 
15
- # Create a directory for the model
16
- RUN mkdir /model && mkdir /model/rep && mkdir /model/embed
17
 
18
  WORKDIR /src
19
 
 
20
  COPY requirements_aws.txt .
21
-
22
  RUN pip install --no-cache-dir -r requirements_aws.txt
23
 
24
- # Gradio needs to be installed after due to conflict with spacy in requirements
25
  RUN pip install --no-cache-dir gradio==4.41.0
26
 
27
- # Set up a new user named "user" with user ID 1000
28
- RUN useradd -m -u 1000 user
 
29
 
30
- # Change ownership of /home/user directory
31
- RUN chown -R user:user /home/user
32
 
33
- # Make output folder
34
- RUN mkdir -p /home/user/app/output && chown -R user:user /home/user/app/output \
35
- && mkdir -p /home/user/.cache/huggingface/hub && chown -R user:user /home/user/.cache/huggingface/hub \
36
- && mkdir -p /home/user/.cache/matplotlib && chown -R user:user /home/user/.cache/matplotlib \
37
- && mkdir -p /home/user/app/model/rep && chown -R user:user /home/user/app/model/rep \
38
- && mkdir -p /home/user/app/model/embed && chown -R user:user /home/user/app/model/embed \
39
- && mkdir -p /home/user/app/cache && chown -R user:user /home/user/app/cache
40
 
41
  # Download the quantised phi model directly with curl. Changed at it is so big - not loaded
42
  #RUN curl -L -o /home/user/app/model/rep/Phi-3.1-mini-128k-instruct-Q4_K_M.gguf https://huggingface.co/bartowski/Phi-3.1-mini-128k-instruct-GGUF/tree/main/Phi-3.1-mini-128k-instruct-Q4_K_M.gguf
43
 
44
- # Download the Mixed bread embedding model during the build process - changed as it is too big for AWS. Not loaded.
45
- #RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash
46
- #RUN apt-get install git-lfs -y
47
- #RUN git lfs install
48
- #RUN git clone https://huggingface.co/mixedbread-ai/mxbai-embed-large-v1 /home/user/app/model/embed
49
- #RUN rm -rf /home/user/app/model/embed/.git
50
-
51
- # Download the embedding model - Create a directory for the model and download specific files using huggingface_hub
52
- COPY download_model.py /src/download_model.py
53
- RUN python /src/download_model.py
54
 
55
- # Switch to the "user" user
56
  USER user
57
 
58
- # Set home to the user's home directory
59
  ENV HOME=/home/user \
60
- PATH=/home/user/.local/bin:$PATH \
61
  PYTHONPATH=/home/user/app \
62
- PYTHONUNBUFFERED=1 \
63
- PYTHONDONTWRITEBYTECODE=1 \
64
- GRADIO_ALLOW_FLAGGING=never \
65
- GRADIO_NUM_PORTS=1 \
66
- GRADIO_SERVER_NAME=0.0.0.0 \
67
- GRADIO_SERVER_PORT=7860 \
68
- GRADIO_THEME=huggingface \
69
- AWS_STS_REGIONAL_ENDPOINT=regional \
70
- GRADIO_OUTPUT_FOLDER='output/' \
71
- NUMBA_CACHE_DIR=/home/user/app/cache \
72
- SYSTEM=spaces
73
-
74
- # Set the working directory to the user's home directory
75
  WORKDIR $HOME/app
76
-
77
- # Copy the current directory contents into the container at $HOME/app setting the owner to the user
78
  COPY --chown=user . $HOME/app
79
 
 
80
  CMD ["python", "app.py"]
 
1
+ # Stage 1: Build dependencies and download models
2
+ FROM python:3.11.9-slim-bookworm AS builder
 
3
 
4
  # Install Lambda web adapter in case you want to run with with an AWS Lamba function URL (not essential if not using Lambda)
5
  #COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.3 /lambda-adapter /opt/extensions/lambda-adapter
6
 
7
+ # Install system dependencies
8
+ RUN apt-get update && apt-get install -y --no-install-recommends \
9
+ && rm -rf /var/lib/apt/lists/*
 
 
 
10
 
11
+ # Create directories (if needed for model download script)
12
+ RUN mkdir -p /model/rep /model/embed
13
 
14
  WORKDIR /src
15
 
16
+ # Copy requirements file and install dependencies
17
  COPY requirements_aws.txt .
 
18
  RUN pip install --no-cache-dir -r requirements_aws.txt
19
 
20
+ # Install Gradio
21
  RUN pip install --no-cache-dir gradio==4.41.0
22
 
23
+ # Download models (using your download_model.py script)
24
+ COPY download_model.py /src/download_model.py
25
+ RUN python /src/download_model.py
26
 
27
+ # Stage 2: Final runtime image
28
+ FROM python:3.11.9-slim-bookworm
29
 
30
+ # Create a non-root user
31
+ RUN useradd -m -u 1000 user
32
+
33
+ # Create necessary directories and set ownership
34
+ RUN mkdir -p /home/user/app/output /home/user/.cache/huggingface/hub /home/user/.cache/matplotlib /home/user/app/cache \
35
+ && chown -R user:user /home/user
 
36
 
37
  # Download the quantised phi model directly with curl. Changed at it is so big - not loaded
38
  #RUN curl -L -o /home/user/app/model/rep/Phi-3.1-mini-128k-instruct-Q4_K_M.gguf https://huggingface.co/bartowski/Phi-3.1-mini-128k-instruct-GGUF/tree/main/Phi-3.1-mini-128k-instruct-Q4_K_M.gguf
39
 
40
+ # Copy models from the builder stage
41
+ COPY --from=builder /model/rep /home/user/app/model/rep
42
+ COPY --from=builder /model/embed /home/user/app/model/embed
 
 
 
 
 
 
 
43
 
44
+ # Switch to the non-root user
45
  USER user
46
 
47
+ # Set environment variables
48
  ENV HOME=/home/user \
49
+ PATH=/home/user/.local/bin:$PATH \
50
  PYTHONPATH=/home/user/app \
51
+ PYTHONUNBUFFERED=1 \
52
+ PYTHONDONTWRITEBYTECODE=1 \
53
+ GRADIO_ALLOW_FLAGGING=never \
54
+ GRADIO_NUM_PORTS=1 \
55
+ GRADIO_SERVER_NAME=0.0.0.0 \
56
+ GRADIO_SERVER_PORT=7860 \
57
+ GRADIO_THEME=huggingface \
58
+ AWS_STS_REGIONAL_ENDPOINT=regional \
59
+ GRADIO_OUTPUT_FOLDER='output/' \
60
+ NUMBA_CACHE_DIR=/home/user/app/cache \
61
+ SYSTEM=spaces
62
+
63
+ # Set working directory and copy application code
64
  WORKDIR $HOME/app
 
 
65
  COPY --chown=user . $HOME/app
66
 
67
+ # Command to run your application
68
  CMD ["python", "app.py"]