Parent

Included Modules

Docker::Container

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

Public Class Methods

all(opts = {}, conn = Docker.connection) click to toggle source

Return all of the Containers.

# File lib/docker/container.rb, line 186
def self.all(opts = {}, conn = Docker.connection)
  hashes = Docker::Util.parse_json(conn.get('/containers/json', opts)) || []
  hashes.map { |hash| new(conn, hash) }
end
create(opts = {}, conn = Docker.connection) click to toggle source

Create a new Container.

# File lib/docker/container.rb, line 169
def self.create(opts = {}, conn = Docker.connection)
  name = opts.delete('name')
  query = {}
  query['name'] = name if name
  resp = conn.post('/containers/create', query, :body => opts.to_json)
  hash = Docker::Util.parse_json(resp) || {}
  new(conn, hash)
end
get(id, opts = {}, conn = Docker.connection) click to toggle source

Return the container with specified ID

# File lib/docker/container.rb, line 179
def self.get(id, opts = {}, conn = Docker.connection)
  container_json = conn.get("/containers/#{URI.encode(id)}/json", opts)
  hash = Docker::Util.parse_json(container_json) || {}
  new(conn, hash)
end

Public Instance Methods

attach(options = {}, &block) click to toggle source

Attach to a container's standard streams / logs.

# File lib/docker/container.rb, line 42
def attach(options = {}, &block)
  stdin = options.delete(:stdin)
  tty   = options.delete(:tty)

  opts = {
    :stream => true, :stdout => true, :stderr => true
  }.merge(options)
  # Creates list to store stdout and stderr messages
  msgs = Docker::Messages.new

  excon_params = {}

  if stdin
    # If attaching to stdin, we must hijack the underlying TCP connection
    # so we can stream stdin to the remote Docker process
    opts[:stdin] = true
    excon_params[:hijack_block] = hijack_for(stdin, block, msgs, tty)
  else
    excon_params[:response_block] = attach_for(block, msgs, tty)
  end

  connection.post(
    path_for(:attach),
    opts,
    excon_params
  )
  [msgs.stdout_messages, msgs.stderr_messages]
end
commit(options = {}) click to toggle source

Create an Image from a Container's change.s

# File lib/docker/container.rb, line 72
def commit(options = {})
  options.merge!('container' => self.id[0..7])
  # [code](https://github.com/dotcloud/docker/blob/v0.6.3/commands.go#L1115)
  # Based on the link, the config passed as run, needs to be passed as the
  # body of the post so capture it, remove from the options, and pass it via
  # the post body
  config = options.delete('run')
  hash = Docker::Util.parse_json(connection.post('/commit',
                                                 options,
                                                 :body => config.to_json))
  Docker::Image.send(:new, self.connection, hash)
end
copy(path, &block) click to toggle source
# File lib/docker/container.rb, line 160
def copy(path, &block)
  connection.post(path_for(:copy), {},
    :body => { "Resource" => path }.to_json,
    :response_block => block
  )
  self
end
delete(options = {}) click to toggle source
Alias for: remove
export(&block) click to toggle source

Export the Container as a tar.

# File lib/docker/container.rb, line 36
def export(&block)
  connection.get(path_for(:export), {}, :response_block => block)
  self
end
hijack_for(stdin, block, msg_stack, tty) click to toggle source
# File lib/docker/container.rb, line 196
def hijack_for(stdin, block, msg_stack, tty)
  attach_block = attach_for(block, msg_stack, tty)

  lambda do |socket|
    debug "hijack: hijacking the HTTP socket"
    threads = []

    debug "hijack: starting stdin copy thread"
    threads << Thread.start do
      debug "hijack: copying stdin => socket"
      IO.copy_stream stdin, socket

      debug "hijack: closing write end of hijacked socket"
      socket.close_write
    end

    debug "hijack: starting hijacked socket read thread"
    threads << Thread.start do
      debug "hijack: reading from hijacked socket"

      begin
        while chunk = socket.readpartial(512)
          debug "hijack: got #{chunk.bytesize} bytes from hijacked socket"
          attach_block.call chunk, nil, nil
        end
      rescue EOFError
      end

      debug "hijack: killing stdin copy thread"
      threads.first.kill
    end

    threads.each(&:join)
  end
end
kill!(opts = {}) click to toggle source
# File lib/docker/container.rb, line 107
def kill!(opts = {})
  connection.post(path_for(:kill), opts)
  self
end
logs(opts = {}) click to toggle source
# File lib/docker/container.rb, line 98
def logs(opts = {})
  connection.get(path_for(:logs), opts)
end
remove(options = {}) click to toggle source

remove container

# File lib/docker/container.rb, line 139
def remove(options = {})
  connection.delete("/containers/#{self.id}", options)
  nil
end
Also aliased as: delete
run(cmd, time = 1000) click to toggle source

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.

# File lib/docker/container.rb, line 27
def run(cmd, time = 1000)
  if (code = tap(&:start).wait(time)['StatusCode']).zero?
    commit.run(cmd)
  else
    raise UnexpectedResponseError, "Command returned status code #{code}."
  end
end
start!(opts = {}) click to toggle source
# File lib/docker/container.rb, line 102
def start!(opts = {})
  connection.post(path_for(:start), {}, :body => opts.to_json)
  self
end
to_s() click to toggle source

Return a String representation of the Container.

# File lib/docker/container.rb, line 86
def to_s
  "Docker::Container { :id => #{self.id}, :connection => #{self.connection} }"
end
top(opts = {}) click to toggle source

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

# File lib/docker/container.rb, line 7
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 = nil) click to toggle source

Wait for the current command to finish executing. Default wait time is `Excon.options`.

# File lib/docker/container.rb, line 18
def wait(time = nil)
  resp = connection.post(path_for(:wait), nil, :read_timeout => time)
  Docker::Util.parse_json(resp)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.