Last active
May 29, 2024 17:37
-
-
Save bcardiff/85ae47e66ff0df35a78697508fcb49af to your computer and use it in GitHub Desktop.
List binary dependencies to build a minimal docker image from scratch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless ARGV.size > 0 | |
puts " Missing executable file argument" | |
puts " Usage (in a Dockerfile)" | |
puts " RUN crystal run ./path/to/list-deps.cr -- ./bin/executable" | |
exit 1 | |
end | |
executable = File.expand_path(ARGV[0]) | |
unless File.exists?(executable) | |
puts " Unable to find #{executable}" | |
exit 1 | |
end | |
puts " Extracting libraries for #{executable} ..." | |
deps = [] of String | |
output = `ldd #{executable}`.scan(/(\/.*)\s\(/) do |m| | |
library = m[1] | |
deps << library | |
real_lib = File.real_path(library) | |
deps << real_lib if real_lib != library | |
end | |
puts " Generating Dockerfile" | |
puts | |
puts "=" * 30 | |
puts "FROM scratch" | |
deps.each do |dep| | |
puts "COPY --from=0 #{dep} #{dep}" | |
end | |
puts "COPY --from=0 #{executable} /#{File.basename(executable)}" | |
puts "ENTRYPOINT [\"/#{File.basename(executable)}\"]" | |
puts "=" * 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Had issues with
scratch
using 0.31.0. I think its because it cant finduname
:However, changing it
alpine
worked.