Class: Docker::Image

Inherits:
Object
  • Object
show all
Extended by:
Error
Includes:
Error, Model
Defined in:
lib/docker/image.rb

Overview

This class represents a Docker Image.

Instance Attribute Summary

Attributes included from Model

#connection, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

included, #initialize, #to_s

Class Method Details

.build(commands, connection = Docker.connection) ⇒ Object

Given a Dockerfile as a string, builds an Image.



98
99
100
101
102
103
104
105
# File 'lib/docker/image.rb', line 98

def build(commands, connection = Docker.connection)
  body = connection.post(
    :path => '/build',
    :body => create_tar(commands),
    :expects => (200..204)
  ).body
  new(:id => extract_id(body), :connection => connection)
end

.build_from_dir(dir, connection = Docker.connection) ⇒ Object

Given a directory that contains a Dockerfile, builds an Image.



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/docker/image.rb', line 108

def build_from_dir(dir, connection = Docker.connection)
  tar = create_dir_tar(dir)
  body = connection.post(
    :path          => '/build',
    :headers       => { 'Content-Type'      => 'application/tar',
                        'Transfer-Encoding' => 'chunked' },
    :request_block => proc { tar.read(Excon.defaults[:chunk_size]).to_s },
    :expects       => (200..204),
  ).body
  new(:id => extract_id(body), :connection => connection)
ensure
  tar.close
end

.import(file, options = {}, connection = Docker.connection) ⇒ Object

Import an Image from the output of Docker::Container#export.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/docker/image.rb', line 83

def import(file, options = {}, connection = Docker.connection)
  File.open(file, 'r') do |io|
    body = connection.post(
      :path          => '/images/create',
      :headers       => { 'User-Agent' => 'Docker-Client/0.4.6',
                          'Transfer-Encoding' => 'chunked' },
      :query         => options.merge('fromSrc' => '-'),
      :request_block => proc { io.read(Excon.defaults[:chunk_size]).to_s },
      :expects       => (200..204)
    ).body
    new(:id => JSON.parse(body)['status'], :connection => connection)
  end
end

.search(query = {}, connection = Docker.connection) ⇒ Object

Given a query like ‘{ :term => ’sshd’ }‘, queries the Docker Registry for a corresponiding Image.



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

def search(query = {}, connection = Docker.connection)
  hashes = connection.json_request(:get, '/images/search', query) || []
  hashes.map { |hash| new(:id => hash['Name'], :connection => connection) }
end

Instance Method Details

#insert(query = {}) ⇒ Object

Insert a file into the Image, returns a new Image that has that file.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/docker/image.rb', line 52

def insert(query = {})
  body = self.connection.post(
    :path    => "/images/#{self.id}/insert",
    :headers => { 'Content-Type' => 'text/plain',
                  'User-Agent' => "Docker-Client/0.4.6" },
    :query   => query,
    :expects => (200..204)
  ).body
  if (id = body.match(/{"Id":"([a-f0-9]+)"}\z/)).nil? || id[1].empty?
    raise UnexpectedResponseError, "Could not find Id in '#{body}'"
  else
    self.class.send(:new, :id => id[1], :connection => self.connection)
  end
end

#push(options = {}) ⇒ Object

Push the Image to the Docker registry.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/docker/image.rb', line 39

def push(options = {})
  self.connection.post(
    :path    => "/images/#{self.id}/push",
    :headers => { 'Content-Type' => 'text/plain',
                  'User-Agent' => 'Docker-Client/0.4.6' },
    :query   => options,
    :body    => Docker.creds,
    :expects => (200..204)
  )
  true
end

#removeObject

Remove the Image from the server.



68
69
70
# File 'lib/docker/image.rb', line 68

def remove
  self.connection.json_request(:delete, "/images/#{self.id}", nil)
end

#run(cmd) ⇒ Object

Given a command and optional list of streams to attach to, run a command on an Image. This will not modify the Image, but rather create a new Container to run the Image.



31
32
33
34
35
36
# File 'lib/docker/image.rb', line 31

def run(cmd)
  cmd = cmd.split(/\s+/) if cmd.is_a?(String)
  Docker::Container.create({ 'Image' => self.id, 'Cmd' => cmd },
                           self.connection)
                   .tap(&:start)
end