Skip to content

Instantly share code, notes, and snippets.

@bobzoller
Forked from bcardiff/list-deps.cr
Created March 6, 2018 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobzoller/651cc4d13244ef10a37a4bb2da318376 to your computer and use it in GitHub Desktop.
Save bobzoller/651cc4d13244ef10a37a4bb2da318376 to your computer and use it in GitHub Desktop.
List binary dependencies to build a minimal docker image from scratch
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