-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Further reduce the size of the docker image #3972
Conversation
richvdh
commented
Sep 27, 2018
- get rid of the pip wheel cache
- get rid of /synapse (everything we need ends up in /usr/local/lib/python2.7/site-packages
* get rid of the pip wheel cache * get rid of /synapse (everything we need ends up in /usr/local/lib/python2.7/site-packages
This is all very well, but we end up with an image which is built on top of a layer which adds /synapse, and then we remove /synapse in the final layer. Is there a way to avoid the redundant layer? |
You can do it with the builder container pattern : https://docs.docker.com/develop/develop-images/multistage-build/ That way you build synapse then throw away the container(s) you built it in and just copy the required bits into a new container - it makes it a little easier than having that one multi-line RUN command to try to make a single image. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So - we can deploy this for a slight gain, if we remove the "rm -rf /synapse" as that's not doing anything useful to the size
Doing it properly will be about using the multi-stage builder to:
FROM python-alpine...
RUN apk install runtime_deps
RUN apk install build_deps # seperate layers, because we can re-use the layer with just the runtime deps below, speeding up builds I think.
pip install to /target/ # somehow, how do you do that in python
other bits here
FROM python-alpine
RUN apk install runtime_deps # this is ok as a separate layer
COPY --from = 0 /target
Then i think we get to re-use layers where we want them - runtime dependencies, and build dependencies, without affecting the end image size.
docker/Dockerfile
Outdated
setup.cfg \ | ||
setup.py \ | ||
synapse \ | ||
&& rm -rf /synapse \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we remove this line then this will reduce the size a little without doing deletes that aren't worthwhile.
This means we can get rid of a whole load of cruft which we don't need.
well, in for a penny, in for a pound. A semi-interesting exercise in learning how dockerfiles work. |