Skip to content

feat(wagtail): build venv from requirements.txt in web image - #9018

Merged
stasadev merged 1 commit into
ddev:mainfrom
stasadev:20261027_stasadev_wagtail_requirements
Oct 27, 2026
Merged

stasadev merged 1 commit into
ddev:mainfrom
stasadev:20261027_stasadev_wagtail_requirements

Conversation

@stasadev

@stasadev stasadev commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Short Summary (TL;DR)

The Wagtail quickstart now builds its virtual environment in the web image from the project's requirements.txt, so Mutagen no longer syncs about 14,000 files. A new section in "Customizing Docker Images" shows the same pattern for any Python project, so users who hit pip's externally-managed-environment error have a page to follow.

The Issue

The Wagtail quickstart created its virtual environment in the project directory with ddev exec. A Wagtail virtual environment has about 14,000 files (220 MB), and Mutagen synced all of them to the host. That slowed down the first sync and every later sync check. The quickstart also ran pip install twice by hand.

With python3-pip added to the web image, pip install fails with error: externally-managed-environment, because Debian manages the system Python. The docs didn't say what to do instead.

How This PR Solves The Issue

The pattern is adapted from mautic/developer-documentation-new#859:

  • A docker-compose.*.yaml adds the project root as a build context, and the Dockerfile copies only requirements.txt from it. BuildKit uses the file's content as the cache key, so ddev restart rebuilds when the file changes.
  • COPY --chmod=644 lets the user read the file. Mutagen gives files it syncs from the container mode 0600, and the build context gives them to root. A bind mount failed in CI with Permission denied for that reason.
  • runuser installs the virtual environment as the user, so ddev exec pip install works for temporary packages.
  • /etc/bashrc/*.bashrc puts the virtual environment first in PATH. A Dockerfile ENV PATH doesn't work, because /etc/profile resets PATH in login shells.
  • The virtual environment is in /opt, outside the project, so Mutagen has nothing extra to sync.

The quickstart writes a placeholder requirements.txt with wagtail, because COPY needs the file before the first build. wagtail start refuses to overwrite it, so the step removes it first. The ddev restart that starts the server also rebuilds from Wagtail's own requirements.txt. SECURE_PROXY_SSL_HEADER goes in mysite/settings/local.py, which Wagtail's dev.py imports.

Manual Testing Instructions

Follow the Wagtail quickstart in the preview: https://ddev--9018.org.readthedocs.build/en/9018/users/quickstart/#wagtail-python-generic

Then log in to /admin with admin/admin, and run ddev exec pip install django-debug-toolbar.

The new section is at https://ddev--9018.org.readthedocs.build/en/9018/users/extend/customizing-images/#python-packages-from-requirementstxt

Automated Testing Overview

docs/tests/wagtail.bats follows the new quickstart. It also checks that python resolves to /opt/wagtail/bin/python, that pip install works as the user, and that an admin login over HTTPS passes Django's CSRF check.

🤖 Developed with assistance from Claude Code

@stasadev
stasadev requested a review from a team as a code owner October 27, 2026 11:23
@stasadev
stasadev force-pushed the 20261027_stasadev_wagtail_requirements branch 2 times, most recently from 5479765 to 7845bd7 Compare October 27, 2026 11:34
## Short Summary (TL;DR)

The Wagtail quickstart now builds its virtual environment in the web image from the project's `requirements.txt`, so Mutagen no longer syncs about 14,000 files. A new section in "Customizing Docker Images" shows the same pattern for any Python project, so users who hit pip's `externally-managed-environment` error have a page to follow.

## The Issue

The Wagtail quickstart created its virtual environment in the project directory with `ddev exec`. A Wagtail virtual environment has about 14,000 files (220 MB), and Mutagen synced all of them to the host. That slowed down the first sync and every later sync check. The quickstart also ran `pip install` twice by hand.

With `python3-pip` added to the web image, `pip install` fails with `error: externally-managed-environment`, because Debian manages the system Python. The docs didn't say what to do instead.

## How This PR Solves The Issue

The pattern is adapted from mautic/developer-documentation-new#859:

- A `docker-compose.*.yaml` adds the project root as a build context, and the Dockerfile copies only `requirements.txt` from it. BuildKit uses the file's content as the cache key, so `ddev restart` rebuilds when the file changes.
- `COPY --chmod=644` lets the user read the file. Mutagen gives files it syncs from the container mode `0600`, and the build context gives them to root. A bind mount failed in CI with `Permission denied` for that reason.
- `runuser` installs the virtual environment as the user, so `ddev exec pip install` works for temporary packages.
- `/etc/bashrc/*.bashrc` puts the virtual environment first in `PATH`. A Dockerfile `ENV PATH` doesn't work, because `/etc/profile` resets `PATH` in login shells.
- The virtual environment is in `/opt`, outside the project, so Mutagen has nothing extra to sync.

The quickstart writes a placeholder `requirements.txt` with `wagtail`, because `COPY` needs the file before the first build. `wagtail start` refuses to overwrite it, so the step removes it first. The `ddev restart` that starts the server also rebuilds from Wagtail's own `requirements.txt`. `SECURE_PROXY_SSL_HEADER` goes in `mysite/settings/local.py`, which Wagtail's `dev.py` imports.

## Manual Testing Instructions

Follow the Wagtail quickstart in the preview: https://ddev--9018.org.readthedocs.build/en/9018/users/quickstart/#wagtail-python-generic

Then log in to `/admin` with `admin`/`admin`, and run `ddev exec pip install django-debug-toolbar`.

The new section is at https://ddev--9018.org.readthedocs.build/en/9018/users/extend/customizing-images/#python-packages-from-requirementstxt

## Automated Testing Overview

`docs/tests/wagtail.bats` follows the new quickstart. It also checks that `python` resolves to `/opt/wagtail/bin/python`, that `pip install` works as the user, and that an admin login over HTTPS passes Django's CSRF check.

🤖 Developed with assistance from [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stasadev
stasadev force-pushed the 20261027_stasadev_wagtail_requirements branch from 7845bd7 to 8d3f3aa Compare October 27, 2026 12:36
@rfay

rfay commented Oct 27, 2026

Copy link
Copy Markdown
Member

I don't understand why we would have been using mutagen with wagtail.

@stasadev

Copy link
Copy Markdown
Member Author

I don't understand why we would have been using mutagen with wagtail.

That's why I created this PR. Note that we had ddev mutagen sync in our tests for Wagtail, and now there's none.

@rfay rfay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested turning off mutagen, thanks for chasing this!

ddev config --project-type=generic --webserver-type=generic \
--webimage-extra-packages=python3-pip,python3-venv \
--webimage-extra-packages=python3-venv \
--web-environment-add=DJANGO_SETTINGS_MODULE=mysite.settings.dev \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
--web-environment-add=DJANGO_SETTINGS_MODULE=mysite.settings.dev \
--web-environment-add=DJANGO_SETTINGS_MODULE=mysite.settings.dev \
--performance-mode=none \

Comment thread docs/tests/wagtail.bats
--webimage-extra-packages=python3-pip,python3-venv \
--webimage-extra-packages=python3-venv \
--web-environment-add=DJANGO_SETTINGS_MODULE=mysite.settings.dev \
--omit-containers=db

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
--omit-containers=db
--omit-containers=db --performance-mode=none

@stasadev

Copy link
Copy Markdown
Member Author

Suggested turning off mutagen, thanks for chasing this!

No, better not to do that.

The first run with Mutagen showed a problem (and there was no problem without Mutagen locally)

https://github.com/ddev/ddev/actions/runs/40497114741/job/122165558765#step:10:378

ERROR: Could not open requirements file: [Errno 13] Permission denied: '/tmp/requirements.txt'

So I had to change the logic:

https://github.com/ddev/ddev/compare/7845bd75e82c28f42dc5eb464909a610758ab155..8d3f3aa7f1ecb46a52eeb956e2ee9b510987e307

@rfay

rfay commented Oct 27, 2026

Copy link
Copy Markdown
Member

I guess I'm not understanding why the example could not turn off mutagen. On some Docker providers you can't use /tmp though, but don't think that's what you're saying.

@stasadev

Copy link
Copy Markdown
Member Author

RUN --mount=type=bind failed with Mutagen, meaning if it was already disabled, Wagtail's quickstart in this PR would fail on macOS (and tests wouldn't show it). Testing on Mutagen is good.

@rfay rfay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking good care of this!

@stasadev
stasadev merged commit 1e363b1 into ddev:main Oct 27, 2026
11 of 12 checks passed
@stasadev
stasadev deleted the 20261027_stasadev_wagtail_requirements branch October 27, 2026 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants