Class: MinimalPipeline::Docker

Inherits:
Object
  • Object
show all
Defined in:
lib/minimal_pipeline/docker.rb

Overview

Here is an example of how to use this class to manage Docker containers.

“‘ docker = MinimalPipeline::Docker.new keystore = MinimalPipeline::Keystore.new

deploy_env = ENV docker_repo = keystore.retrieve(“#deploy_env_EXAMPLE_ECR_REPO”) docker_image = “#docker_repo/example:latest” docker.build_docker_image(docker_image, build_context: ‘containers/example’) docker.push_docker_image(docker_image) “‘

Instance Method Summary collapse

Constructor Details

#initializeDocker

Returns a new instance of Docker.



22
# File 'lib/minimal_pipeline/docker.rb', line 22

def initialize; end

Instance Method Details

#build_docker_image(image_id, build_context: '.', dockerfile: 'Dockerfile', build_args: {}, timeout: 600, **options) ⇒ Object

Builds a docker image from a Dockerfile

For a full list of options, see docs.docker.com/engine/api/v1.37/#operation/ImageBuild

rubocop:disable Metrics/ParameterLists

Parameters:

  • image_id (String)

    The name of the docker image

  • build_context (String) (defaults to: '.')

    The build context for Dockerfile

  • dockerfile (String) (defaults to: 'Dockerfile')

    The path to Dockerfile

  • build_args (Hash) (defaults to: {})

    Additional build args to pass to Docker

  • timeout (Integer) (defaults to: 600)

    The Docker build timeout

  • options

    Additional options to pass to docker API



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/minimal_pipeline/docker.rb', line 74

def build_docker_image(image_id, build_context: '.',
                       dockerfile: 'Dockerfile', build_args: {},
                       timeout: 600, **options)

  build_args_json = generate_build_args_json(build_args)

  args = populate_args_hash(image_id, build_args_json, dockerfile)
  options.each { |key, value| args[key] = value }

  puts "Build args: #{args.inspect}" if ENV['DEBUG']
  build_image(build_context, args, timeout)
end

#build_output(build_output) ⇒ Object

Outputs JSON build output lines as human readible text

Parameters:

  • build_output (String)

    Raw JSON build output line



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/minimal_pipeline/docker.rb', line 42

def build_output(build_output)
  # Each line of the response is its own JSON structure
  build_output.each_line do |l|
    if (log = JSON.parse(l)) && log.key?('stream')
      $stdout.puts log['stream']
    end
  end
rescue JSON::ParserError
  $stdout.puts "Bad JSON parse\n"
  $stdout.puts build_output
end

#clean_up_image(image_id) ⇒ Object

Cleans up docker images

Parameters:

  • image_id (String)

    The Docker container ID to delete



57
58
59
60
# File 'lib/minimal_pipeline/docker.rb', line 57

def clean_up_image(image_id)
  image = ::Docker::Image.get(image_id)
  image.remove(force: true)
end

#push_docker_image(image_id) ⇒ Object

Pushes a docker image from local to AWS ECR. This handles login, the upload, and local cleanup of the container

Parameters:

  • image_id (String)

    The name of the docker image



92
93
94
95
96
97
98
99
# File 'lib/minimal_pipeline/docker.rb', line 92

def push_docker_image(image_id)
  docker_bin = which('docker')
  raise "docker_push: no docker binary: #{image_id}" unless docker_bin
  stdout, stderr, status = Open3.capture3(docker_bin, 'push', image_id)
  raise "stdout: #{stdout}\nstderr: #{stderr}\nstatus: #{status}" \
    unless status.exitstatus.zero?
  clean_up_image(image_id)
end

#which(cmd) ⇒ String

Finds the absolute path to a given executable

Parameters:

  • cmd (String)

    The name of the executable to locate

Returns:

  • (String)

    The absolute path to the executable



28
29
30
31
32
33
34
35
36
37
# File 'lib/minimal_pipeline/docker.rb', line 28

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end