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.



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

def build(commands, connection = Docker.connection)
  body = connection.post('/build', {}, :body => create_tar(commands))
  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.



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

def build_from_dir(dir, connection = Docker.connection)
  tar = create_dir_tar(dir)
  body = connection.post(
    '/build', {},
    :headers => { 'Content-Type'      => 'application/tar',
                  'Transfer-Encoding' => 'chunked' }
  ) { tar.read(Excon.defaults[:chunk_size]).to_s }
  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.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/docker/image.rb', line 65

def import(file, options = {}, connection = Docker.connection)
  File.open(file, 'r') do |io|
    body = connection.post(
      '/images/create',
       options.merge('fromSrc' => '-'),
       :headers => { 'Transfer-Encoding' => 'chunked' }
    ) { io.read(Excon.defaults[:chunk_size]).to_s }
    new(:id => Docker::Util.parse_json(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.



58
59
60
61
62
# File 'lib/docker/image.rb', line 58

def search(query = {}, connection = Docker.connection)
  body = connection.get('/images/search', query)
  hashes = Docker::Util.parse_json(body) || []
  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.



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

def insert(query = {})
  body = connection.post("/images/#{self.id}/insert", query)
  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.



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

def push(options = {})
  connection.post("/images/#{self.id}/push", options, :body => Docker.creds)
  true
end

#removeObject

Remove the Image from the server.



49
50
51
# File 'lib/docker/image.rb', line 49

def remove
  connection.delete("/images/#{self.id}")
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.



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

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