Class: Docker::Image

Inherits:
Object
  • Object
show all
Extended by:
Error, Multipart
Includes:
Error, Model, Multipart
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 Multipart

multipart_request

Methods included from Model

#create!, #created?, included, #initialize, #to_s

Class Method Details

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

Given a Dockerfile as a string, builds an Image.



88
89
90
91
92
93
94
95
96
# File 'lib/docker/image.rb', line 88

def build(commands, connection = Docker.connection)
  body = multipart_request(
    '/build',
    'Dockerfile',
    StringIO.new("#{commands}\n"),
    connection
  )
  new(:id => extract_id(body), :connection => connection)
end

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

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



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

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

#create_from_file(file, options = {}) ⇒ Object

Given a Docker export and optional Hash of options, creates a new Image.



69
70
71
72
73
74
# File 'lib/docker/image.rb', line 69

def create_from_file(file, options = {})
  File.open(file, 'r') do |f|
    read_chunked = lambda { f.read(Excon.defaults[:chunk_size]).to_s }
    self.create!(options, :request_block => read_chunked)
  end
end

#insert(query = {}) ⇒ Object

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/docker/image.rb', line 44

def insert(query = {})
  ensure_created!
  body = self.connection.post(
    :path    => "/images/#{self.id}/insert",
    :headers => { 'Content-Type' => 'text/plain',
                  'User-Agent' => "Docker-Client/1.2" },
    :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
    Docker::Image.new(:id => id[1], :connection => self.connection)
  end
end

#push(options = {}) ⇒ Object

Push the Image to the Docker registry.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/docker/image.rb', line 30

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

#removeObject

Remove the Image from the server.



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

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