class Indocker::Docker

Public Class Methods

build(image, build_args = '') click to toggle source
# File lib/indocker/docker.rb, line 5
def build(image, build_args = '')
  Indocker::Shell.command_with_result("docker build #{build_args} --rm=true -t #{image} .", Indocker.logger)
end
container_id_by_name(container_name, only_healthy: false) click to toggle source
# File lib/indocker/docker.rb, line 73
def container_id_by_name(container_name, only_healthy: false)
  health_args = if only_healthy
    '--filter="health=healthy"'
  end

  command = "docker ps -a #{health_args} --filter=\"status=running\" --filter \"name=#{container_name}$\" -q"

  id = nil

  res = Indocker::Shell.command_with_result(command, Indocker.logger)

  res.stdout.empty? ? nil : res.stdout
end
create_network(name) click to toggle source
# File lib/indocker/docker.rb, line 55
def create_network(name)
  network_exist = false

  res = Indocker::Shell.command_with_result("docker network ls --filter \"name=^#{name}$\" --format \"{{.Name}}\"", Indocker.logger)
  network_exist = !res.stdout.empty?

  if !network_exist
    Indocker::Shell.command("docker network create #{name}", Indocker.logger, skip_errors: true)
  end
end
create_volume(name) click to toggle source
# File lib/indocker/docker.rb, line 46
def create_volume(name)
  res = Indocker::Shell.command_with_result("docker volume ls --filter \"name=^#{name}$\" --format \"{{.Name}}\"", Indocker.logger)
  volume_exist = !res.stdout.empty?

  if !volume_exist
    Indocker::Shell.command("docker volume create #{name}", Indocker.logger, skip_errors: true)
  end
end
image_id(image_url) click to toggle source
# File lib/indocker/docker.rb, line 66
def image_id(image_url)
  command = "docker image inspect #{image_url} --format \"{{.Id}}\""

  res = Indocker::Shell.command_with_result(command, Indocker.logger)
  res.stdout
end
pull(url) click to toggle source
# File lib/indocker/docker.rb, line 17
def pull(url)
  Indocker::Shell.command("docker pull #{url}", Indocker.logger)
end
push(tag) click to toggle source
# File lib/indocker/docker.rb, line 13
def push(tag)
  Indocker::Shell.command("docker push #{tag}", Indocker.logger)
end
rm(container_name, skip_errors: false) click to toggle source
# File lib/indocker/docker.rb, line 26
def rm(container_name, skip_errors: false)
  Indocker::Shell.command("docker rm -fv #{container_name}", Indocker.logger, skip_errors: skip_errors)
end
run(image, args_list, command, service_args) click to toggle source
# File lib/indocker/docker.rb, line 42
def run(image, args_list, command,  service_args)
  Indocker::Shell.command(run_command(image, args_list, command, service_args), Indocker.logger)
end
run_command(image, args_list, command, service_args) click to toggle source
# File lib/indocker/docker.rb, line 30
def run_command(image, args_list, command, service_args)
  extra_args = ""

  if service_args && service_args.is_a?(Hash)
    service_args.each do |arg, val|
      extra_args += " #{arg} #{val}"
    end
  end

  "docker run #{args_list} #{image} #{command} #{extra_args}"
end
stop(container_name, time = 10, skip_errors: false) click to toggle source
# File lib/indocker/docker.rb, line 21
def stop(container_name, time = 10, skip_errors: false)
  Indocker::Shell.command("docker stop --time=#{time} #{container_name}", Indocker.logger, skip_errors: skip_errors)
  rm(container_name, skip_errors: skip_errors)
end
tag(image, tag) click to toggle source
# File lib/indocker/docker.rb, line 9
def tag(image, tag)
  Indocker::Shell.command_with_result("docker tag #{image} #{tag}", Indocker.logger)
end