SkyNetWalker commited on
Commit
72ac111
·
verified ·
1 Parent(s): fb249e4

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +37 -41
Dockerfile CHANGED
@@ -1,54 +1,50 @@
1
- # Use the official Ollama base image, which already includes Ollama
2
- FROM ollama/ollama:latest
3
 
4
  # Set environment variables to prevent interactive prompts during apt operations
5
  ENV DEBIAN_FRONTEND=noninteractive
6
 
7
- # Install system dependencies: git (optional), python3 and pip for the application
8
- RUN apt update && apt install -y git python3 python3-pip
9
 
10
- # Set up a non-root user as recommended for Hugging Face Spaces
 
 
 
 
 
 
 
 
11
  RUN useradd -m -u 1000 user
12
- USER user
13
- ENV HOME=/home/user \
14
- PATH=/home/user/.local/bin:$PATH \
15
- OLLAMA_HOST=0.0.0.0 # Allow Ollama to be accessed from outside localhost
16
-
17
- # Set the working directory inside the container
18
- WORKDIR $HOME/app
19
-
20
- # Define the model to be pulled
21
- ENV OLLAMA_HF_MODEL="hf.co/unsloth/gemma-3-4b-it-qat-GGUF:Q4_K_M"
22
-
23
- # Pull the model during the build process
24
- # Start ollama serve in background, wait for it, pull the model, then kill ollama.
25
- # This ensures the model is downloaded and cached within the image.
26
- RUN ollama serve & \
27
- echo "Waiting for Ollama to start for model pull..." && \
28
- sleep 5 && \
29
- while ! curl -s http://localhost:11434 > /dev/null; do \
30
- sleep 1; \
31
- done && \
32
- echo "Ollama started. Pulling model: ${OLLAMA_HF_MODEL}" && \
33
- ollama pull ${OLLAMA_HF_MODEL} && \
34
- echo "Model pull complete. Stopping Ollama for build process." && \
35
- pkill ollama || true # '|| true' to prevent build failure if pkill returns non-zero when ollama already stopped
36
-
37
- # Copy the application files
38
- COPY app.py .
39
- COPY requirements.txt .
40
- COPY run.sh .
41
-
42
- # Install Python dependencies required by your Gradio application
43
  RUN pip install --no-cache-dir -r requirements.txt
44
 
45
- # Make the startup script executable
46
  RUN chmod +x run.sh
47
 
48
- # Expose the port that your Gradio application will listen on.
49
- # Hugging Face Spaces typically use port 7860 for Gradio apps.
 
 
 
 
 
 
 
 
50
  EXPOSE 7860
51
 
52
- # Set the entrypoint for the container to execute our startup script.
53
- # This script will start Ollama and then your application.
54
  CMD ["./run.sh"]
 
1
+ # Start with a Python base image, which is convenient for running the Gradio app.
2
+ FROM python:3.9
3
 
4
  # Set environment variables to prevent interactive prompts during apt operations
5
  ENV DEBIAN_FRONTEND=noninteractive
6
 
7
+ # Install system dependencies: curl for Ollama and wget for model download.
8
+ RUN apt-get update && apt-get install -y curl wget
9
 
10
+ # Install Ollama using its official installation script.
11
+ RUN curl -fsSL https://ollama.com/install.sh | sh
12
+
13
+ # Set environment variables for the model for easy modification.
14
+ ENV MODEL_REPO="unsloth/gemma-3-4b-it-qat-GGUF"
15
+ ENV MODEL_FILENAME="gemma-3-4b-it-qat.Q4_K_M.gguf"
16
+
17
+ # Create a non-root user and switch to its home directory. This is a best practice for security and avoids permission errors with model caching.
18
+ # As seen in the Hugging Face Docker guide. [1]
19
  RUN useradd -m -u 1000 user
20
+ WORKDIR /home/user/app
21
+
22
+ # Download the GGUF model file directly from Hugging Face Hub.
23
+ RUN wget -O ${MODEL_FILENAME} https://huggingface.co/${MODEL_REPO}/resolve/main/${MODEL_FILENAME}
24
+
25
+ # Copy the application files and set the correct ownership to the new user. [1]
26
+ COPY --chown=user:user Modelfile .
27
+ COPY --chown=user:user app.py .
28
+ COPY --chown=user:user requirements.txt .
29
+ COPY --chown=user:user run.sh .
30
+
31
+ # Install Python dependencies.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  RUN pip install --no-cache-dir -r requirements.txt
33
 
34
+ # Make the startup script executable.
35
  RUN chmod +x run.sh
36
 
37
+ # Switch to the non-root user. [1]
38
+ USER user
39
+
40
+ # Set environment variables for the user and for Ollama.
41
+ # This ensures cached models are stored in a writable directory and that Ollama is accessible within the container network. [1]
42
+ ENV HOME=/home/user
43
+ ENV PATH=/home/user/.local/bin:$PATH
44
+ ENV OLLAMA_HOST=0.0.0.0
45
+
46
+ # Expose the port for the Gradio application.
47
  EXPOSE 7860
48
 
49
+ # Set the command to run our startup script.
 
50
  CMD ["./run.sh"]