Module: Kontena::Cli::Apps::DockerHelper
- Included in:
- BuildCommand, DeployCommand
- Defined in:
- lib/kontena/cli/apps/docker_helper.rb
Instance Method Summary collapse
- #build_docker_image(service, no_cache = false) ⇒ Integer
- #dockerfile_exist?(path, dockerfile) ⇒ Boolean
- #image_exist?(image) ⇒ Boolean
- #process_docker_images(services, force_build = false, no_cache = false) ⇒ Object
- #push_docker_image(image) ⇒ Integer
- #run_pre_build_hook(hook) ⇒ Object
- #validate_image_name(name) ⇒ Boolean
Instance Method Details
#build_docker_image(service, no_cache = false) ⇒ Integer
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/kontena/cli/apps/docker_helper.rb', line 34 def build_docker_image(service, no_cache = false) dockerfile = dockerfile = service['build']['dockerfile'] || 'Dockerfile' build_context = service['build']['context'] cmd = ['docker', 'build', '-t', service['image']] cmd << ['-f', File.join(File.(build_context), dockerfile)] if dockerfile != "Dockerfile" cmd << '--no-cache' if no_cache args = service['build']['args'] || {} args.each do |k, v| cmd << "--build-arg=#{k}=#{v}" end cmd << build_context ret = system(*cmd.flatten) raise ("Failed to build image #{service['image'].colorize(:cyan)}") unless ret ret end |
#dockerfile_exist?(path, dockerfile) ⇒ Boolean
67 68 69 70 |
# File 'lib/kontena/cli/apps/docker_helper.rb', line 67 def dockerfile_exist?(path, dockerfile) file = File.join(File.(path), dockerfile) File.exist?(file) end |
#image_exist?(image) ⇒ Boolean
60 61 62 |
# File 'lib/kontena/cli/apps/docker_helper.rb', line 60 def image_exist?(image) system("docker history '#{image}' >/dev/null 2>/dev/null") end |
#process_docker_images(services, force_build = false, no_cache = false) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kontena/cli/apps/docker_helper.rb', line 7 def process_docker_images(services, force_build = false, no_cache = false) services.each do |name, service| if service['build'] && (!image_exist?(service['image']) || force_build) dockerfile = service['build']['dockerfile'] || 'Dockerfile' raise ("'#{service['image']}' is not valid Docker image name") unless validate_image_name(service['image']) raise ("'#{service['build']['context']}' does not have #{dockerfile}") unless dockerfile_exist?(service['build']['context'], dockerfile) if service['hooks'] && service['hooks']['pre_build'] puts "Running pre_build hook".colorize(:cyan) run_pre_build_hook(service['hooks']['pre_build']) end puts "Building image #{service['image'].colorize(:cyan)}" build_docker_image(service, no_cache) puts "Pushing image #{service['image'].colorize(:cyan)} to registry" push_docker_image(service['image']) end end end |
#push_docker_image(image) ⇒ Integer
52 53 54 55 56 |
# File 'lib/kontena/cli/apps/docker_helper.rb', line 52 def push_docker_image(image) ret = system('docker', 'push', image) raise ("Failed to push image #{image.colorize(:cyan)}") unless ret ret end |
#run_pre_build_hook(hook) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/kontena/cli/apps/docker_helper.rb', line 73 def run_pre_build_hook(hook) hook.each do |h| ret = system(h['cmd']) raise ("Failed to run pre_build hook: #{h['name']}!") unless ret end end |
#validate_image_name(name) ⇒ Boolean
27 28 29 |
# File 'lib/kontena/cli/apps/docker_helper.rb', line 27 def validate_image_name(name) !(/^[\w.\/\-:]+:?+[\w+.]+$/ =~ name).nil? end |