Skip to content

Commit 09a38f4

Browse files
committed
Reduce size of docker image by adding building stage
1 parent 057d81b commit 09a38f4

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Dockerfile

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
FROM python:3.12-alpine
1+
# Stage 1: Build stage
2+
FROM python:3.12-alpine AS builder
23

34
WORKDIR /app
45

5-
COPY requirements.txt ./
6-
7-
8-
RUN apk add gcc musl-dev mariadb-connector-c-dev libpq-dev
6+
COPY requirements.txt .
97

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

1110
RUN pip install --no-cache-dir --upgrade pip && \
1211
pip install --no-cache-dir gunicorn && \
1312
pip install --no-cache-dir -r requirements.txt
1413

14+
# Stage 2: Final stage
15+
FROM python:3.12-alpine
16+
17+
WORKDIR /app
18+
19+
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
20+
COPY --from=builder /usr/local/bin/gunicorn /usr/local/bin/gunicorn
21+
COPY --from=builder /app /app
22+
23+
RUN apk add --no-cache mariadb-connector-c libpq
24+
1525
COPY . .
16-
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
17-
EXPOSE 8000/tcp
26+
27+
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"]
28+
29+
EXPOSE 8000/tcp

0 commit comments

Comments
 (0)