Change Docker image to multi-stage build for Django#2815
Change Docker image to multi-stage build for Django#2815browniebroke merged 15 commits intocookiecutter:masterfrom
Conversation
… resolving conflicts in package sub-dependencies. Ends up being much more trouble than it's worth.
…ly in the run stage
|
Wouldn't it be better to move the python requirements processing after the system requirements installation? Maybe I am wrong, but as you have it now, a change in a requirements file would force the system requirements to be installed since they are in a single command. I believe this is how it is currently implemented. |
…ntal image build times. The extra image layer is inconsequential since python-build-stage layers are discarded anyway.
|
@demestav Good catch. That would be a great thing to do in the |
…e then used to install all requirements. This way the generated images would be a lot more stable as well. Size increases by 2-3mb though
|
Docker will cache tags like |
|
I do not understand why you use wheel, as you need to install libq and do other steps in dockerfile. You can just copy libraries from builder steps ? |
|
@mwolff44 I tried doing that but even building wheels requires those dependencies to be installed. And the final run stage also needs them for postgres and django to run correctly that is why they are in 2 places. |
They are now the default and cause a warning: WARNING: --use-feature=2020-resolver no longer has any effect, since it is now the default dependency resolver in pip. This will become an error in pip 21.0.
|
@browniebroke let me update this PR. I believe I had come up with a better way to write this multi stage dockerfile. |
…and easier to update in mind.
|
@browniebroke I have updated both the files with the following changes: I would suggest building docker images by enabling docker buildkit as that allows one to also cache all apt and pip packages on the host system as well and can greatly speed up re-builds. All one would have to do is add the following line to the top of the dockerfile:
|
{{cookiecutter.project_slug}}/compose/production/django/Dockerfile
Outdated
Show resolved
Hide resolved
…file Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>
browniebroke
left a comment
There was a problem hiding this comment.
Looks good, tested a bit, works well 👍
Description
I am proposing to use multi-stage builds for building the python part of Django.
This can be done by installing all required OS and python dependencies and then to just copy over those files to the run stage. This reduces the size of a Django image from 509MB to 306MB (without gulp) and 561MB to 360MB (with gulp). A nearly 35% reduction in size!
One more improvement that can be made is to also copy over system dependencies from the python build stage, but that would make the configuration brittle due to apt installing files and symlinks in different locations depending on the package being installed. And that is why they are again installed in the run stage.
Checklist:
tests/test_cookiecutter_generation.pyis updated accordingly (especially if adding or updating a template option)Rationale
Smaller images are highly desirable because they would greatly reduce the time of re-deployment and deployment of containers and would also save a lot of time and money especially bandwidth costs.
Fix #3050