Skip to content

Commit

Permalink
Update Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlesnorris509 authored Nov 12, 2024
1 parent b37857c commit 9e095b7
Showing 1 changed file with 41 additions and 45 deletions.
86 changes: 41 additions & 45 deletions application/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,88 +1,84 @@
# Builder Stage
FROM ubuntu:24.04 as builder

ENV DEBIAN_FRONTEND=noninteractive
# Set environment variables and non-interactive mode
ENV DEBIAN_FRONTEND=noninteractive \
PATH="/venv/bin:$PATH" \
PYTHON_VERSION=3.11 \
MODEL_URL="https://d3dg1063dc54p9.cloudfront.net/models/embeddings/mpnet-base-v2.zip"

# Install necessary dependencies and set up Python
RUN apt-get update && \
apt-get install -y software-properties-common && \
apt-get install -y --no-install-recommends software-properties-common && \
add-apt-repository ppa:deadsnakes/ppa && \
# Install necessary packages and Python
apt-get update && \
apt-get install -y --no-install-recommends gcc wget unzip libc6-dev python3.11 python3.11-distutils python3.11-venv && \
rm -rf /var/lib/apt/lists/*
rm -rf /var/lib/apt/lists/*

# Verify Python installation and setup symlink
RUN if [ -f /usr/bin/python3.11 ]; then \
ln -s /usr/bin/python3.11 /usr/bin/python; \
else \
echo "Python 3.11 not found"; exit 1; \
fi
# Set up Python symlink in one step to minimize layers
RUN ln -s /usr/bin/python3.11 /usr/bin/python

# Download and unzip the model
RUN wget https://d3dg1063dc54p9.cloudfront.net/models/embeddings/mpnet-base-v2.zip && \
unzip mpnet-base-v2.zip -d model && \
rm mpnet-base-v2.zip
# Download and unzip the model in a single layer, then clean up to save space
RUN wget -q $MODEL_URL -O model.zip && \
unzip model.zip -d model && \
rm model.zip

# Install Rust
# Install Rust for building dependencies that require it
RUN wget -q -O - https://sh.rustup.rs | sh -s -- -y

# Clean up to reduce container size
RUN apt-get remove --purge -y wget unzip && apt-get autoremove -y && rm -rf /var/lib/apt/lists/*

# Copy requirements.txt
# Copy requirements file early to leverage Docker caching of dependencies
COPY requirements.txt .

# Setup Python virtual environment
RUN python3.11 -m venv /venv
# Setup Python virtual environment and install Python packages in a single layer
RUN python3.11 -m venv /venv && \
/venv/bin/pip install --no-cache-dir --upgrade pip && \
/venv/bin/pip install --no-cache-dir tiktoken && \
/venv/bin/pip install --no-cache-dir -r requirements.txt

# Activate virtual environment and install Python packages
ENV PATH="/venv/bin:$PATH"

# Install Python packages
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir tiktoken && \
pip install --no-cache-dir -r requirements.txt

# Final Stage
FROM ubuntu:24.04 as final

# Environment and path setup for final stage
ENV DEBIAN_FRONTEND=noninteractive \
FLASK_APP=app.py \
FLASK_DEBUG=true \
PATH="/venv/bin:$PATH" \
PYTHON_VERSION=3.11

# Install Python runtime and link it in one command to reduce layers
RUN apt-get update && \
apt-get install -y software-properties-common && \
apt-get install -y --no-install-recommends software-properties-common && \
add-apt-repository ppa:deadsnakes/ppa && \
# Install Python
apt-get update && apt-get install -y --no-install-recommends python3.11 && \
apt-get update && \
apt-get install -y --no-install-recommends python3.11 && \
ln -s /usr/bin/python3.11 /usr/bin/python && \
rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app

# Create a non-root user: `appuser` (Feel free to choose a name)
# Create a non-root user for running the app and set permissions
RUN groupadd -r appuser && \
useradd -r -g appuser -d /app -s /sbin/nologin -c "Docker image user" appuser
useradd -r -g appuser -d /app -s /sbin/nologin -c "Docker image user" appuser && \
mkdir -p /app/application/inputs/local && \
chown -R appuser:appuser /app

# Copy the virtual environment and model from the builder stage
COPY --from=builder /venv /venv
COPY --from=builder /model /app/model

# Copy your application code
# Copy application code into the image
COPY . /app/application

# Change the ownership of the /app directory to the appuser

RUN mkdir -p /app/application/inputs/local
# Set ownership of the /app directory to appuser
RUN chown -R appuser:appuser /app

# Set environment variables
ENV FLASK_APP=app.py \
FLASK_DEBUG=true \
PATH="/venv/bin:$PATH"

# Expose the port the app runs on
# Expose the application port
EXPOSE 7091

# Switch to non-root user
USER appuser

# Start Gunicorn
CMD ["gunicorn", "-w", "2", "--timeout", "120", "--bind", "0.0.0.0:7091", "application.wsgi:app"]
# Start the application with Gunicorn
CMD ["gunicorn", "-w", "2", "--timeout", "120", "--bind", "0.0.0.0:7091", "application.wsgi:app"]

0 comments on commit 9e095b7

Please sign in to comment.