Repository navigation
feat(wagtail): build venv from requirements.txt in web image - #9018
Conversation
5479765 to
7845bd7
Compare
## 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]>
7845bd7 to
8d3f3aa
Compare
|
I don't understand why we would have been using mutagen with wagtail. |
That's why I created this PR. Note that we had |
rfay
left a comment
There was a problem hiding this comment.
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 \ |
There was a problem hiding this comment.
| --web-environment-add=DJANGO_SETTINGS_MODULE=mysite.settings.dev \ | |
| --web-environment-add=DJANGO_SETTINGS_MODULE=mysite.settings.dev \ | |
| --performance-mode=none \ |
| --webimage-extra-packages=python3-pip,python3-venv \ | ||
| --webimage-extra-packages=python3-venv \ | ||
| --web-environment-add=DJANGO_SETTINGS_MODULE=mysite.settings.dev \ | ||
| --omit-containers=db |
There was a problem hiding this comment.
| --omit-containers=db | |
| --omit-containers=db --performance-mode=none |
No, better not to do that. The first run with Mutagen showed a problem (and there was no problem without Mutagen locally) So I had to change the logic: |
|
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. |
|
|
rfay
left a comment
There was a problem hiding this comment.
Thanks for taking good care of this!
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'sexternally-managed-environmenterror 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 ranpip installtwice by hand.With
python3-pipadded to the web image,pip installfails witherror: 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:
docker-compose.*.yamladds the project root as a build context, and the Dockerfile copies onlyrequirements.txtfrom it. BuildKit uses the file's content as the cache key, soddev restartrebuilds when the file changes.COPY --chmod=644lets the user read the file. Mutagen gives files it syncs from the container mode0600, and the build context gives them to root. A bind mount failed in CI withPermission deniedfor that reason.runuserinstalls the virtual environment as the user, soddev exec pip installworks for temporary packages./etc/bashrc/*.bashrcputs the virtual environment first inPATH. A DockerfileENV PATHdoesn't work, because/etc/profileresetsPATHin login shells./opt, outside the project, so Mutagen has nothing extra to sync.The quickstart writes a placeholder
requirements.txtwithwagtail, becauseCOPYneeds the file before the first build.wagtail startrefuses to overwrite it, so the step removes it first. Theddev restartthat starts the server also rebuilds from Wagtail's ownrequirements.txt.SECURE_PROXY_SSL_HEADERgoes inmysite/settings/local.py, which Wagtail'sdev.pyimports.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
/adminwithadmin/admin, and runddev 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.batsfollows the new quickstart. It also checks thatpythonresolves to/opt/wagtail/bin/python, thatpip installworks as the user, and that an admin login over HTTPS passes Django's CSRF check.🤖 Developed with assistance from Claude Code