Sharing here some insights I got while working on reducing size of the image
Docker images Copy-On-Write based, and new layer created for each RUN command. So, that means clean up step must be in the same RUN as the main operationWrong:
RUN apt-get install my-package # will add a package and also cache to layer
RUN apt-get clean # will remove cache from resulting image, but will not reduce the size
Right:
RUN apt-get install my-package && apt-get clean # will add only package itself
docker history command can be used to show a detailed breakdown of the size of layers.
When there is a lot going on in dockerfile grouping all to singe RUN reduces readability a lot. To help with that you can split commands over several RUNs and perform cleanup after each of them.
# installing build dependencies
RUN apt-get install build_dep1 build_dep2 && apt-get clean
# installing runtime dependencies
RUN apt-get install runtime_dep1 runtime_dep2 && apt-get clean
Or use inline comments in single RUN.
RUN apt-get install \
`# installing build dependencies` \
build_dep1 `# required for ...` && \
build_dep2 `# also required for build` && \
`# installing runtime dependecies` \
runtime_dep1 `# needed for this` \
runtime_dep2 `# needed for that` && \
apt-get clean `# clean up`
Note that I commented why each dependency is required. This will help a lot when it's time to migrate to a new base image. Like update from ubuntu 16 to ubuntu 18
Комментариев нет:
Отправить комментарий