This class represents a Docker Container. It's important to note that nothing is cached so that the information is always up to date.
Return all of the Containers.
# File lib/docker/container.rb, line 128 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 a new Container.
# File lib/docker/container.rb, line 111 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
Return the container with specified ID
# File lib/docker/container.rb, line 121 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
Attach to a container's standard streams / logs.
# File lib/docker/container.rb, line 40 def attach(options = {}, &block) opts = { :stream => true, :stdout => true, :stderr => true }.merge(options) # Creates list to store stdout and stderr messages msgs = Docker::Messages.new connection.post( path_for(:attach), opts, :response_block => attach_for(block, msgs) ) [msgs.stdout_messages, msgs.stderr_messages] end
Create an Image from a Container's change.s
# File lib/docker/container.rb, line 55 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
# File lib/docker/container.rb, line 102 def copy(path, &block) connection.post(path_for(:copy), {}, :body => { "Resource" => path }.to_json, :response_block => block ) self end
Export the Container as a tar.
# File lib/docker/container.rb, line 34 def export(&block) connection.get(path_for(:export), {}, :response_block => block) self end
remove container
# File lib/docker/container.rb, line 96 def remove(options = {}) connection.delete("/containers/#{self.id}", options) nil end
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 25 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
Return a String representation of the Container.
# File lib/docker/container.rb, line 69 def to_s "Docker::Container { :id => #{self.id}, :connection => #{self.connection} }" end
Return a List of Hashes that represents the top running processes.
# File lib/docker/container.rb, line 6 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
Generated with the Darkfish Rdoc Generator 2.