Hi to all,
This is my first time posting here, so I’m sorry if this isn’t the relevant category for this post, but I couldn’t find one more suitable for it.
What I’m currently experiencing is a difficulty reducing the final docker image size for an application that I’m developing. With the multi-stage build system some issues are resolved around the “builder pattern”, but since my application is built with C++ on top of several open-source libraries (most of which don’t play nicely when trying to compile statically) I need to make sure that I use only what is needed in the final image for distribution.
Initially I was just downloading the source code repositories and archives and compiling the libraries, but the image size became too large, so I added commands to delete them and the image became smaller, but it can become even smaller if only the relevant files are in it.
Since for some libraries I need the latest version of them (which isn’t present in the apt-get repositories) I have to download them and have the dependency dev libraries from apt-get to be able to compile them. But once they are compiled, the dev libraries aren’t needed. Sure I could have a command to delete the dev libraries after that, but the way the layers work, it won’t result in a smaller image size, or if I have all commands on 1 line, I won’t be able to benefit from the caching mechanism.
What I would like to do is use a multi-stage build where the first build phase has all the development libraries and compiles all the needed opensource libraries. After that I have a build phase where only the runtime versions of the libraries are installed + the compiled libraries from the previous stage are copied over. Since some libraries generate tons of files, I can’t easily trace them to generate a relevant COPY command and since I don’t see a way to copy an entire layer (maybe if this feature was added this would’ve been possible - Proposal: add IMPORT/EXPORT commands to Dockerfile · Issue #32100 · moby/moby · GitHub) I don’t see what options I have in front of me to achieve this. A COPY of the entire library or include folders won’t work since it will copy the development libraries as well (if I’m not mistaken, or if the case is otherwise, please correct me). Maybe a linux command that is run with the RUN commands to note the new files in a text file and later on be used for copying from the previous build phase (which I don’t think is possible since copying from the previous build phase requires a docker command).
Any tips, comments and feedback would be welcome as I currently think I’ve reached a dead end, but would like to make my docker image as small as possible, since if some of the functionalities discussed above were present, this would be achievable.