-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dockerfile Improvements #21
base: main
Are you sure you want to change the base?
Conversation
comrumino
commented
Jan 17, 2023
- Removed Dockerfile command to install the latest version of psycopg2 since requirements.txt specifies 2.8.6
- Reduced built docker images size by roughly 40% (135MB) by using multistage build so build deps arent distributed for runtime
…since requirements.txt specifies 2.8.6
…stage build so build deps arent distributed for runtime
This is great @comrumino - we don't really have any tests (yet) so I will test manually. |
What's the status of this PR? I think it is also a good opportunity to update the Dockerfile to run the application as an user other than |
@comrumino - I like the approach towards using a slimmer image and incorporating a more restricted user for runtime is quite insightful. While implementing your Dockerfile, I encountered dependency issues, particularly after introducing the Werkzeug package. To address these, I've made some adjustments to your initial suggestion, which includes the integration of the limited user. Could you take a look at these modifications for a review? Here are a few manual test if you have the time.
# Use Python 3.8 slim image as the builder stage
FROM python:3.8.12-slim-buster AS builder
# Install build-time dependencies
RUN apt-get update && apt-get install -y \
libpq-dev \
gcc \
libgirepository1.0-dev \
libcairo2-dev \
libpango1.0-dev \
libgdk-pixbuf2.0-dev \
libffi-dev \
shared-mime-info \
&& rm -rf /var/lib/apt/lists/*
# Install Python dependencies
COPY requirements.txt .
RUN pip install --upgrade pip setuptools wheel && \
pip install -r requirements.txt
# Use Python 3.8 slim image for the runtime stage
FROM python:3.8.12-slim-buster AS app
# Set the working directory
WORKDIR /app
# Install runtime dependencies as root
RUN apt-get update && apt-get install -y \
libpq5 \
libcairo2 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libgdk-pixbuf2.0-0 \
shared-mime-info \
&& rm -rf /var/lib/apt/lists/*
# Create a non-root user and switch to it
RUN useradd -m gapps
# Create the directory for Flask sessions and set permissions
RUN mkdir -p /app/flask_session && chown -R gapps:gapps /app
USER gapps
# Copy installed Python packages from builder stage
COPY --from=builder /usr/local /usr/local/
# Copy the application source code
COPY . .
# Define the command to run the application
CMD ["/bin/bash", "run.sh"] |