Open
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
Owner
|
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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.