- 
      
- 
        Save diegogub/9765e351b0c52dcfb874a3bbd29b932d 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