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 stages to Dockerfile.
- Moved package removal to production stage.
- Added target dev to compose file.
  • Loading branch information
pbratkowski committed Dec 13, 2024
1 parent ff2fd01 commit 5424f72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
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 djangoproject-www-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 djangoproject-www-base AS djangoproject-www-dev

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


FROM djangoproject-www-dev AS djangoproject-www-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: djangoproject-www-dev
args:
- REQ_FILE=requirements/tests.txt
entrypoint: ./docker-entrypoint.dev.sh
Expand Down

0 comments on commit 5424f72

Please sign in to comment.