-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce multi-stage Docker build (#1636)
To ensure that the 'dev' and 'prod' container images are derived from the same base image and reduce the amount of divergence between them.
- Loading branch information
Showing
15 changed files
with
108 additions
and
105 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,58 @@ | ||
# Use an official Python runtime based on Debian 10 "buster" as a parent image. | ||
FROM python:3.8.1-slim-buster | ||
|
||
# Add user that will be used in the container. | ||
RUN useradd wagtail | ||
|
||
# Port used by this container to serve HTTP. | ||
EXPOSE 8000 | ||
|
||
# Set environment variables. | ||
# 1. Force Python stdout and stderr streams to be unbuffered. | ||
# 2. Set PORT variable that is used by Gunicorn. This should match "EXPOSE" | ||
# command. | ||
ENV PYTHONUNBUFFERED=1 \ | ||
PORT=8000 | ||
|
||
COPY requirements.txt / | ||
|
||
# Install system packages required by Wagtail and Django. | ||
FROM python:3.8.17-slim-bookworm AS builder | ||
ENV PIP_NO_CACHE_DIR=1 \ | ||
PYTHONUNBUFFERED=1 | ||
RUN apt-get update --yes --quiet \ | ||
&& apt-get install --yes --quiet --no-install-recommends \ | ||
&& apt-get install --yes --quiet --no-install-recommends \ | ||
build-essential \ | ||
gettext \ | ||
git \ | ||
libpq5 \ | ||
libpango1.0-0 \ | ||
libpq-dev \ | ||
libpq5 \ | ||
&& pip install --upgrade pip \ | ||
&& pip install pip-tools \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
WORKDIR /opt | ||
RUN python -m venv venv | ||
ENV PATH="/opt/venv/bin:$PATH" | ||
RUN pip install --upgrade pip | ||
ARG requirements=requirements.txt | ||
COPY ${requirements} . | ||
RUN pip install -r $requirements | ||
|
||
FROM python:3.8.17-slim-bookworm AS base | ||
EXPOSE 8000 | ||
ENV PATH="/opt/venv/bin:$PATH" \ | ||
PIP_NO_CACHE_DIR=1 \ | ||
PYTHONUNBUFFERED=1 | ||
RUN useradd wagtail | ||
RUN apt-get update --yes --quiet \ | ||
&& apt-get install --yes --quiet --no-install-recommends \ | ||
gettext \ | ||
libpango1.0-0 \ | ||
&& pip install --no-cache-dir --upgrade pip \ | ||
&& pip install --no-cache-dir "gunicorn==20.0.4" \ | ||
&& pip install --no-cache-dir -r /requirements.txt \ | ||
&& apt remove --yes --quiet git build-essential libpq-dev \ | ||
&& apt autoremove --yes --quiet \ | ||
libpq5 \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Use /app folder as a directory where the source code is stored. | ||
WORKDIR /app | ||
|
||
# Set this directory to be owned by the "wagtail" user. This Wagtail project | ||
# uses SQLite, the folder needs to be owned by the user that | ||
# will be writing to the database file. | ||
RUN chown wagtail:wagtail /app | ||
|
||
# Copy the source code of the project into the container. | ||
FROM base AS dev | ||
RUN apt-get update --yes --quiet \ | ||
&& apt-get install --yes --quiet --no-install-recommends \ | ||
tini \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
COPY --from=builder --chown=wagtail:wagtail /opt/venv /opt/venv | ||
COPY --chown=wagtail:wagtail . . | ||
|
||
# Use user "wagtail" to run the build commands below and the server itself. | ||
USER wagtail | ||
ENTRYPOINT ["tini", "--"] | ||
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] | ||
|
||
# Collect static files. | ||
FROM base AS prod | ||
# PORT variable is used by Gunicorn. Should match "EXPOSE" command. | ||
ENV PORT=8000 | ||
USER wagtail | ||
COPY --from=builder --chown=wagtail:wagtail /opt/venv /opt/venv | ||
RUN pip install "gunicorn==20.0.4" | ||
COPY --chown=wagtail:wagtail . . | ||
RUN python manage.py collectstatic --noinput --clear | ||
|
||
# Compile files for localization | ||
RUN python manage.py compilemessages | ||
|
||
# Start the application server. | ||
CMD gunicorn iogt.wsgi:application | ||
CMD ["gunicorn", "iogt.wsgi:application"] |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: "3" | ||
|
||
services: | ||
builder: | ||
build: | ||
context: . | ||
target: builder | ||
command: | ||
- /app/scripts/compile-requirements.sh | ||
volumes: | ||
- ./:/app/ |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,50 @@ | ||
# Introduce pip-tools | ||
|
||
## What is it? | ||
Pip-tools is a command-line toolset for managing dependencies in Python projects. | ||
It streamlines the process of handling project dependencies by providing essential functionalities. | ||
Pip-tools is a command-line toolset for managing dependencies in Python projects. It streamlines the process of handling project dependencies by providing essential functionalities. | ||
|
||
## Why do we use it? | ||
|
||
### Dependency Management | ||
Pip-tools helps manage a project's dependencies by resolving and pinning specific versions. | ||
It ensures that everyone working on the project uses the same versions of dependencies, promoting consistency and reproducibility. | ||
|
||
Pip-tools helps manage a project's dependencies by resolving and pinning specific versions. It ensures that everyone working on the project uses the same versions of dependencies, promoting consistency and reproducibility. | ||
|
||
### Simplified Workflow | ||
Pip-tools simplifies the workflow of managing dependencies by keeping the requirements.in file separate from the requirements.txt file. | ||
|
||
The requirements.in file contains high-level dependencies, while the requirements.txt file contains the pinned versions. This separation enables easy updating and maintenance of dependencies. | ||
|
||
## How do we use it? | ||
|
||
### For Virtual Environment: | ||
### For virtual environment: | ||
|
||
#### Install or Upgrade pip: | ||
```pip install --upgrade pip``` | ||
Install or upgrade pip. | ||
|
||
#### Install pip-tools: | ||
```pip install pip-tools``` | ||
```sh | ||
pip install --upgrade pip | ||
``` | ||
|
||
#### Generate Pinned Versions: | ||
Add or update the package and its version specifier according to your needs in <requirements_file>.in file. | ||
Install pip-tools. | ||
|
||
```pip-compile --generate-hashes --resolver=backtracking -o <requirements_file>.txt <requirements_file>.in``` | ||
```sh | ||
pip install pip-tools | ||
``` | ||
|
||
To generate pinned versions, add or update a package and its version specifier according to your needs in the _requirements.in_ or _requirements.dev.in_ file. | ||
|
||
```sh | ||
pip-compile --generate-hashes --resolver=backtracking -o <requirements_file>.txt <requirements_file>.in | ||
``` | ||
|
||
### For Docker | ||
Add or update the package and its version specifier according to your needs in <requirements_file>.in file. | ||
|
||
Add or update the package and its version specifier according to your needs in a _\*.in_ file. In the project root directory. | ||
|
||
``` | ||
make compile-requirements | ||
``` | ||
``` | ||
|
||
Alternatively, you could run `pip-compile` directly in the _builder_ container. | ||
|
||
``` | ||
docker compose -f docker-compose.builder.yml run --rm builder pip-compile ... | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
-r requirements.txt | ||
beautifulsoup4 | ||
coverage | ||
django-test-plus>=1.4.0 | ||
factory-boy==3.2.* | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Markdown==3.3.7 | ||
beautifulsoup4~=4.9.3 | ||
cairosvg==2.7.0 | ||
django-allauth~=0.44 | ||
django-comments-xtd==2.9.* | ||
|
This file contains 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
This file contains 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