We have two goals for a speedy build of a docker image:
- small image
- small diffs
I notice you're not yet using layers. You want to.
You start out with LTS Jammy Jellyfish as the base layer, good.
Although you might prefer an image with the Alpine distribution,
as they have gone to some trouble to pare its functionality
to the bone. If there's an apt package installed which you never use,
then copying its bits around just costs us more time.
After the base layer, we don't take advantage of any additional CoW layers.
Depending on your requirements, you may or may not have
to ask apt to update and upgrade on every CI/CD build.
Doing that daily or weekly might suffice.
On top of base, use FROM base as python to build a python layer that
includes interpreter plus pip-installed libraries.
Again, up-to-the-minute fresh libraries might not
be a requirement for every build.
Ideally we would see a stable hash for this layer
over multiple builds, indicating no work needed to be done.
On top of python, build an app layer
that includes the checked-out feature branch.
Our goal in all this is to minimize diffs
within a layer from one build to the next,
so the apt and pip steps can complete
within a couple seconds because there is
very little to do, few diffs since last time.
Use RUN date or other timing measurements
to help you assess whether a given stage is
doing more work than you believe it should
have to do.
Rapid docker builds are possible.