Class: Indocker::Docker

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

Class Method Summary collapse

Class Method Details

.build(image, build_args = '') ⇒ Object



5
6
7
# 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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



46
47
48
49
50
51
52
53
# 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) ⇒ Object



66
67
68
69
70
71
# 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) ⇒ Object



17
18
19
# File 'lib/indocker/docker.rb', line 17

def pull(url)
  Indocker::Shell.command("docker pull #{url}", Indocker.logger)
end

.push(tag) ⇒ Object



13
14
15
# 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) ⇒ Object



26
27
28
# 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) ⇒ Object



42
43
44
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



21
22
23
24
# 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) ⇒ Object



9
10
11
# File 'lib/indocker/docker.rb', line 9

def tag(image, tag)
  Indocker::Shell.command_with_result("docker tag #{image} #{tag}", Indocker.logger)
end