Class: Docker::Resource::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/docker/resource/container.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Docker::Resource::Base

Instance Method Details

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

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

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



38
39
40
# 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 = {}) ⇒ Object

TODO default run configuration not supported yet



43
44
45
46
47
48
49
50
# 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 = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# 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

#exportObject

Returns a stream



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

def export
  
end

#kill(container_id) ⇒ Object



79
80
81
82
83
# 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 = {}) ⇒ Object

Options all limit since before



15
16
17
# File 'lib/docker/resource/container.rb', line 15

def list(options = {})
  @connection.get('/containers/ps', options).body_as_json
end

#logs(container_id, options = {}) ⇒ Object

Raises:

  • (BadParameterError)


99
100
101
102
103
104
105
106
107
# 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) ⇒ Object

Options: v remove volumes of container



118
119
120
121
122
123
# 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) ⇒ Object



71
72
73
74
75
76
77
# 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) ⇒ Object

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



32
33
34
35
36
# 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) ⇒ Object



57
58
59
60
61
# 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) ⇒ Object



63
64
65
66
67
68
69
# 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) ⇒ Object

Blocks until container exits



110
111
112
113
114
# 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