Class: Docker::Container

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

Attributes included from Model

#connection, #id

Instance Method Summary collapse

Methods included from Model

included, #initialize, #to_s

Instance Method Details

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

Attach to a container’s standard streams / logs.



68
69
70
71
72
# File 'lib/docker/container.rb', line 68

def attach(options = {}, &block)
  options = { :stream => true, :stdout => true }.merge(options)
  connection.post("/containers/#{id}/attach", options,
                  :response_block => block)
end

#commit(options = {}) ⇒ Object

Create an Image from a Container’s change.s



75
76
77
78
79
# File 'lib/docker/container.rb', line 75

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

#export(&block) ⇒ Object

Export the Container as a tar.



62
63
64
65
# File 'lib/docker/container.rb', line 62

def export(&block)
  connection.get("/containers/#{id}/export", nil, :response_block => block)
  true
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.



53
54
55
56
57
58
59
# File 'lib/docker/container.rb', line 53

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

#top(opts = {}) ⇒ Object

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



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

def top(opts = {})
  resp = connection.get("/containers/#{id}/top", opts)
  hash = Docker::Util.parse_json(resp)
  hash['Processes'].map { |ary| Hash[hash['Titles'].zip(ary)] }
end

#wait(time = 60) ⇒ Object

Wait for the current command to finish executing.



44
45
46
47
# File 'lib/docker/container.rb', line 44

def wait(time = 60)
  resp = connection.post("/containers/#{id}/wait", nil, :read_timeout => time)
  Docker::Util.parse_json(resp)
end