Class: Docker::Container

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/docker/container.rb

Overview

This class represents a Docker Container. It’s important to note that nothing is cached so that the information is always up to date.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, id = nil) ⇒ Container

The private new method accepts a connection and optional id.



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

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.



6
7
8
# File 'lib/docker/container.rb', line 6

def connection
  @connection
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/docker/container.rb', line 6

def id
  @id
end

Class Method Details

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

Return all of the Containers.



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

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

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

Create a new Container.



106
107
108
109
110
111
112
113
114
# File 'lib/docker/container.rb', line 106

def self.create(opts = {}, conn = Docker.connection)
  instance = new(conn)
  resp = conn.post('/containers/create', {}, :body => opts.to_json)
  if (instance.id = Docker::Util.parse_json(resp)['Id']).nil?
    raise UnexpectedResponseError, 'Create response did not contain an Id'
  else
    instance
  end
end

Instance Method Details

#attach(options = {}, &block) ⇒ Object

Attach to a container’s standard streams / logs.



52
53
54
55
# File 'lib/docker/container.rb', line 52

def attach(options = {}, &block)
  opts = { :stream => true, :stdout => true }.merge(options)
  connection.post(path_for(:attach), opts, :response_block => block)
end

#commit(options = {}) ⇒ Object

Create an Image from a Container’s change.s



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

def commit(options = {})
  options.merge!('container' => self.id[0..7])
  hash = Docker::Util.parse_json(connection.post('/commit', options))
  Docker::Image.send(:new, self.connection, hash['Id'])
end

#copy(path, &block) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/docker/container.rb', line 97

def copy(path, &block)
  connection.post(path_for(:copy), {},
    :body => { "Resource" => path }.to_json,
    :response_block => block
  )
  self
end

#delete(options = {}) ⇒ Object

delete container



92
93
94
95
# File 'lib/docker/container.rb', line 92

def delete(options = {})
  connection.delete("/containers/#{self.id}", options)
  nil
end

#export(&block) ⇒ Object

Export the Container as a tar.



46
47
48
49
# File 'lib/docker/container.rb', line 46

def export(&block)
  connection.get(path_for(:export), {}, :response_block => block)
  self
end

#run(cmd, time = 1000) ⇒ Object

Given a command and an optional number of seconds to wait for the currently executing command, creates a new Container to run the specified command. If the command that is currently executing does not return a 0 status code, an UnexpectedResponseError is raised.



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

def run(cmd, time = 1000)
  if (code = tap(&:start).wait(time)['StatusCode']).zero?
    commit.run(cmd).tap(&:start)
  else
    raise UnexpectedResponseError, "Command returned status code #{code}."
  end
end

#to_sObject

Return a String represntation of the Container.



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

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

#top(opts = {}) ⇒ Object

Return a List of Hashes that represents the top running processes.



18
19
20
21
22
23
24
25
# File 'lib/docker/container.rb', line 18

def top(opts = {})
  resp = Docker::Util.parse_json(connection.get(path_for(:top), opts))
  if resp['Processes'].nil?
    []
  else
    resp['Processes'].map { |ary| Hash[resp['Titles'].zip(ary)] }
  end
end

#wait(time = 60) ⇒ Object

Wait for the current command to finish executing.



28
29
30
31
# File 'lib/docker/container.rb', line 28

def wait(time = 60)
  resp = connection.post(path_for(:wait), nil, :read_timeout => time)
  Docker::Util.parse_json(resp)
end