class Docker::Resource::Container

Public Instance Methods

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

Valid options: stdout true default is false stderr true default is false

# File lib/docker/resource/container.rb, line 88
def attach(container_id, options = {}, timeout = nil, &block)
  raise(ArgumentError, "Block must be given to handle streamed data") if block.nil?
  options = {stdout: true, stderr: true} if options.empty?
  options = options.merge(stream: true, logs: false)
  
  response = @connection.stream("/containers/#{container_id}/attach", options, timeout, {}, &block)
  raise_if_container_not_found(response.status)
  raise(BadParameterError) if response.status == 400
  response
end
changes(container_id) click to toggle source
# File lib/docker/resource/container.rb, line 38
def changes(container_id)
  @connection.get("/containers/#{container_id}/changes").body_as_json
end
commit(container_id, repository, tag = nil, options = {}) click to toggle source

TODO default run configuration not supported yet

# File lib/docker/resource/container.rb, line 43
def commit(container_id, repository, tag = nil, options = {})
  options[:container] = container_id
  options[:repo] = repository
  options[:tag]  = tag if tag
  response = @connection.post("/commit", options)
  raise_if_container_not_found(response.status)
  response.body_as_json
end
create(command, image = 'base', options = {}) click to toggle source
# File lib/docker/resource/container.rb, line 19
def create(command, image = 'base', options = {})
  command = [command] if command.is_a?(String)
  body = {'Cmd' => command, 'Image' => image}
  body = options.merge(body)
  json_body = MultiJson.dump(body)
  
  response = @connection.post("/containers/create", {}, json_body, {'Content-Type' => 'application/json'})
  raise(Docker::Error::NotFoundError) if response.status == 404
  response.body_as_json
end
export() click to toggle source

Returns a stream

# File lib/docker/resource/container.rb, line 53
def export
  
end
kill(container_id) click to toggle source
# File lib/docker/resource/container.rb, line 79
def kill(container_id)
  status = @connection.post("/containers/#{container_id}/kill").status
  raise_if_container_not_found(status)
  status == 204
end
list(options = {}) click to toggle source

Options all limit since before

# File lib/docker/resource/container.rb, line 15
def list(options = {})
  @connection.get('/containers/ps', options).body_as_json
end
logs(container_id, options = {}) click to toggle source
# File lib/docker/resource/container.rb, line 99
def logs(container_id, options = {})
  options = {stdout: true, stderr: true} if options.empty?
  options = options.merge(logs: true, stream: false)
  
  response = @connection.post("/containers/#{container_id}/attach", options)
  raise_if_container_not_found(response.status)
  raise(BadParameterError) if response.status == 400
  response.body
end
remove(container_id, delete_volumes = false) click to toggle source

Options: v remove volumes of container

# File lib/docker/resource/container.rb, line 118
def remove(container_id, delete_volumes = false)
  params = {v: delete_volumes}
  status = @connection.delete("/containers/#{container_id}", params).status
  raise_if_container_not_found(status)
  status == 204
end
restart(container_id, timeout = nil) click to toggle source
# File lib/docker/resource/container.rb, line 71
def restart(container_id, timeout = nil)
  params = {}
  params['t'] = timeout if timeout
  status = @connection.post("/containers/#{container_id}/restart", params).status
  raise_if_container_not_found(status)
  status == 204
end
show(container_id) click to toggle source

inspect is a Ruby internal method that should not be overwritten therefore we use show as it displays the container details

# File lib/docker/resource/container.rb, line 32
def show(container_id)
  response = @connection.get("/containers/#{container_id}/json")
  raise_if_container_not_found(response.status)
  response.body_as_json
end
start(container_id) click to toggle source
# File lib/docker/resource/container.rb, line 57
def start(container_id)
  status = @connection.post("/containers/#{container_id}/start").status
  raise_if_container_not_found(status)
  status == 204
end
stop(container_id, timeout = nil) click to toggle source
# File lib/docker/resource/container.rb, line 63
def stop(container_id, timeout = nil)
  params = {}
  params['t'] = timeout if timeout
  status = @connection.post("/containers/#{container_id}/stop", params).status
  raise_if_container_not_found(status)
  status == 204
end
wait(container_id) click to toggle source

Blocks until container exits

# File lib/docker/resource/container.rb, line 110
def wait(container_id)
  response = @connection.post("/containers/#{container_id}/wait")
  raise_if_container_not_found(response.status)
  response.body_as_json
end

Private Instance Methods

raise_if_container_not_found(status) click to toggle source
# File lib/docker/resource/container.rb, line 127
def raise_if_container_not_found(status)
  raise(Docker::Error::ContainerNotFound) if status == 404
end