Using docker to build binaries can cause the docker image to be really large as it includes all the source code, transient build files, linkers, etc. For production it is recommended to us docker multi-stage builds to produce a container that only has the binary and needed libraries to run.
It is easy if the binary produced has all the needed libraries statically linked. But.. what if there are dynamically linked libraries? How do we find them?
ldd
ldd
would provide a list of dynamically linked libraries given a binary. However, it doesn't do multiple binaries at once. Hence pipes to the rescue.
find * -type f -perm /a+x -exec ldd {} \; \
| grep so \
| sed -e '/^[^\t]/ d' \
| sed -e 's/\t//' \
| sed -e 's/.*=..//' \
| sed -e 's/ (0.*)//' \
| sort \
| uniq -c \
| sort -n
The following will give us a list of dynamically linked libraries for all binaries found in the current folder.
Now we just have to copy the list of libraries with the binaries to a scratch docker image. #Win