Class: Docker::Image

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

Overview

This class represents a Docker Image.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, id = nil) ⇒ Image

The private new method accepts a connection and optional id.



8
9
10
11
12
13
14
# File 'lib/docker/image.rb', line 8

def initialize(connection, id = nil)
  if connection.is_a?(Docker::Connection)
    @connection, @id = connection, id
  else
    raise ArgumentError, "Expected a Docker::Connection, got: #{connection}."
  end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/docker/image.rb', line 5

def connection
  @connection
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/docker/image.rb', line 5

def id
  @id
end

Class Method Details

.all(opts = {}, conn = Docker.connection) ⇒ Object

Return every Image.



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

def all(opts = {}, conn = Docker.connection)
  hashes = Docker::Util.parse_json(conn.get('/images/json', opts)) || []
  hashes.map { |hash| new(conn, hash['Id']) }
end

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

Given a Dockerfile as a string, builds an Image.



125
126
127
128
129
130
131
# File 'lib/docker/image.rb', line 125

def build(commands, connection = Docker.connection)
  body = connection.post(
    '/build', {},
    :body => Docker::Util.create_tar('Dockerfile' => commands)
  )
  new(connection, Docker::Util.extract_id(body))
end

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

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



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/docker/image.rb', line 134

def build_from_dir(dir, connection = Docker.connection)
  tar = Docker::Util.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(connection, Docker::Util.extract_id(body))
ensure
  tar.close unless tar.nil?
end

.create(opts = {}, conn = Docker.connection) ⇒ Object

Create a new Image.



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

def create(opts = {}, conn = Docker.connection)
  instance = new(conn)
  conn.post('/images/create', opts)
  id = opts['repo'] ? "#{opts['repo']}/#{opts['tag']}" : opts['fromImage']
  if (instance.id = id).nil?
    raise UnexpectedResponseError, 'Create response did not contain an Id'
  else
    instance
  end
end

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

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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/docker/image.rb', line 112

def import(file, options = {}, connection = Docker.connection)
  File.open(file, 'r') do |io|
    body = connection.post(
      '/images/create',
       options.merge('fromSrc' => '-'),
       :headers => { 'Content-Type' => 'application/tar',
                     'Transfer-Encoding' => 'chunked' }
    ) { io.read(Excon.defaults[:chunk_size]).to_s }
    new(connection, Docker::Util.parse_json(body)['status'])
  end
end

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

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



105
106
107
108
109
# File 'lib/docker/image.rb', line 105

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

Instance Method Details

#insert(query = {}) ⇒ Object

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



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

def insert(query = {})
  body = connection.post(path_for(: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, connection, id[1])
  end
end

#insert_local(opts = {}) ⇒ Object

Given a path of a local file and the path it should be inserted, creates a new Image that has that file.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/docker/image.rb', line 48

def insert_local(opts = {})
  local_path = opts.delete('localPath')
  output_path = opts.delete('outputPath')
  if File.exist?(local_path)
    basename = File.basename(local_path)
    tar = Docker::Util.create_tar(
      basename => File.read(local_path),
      'Dockerfile' => "from #{self.id}\nadd #{basename} #{output_path}"
    )
    body = connection.post('/build', {}, :body => tar)
    self.class.send(:new, connection, Docker::Util.extract_id(body))
  else
    raise ArgumentError, "#{local_path} does not exist."
  end
end

#push(options = {}) ⇒ Object

Push the Image to the Docker registry.



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

def push(options = {})
  connection.post(path_for(:push), options, :body => Docker.creds)
  self
end

#removeObject

Remove the Image from the server.



65
66
67
# File 'lib/docker/image.rb', line 65

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.



19
20
21
22
23
# File 'lib/docker/image.rb', line 19

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

#tag(opts = {}) ⇒ Object

Tag the Image.



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

def tag(opts = {})
  Docker::Util.parse_json(connection.post(path_for(:tag), opts))
end

#to_sObject

Return a String representation of the Image.



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

def to_s
  "Docker::Image { :id => #{self.id}, :connection => #{self.connection} }"
end