Skip to content

Commit

Permalink
Implemented Docker multi-stage build
Browse files Browse the repository at this point in the history
This solves the first part of django#1817, where tox fails to install test
dependencies due to missing packages.

- Added target inputs to GitHub workflows.
- Added stages to Dockerfile.
- Moved package removal to production stage.
- Added target dev to compose file.
  • Loading branch information
pbratkowski committed Dec 10, 2024
1 parent 75f3fde commit d4f8800
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
target: django-web-prod
1 change: 1 addition & 0 deletions .github/workflows/docker-test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ jobs:
push: false
build-args: |
REQ_FILE=requirements/${{ matrix.req_file }}
target: django-web-dev
23 changes: 15 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pull official base image
FROM python:3.12-slim-bookworm
FROM python:3.12-slim-bookworm AS django-web-base

# set work directory
WORKDIR /usr/src/app
Expand Down Expand Up @@ -33,13 +33,10 @@ RUN apt-get update \
libc6-dev \
libpq-dev \
zlib1g-dev \
&& python3 -m pip install --no-cache-dir -r ${REQ_FILE} \
&& apt-get purge --assume-yes --auto-remove \
gcc \
libc6-dev \
libpq-dev \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*
&& python3 -m pip install --no-cache-dir -r ${REQ_FILE}


FROM django-web-base AS django-web-dev

# install node dependencies
COPY ./package.json ./package.json
Expand All @@ -48,5 +45,15 @@ RUN npm install
# copy project
COPY . .


FROM django-web-dev AS django-web-prod

RUN apt-get purge --assume-yes --auto-remove \
gcc \
libc6-dev \
libpq-dev \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*

# ENTRYPOINT is specified only in the local docker-compose.yml to avoid
# accidentally running it in deployed environments.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ services:
build:
context: ./
dockerfile: Dockerfile
target: django-web-dev
args:
- REQ_FILE=requirements/tests.txt
entrypoint: ./docker-entrypoint.dev.sh
Expand Down

0 comments on commit d4f8800

Please sign in to comment.