Skip to content

Commit

Permalink
Reduce size of docker image by adding building stage
Browse files Browse the repository at this point in the history
  • Loading branch information
maexled committed Jul 14, 2024
1 parent 057d81b commit 09a38f4
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
FROM python:3.12-alpine
# Stage 1: Build stage
FROM python:3.12-alpine AS builder

WORKDIR /app

COPY requirements.txt ./


RUN apk add gcc musl-dev mariadb-connector-c-dev libpq-dev
COPY requirements.txt .

RUN apk add --no-cache --virtual .build-deps gcc musl-dev mariadb-connector-c-dev libpq-dev

RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir gunicorn && \
pip install --no-cache-dir -r requirements.txt

# Stage 2: Final stage
FROM python:3.12-alpine

WORKDIR /app

COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin/gunicorn /usr/local/bin/gunicorn
COPY --from=builder /app /app

RUN apk add --no-cache mariadb-connector-c libpq

COPY . .
CMD python manage.py makemigrations && python manage.py migrate && python manage.py collectstatic --noinput && gunicorn --bind=0.0.0.0:8000 --timeout 300 --workers=3 --threads=3 --max-requests 5 --max-requests-jitter 2 pim.wsgi:application
EXPOSE 8000/tcp

CMD ["sh", "-c", "python manage.py makemigrations && python manage.py migrate && python manage.py collectstatic --noinput && gunicorn --bind=0.0.0.0:8000 --timeout 300 --workers=3 --threads=3 --max-requests 5 --max-requests-jitter 2 pim.wsgi:application"]

EXPOSE 8000/tcp

0 comments on commit 09a38f4

Please sign in to comment.