Class: Dockistrano::Docker

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

Overview

Class for communication with Docker. Uses two means of communication:

  • Actions on containers are executed by calling the docker binary with an ip address of the Docker location. The docker command line client is best capable of executing the actions for building images, running containers and managing containers.

  • Queries are executed using the HTTP API of docker. This API is exposed via HTTP and returns JSON. This allows us to easily parse and return the information.

This class uses two environment variables:

DOCKER_BINARY  - Location of the docker binary on your system
DOCKER_HOST_IP - IP address or host of the docker server

Defined Under Namespace

Classes: EnvironmentVariableMissing, ImageNotFound, ResourceNotFound

Class Method Summary collapse

Class Method Details

.attach(id) ⇒ Object



94
95
96
# File 'lib/dockistrano/docker.rb', line 94

def self.attach(id)
  execute(["attach", id], :stream)
end

.build(full_image_name) ⇒ Object



78
79
80
# File 'lib/dockistrano/docker.rb', line 78

def self.build(full_image_name)
  execute(["build", { t: full_image_name }, "."], :stream)
end

.cleanObject



98
99
100
101
# File 'lib/dockistrano/docker.rb', line 98

def self.clean
  Dockistrano::CommandLine.command_with_stream("#{docker_command} rmi $(#{docker_command} images -a | grep \"^<none>\" | awk '{print $3}')")
  Dockistrano::CommandLine.command_with_stream("#{docker_command} rm $(#{docker_command} ps -a -q)")
end

.console(full_image_name, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/dockistrano/docker.rb', line 68

def self.console(full_image_name, options={})
  options["t"] = true
  options["i"] = true
  if (command = options.delete(:command))
    execute(["run", options, full_image_name, command], :interaction)
  else
    execute(["run", options, full_image_name], :interaction)
  end
end

.docker_commandObject

Returns the docker command as a string: ‘docker -H 127.0.0.1’



26
27
28
29
30
# File 'lib/dockistrano/docker.rb', line 26

def self.docker_command
  raise EnvironmentVariableMissing.new("Missing DOCKER_BINARY in environment, please provide the location of the docker binary") unless ENV["DOCKER_BINARY"]
  raise EnvironmentVariableMissing.new("Missing DOCKER_HOST_IP in environment, please provide the host or ip address of the docker server") unless ENV["DOCKER_HOST_IP"]
  "#{ENV['DOCKER_BINARY']} -H #{ENV['DOCKER_HOST_IP']}"
end

.exec(full_image_name, options = {}) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/dockistrano/docker.rb', line 60

def self.exec(full_image_name, options={})
  if (command = options.delete(:command))
    execute(["run", options, full_image_name, command], :stream)
  else
    execute(["run", options, full_image_name], :stream)
  end
end

.execute(command, mode = :string_result) ⇒ Object

Executes the given command on the command line



33
34
35
36
37
38
39
40
41
42
# File 'lib/dockistrano/docker.rb', line 33

def self.execute(command, mode=:string_result)
  case mode
  when :string_result
    Dockistrano::CommandLine.command_with_result("#{docker_command} #{command.collect { |c| c.kind_of?(String) ? c : arguments(c) }.join(" ")}".strip)
  when :stream
    Dockistrano::CommandLine.command_with_stream("#{docker_command} #{command.collect { |c| c.kind_of?(String) ? c : arguments(c) }.join(" ")}".strip)
  else
    Dockistrano::CommandLine.command_with_interaction("#{docker_command} #{command.collect { |c| c.kind_of?(String) ? c : arguments(c) }.join(" ")}".strip)
  end
end

.image_id(full_image_name) ⇒ Object



118
119
120
# File 'lib/dockistrano/docker.rb', line 118

def self.image_id(full_image_name)
  inspect_image(full_image_name)["id"]
end

.inspect_container(id) ⇒ Object



131
132
133
# File 'lib/dockistrano/docker.rb', line 131

def self.inspect_container(id)
  request(["containers", id, "json"])
end

.inspect_image(full_image_name) ⇒ Object



125
126
127
128
129
# File 'lib/dockistrano/docker.rb', line 125

def self.inspect_image(full_image_name)
  response = request(["images", full_image_name, "json"])
rescue ResourceNotFound => e
  raise ImageNotFound.new(e.message)
end

.last_run_container_id(full_image_name) ⇒ Object

Returns the id of the last container with an error



111
112
113
114
115
116
# File 'lib/dockistrano/docker.rb', line 111

def self.last_run_container_id(full_image_name)
  request(["containers", "json?all=1"]).each do |container|
    return container["Id"] if container["Image"] == full_image_name and container["Command"] != "cat /dockistrano.yml"
  end
  nil
end

.logs(id) ⇒ Object



90
91
92
# File 'lib/dockistrano/docker.rb', line 90

def self.logs(id)
  execute(["logs", id], :stream)
end

.ps(options = {}) ⇒ Object



44
45
46
# File 'lib/dockistrano/docker.rb', line 44

def self.ps(options={})
  execute(["ps", options])
end

.pull(full_image_name, tag) ⇒ Object



82
83
84
# File 'lib/dockistrano/docker.rb', line 82

def self.pull(full_image_name, tag)
  execute(["pull", { t: tag }, full_image_name])
end

.push(image_name, tag) ⇒ Object



86
87
88
# File 'lib/dockistrano/docker.rb', line 86

def self.push(image_name, tag)
  execute(["push", image_name, tag], :stream)
end

.run(full_image_name, options = {}) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/dockistrano/docker.rb', line 52

def self.run(full_image_name, options={})
  if (command = options.delete(:command))
    execute(["run", options, full_image_name, command])
  else
    execute(["run", options, full_image_name])
  end
end

.running_container_id(full_image_name) ⇒ Object



103
104
105
106
107
108
# File 'lib/dockistrano/docker.rb', line 103

def self.running_container_id(full_image_name)
  request(["containers", "json"]).each do |container|
    return container["Id"] if container["Image"] == full_image_name
  end
  nil
end

.stop(id) ⇒ Object



48
49
50
# File 'lib/dockistrano/docker.rb', line 48

def self.stop(id)
  execute(["stop", id])
end

.stop_all_containers_from_image(full_image_name) ⇒ Object



135
136
137
138
139
140
# File 'lib/dockistrano/docker.rb', line 135

def self.stop_all_containers_from_image(full_image_name)
  containers = request(["containers", "json"])
  containers.each do |container|
    execute(["stop", container["Id"]]) if container["Image"] == full_image_name
  end
end

.tags_for_image(image_name) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/dockistrano/docker.rb', line 142

def self.tags_for_image(image_name)
  images = request(["images", "json"])
  [].tap do |tags|
    images.each do |image|
      tags << image["Tag"] if image["Repository"] == image_name
    end
  end
end