Class: Docker::Container
- Inherits:
-
Object
- Object
- Docker::Container
- Includes:
- Base
- 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 Base
Class Method Summary collapse
-
.all(opts = {}, conn = Docker.connection) ⇒ Object
Return all of the Containers.
-
.create(opts = {}, conn = Docker.connection) ⇒ Object
Create a new Container.
-
.get(id, opts = {}, conn = Docker.connection) ⇒ Object
Return the container with specified ID.
Instance Method Summary collapse
- #archive_in(inputs, output_path, opts = {}) ⇒ Object
- #archive_in_stream(output_path, opts = {}, &block) ⇒ Object
- #archive_out(path, &block) ⇒ Object
-
#attach(options = {}, &block) ⇒ Object
Attach to a container’s standard streams / logs.
-
#commit(options = {}) ⇒ Object
Create an Image from a Container’s change.s.
- #copy(path, &block) ⇒ Object
-
#exec(command, options = {}, &block) ⇒ Docker::Exec
Create an Exec instance inside the container.
-
#export(&block) ⇒ Object
Export the Container as a tar.
- #kill!(opts = {}) ⇒ Object
- #logs(opts = {}) ⇒ Object
- #read_file(path) ⇒ Object
-
#refresh! ⇒ Object
Update the @info hash, which is the only mutable state in this object.
-
#remove(options = {}) ⇒ Object
(also: #delete)
remove container.
- #rename(new_name) ⇒ Object
-
#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.
- #start!(opts = {}) ⇒ Object
-
#stats ⇒ Object
TODO: Implement Streaming stats.
- #store_file(path, file_content) ⇒ Object
- #streaming_logs(opts = {}, &block) ⇒ Object
-
#to_s ⇒ Object
Return a String representation of the Container.
-
#top(opts = {}) ⇒ Object
Return a List of Hashes that represents the top running processes.
- #update(opts) ⇒ Object
-
#wait(time = nil) ⇒ Object
Wait for the current command to finish executing.
Methods included from Base
Class Method Details
.all(opts = {}, conn = Docker.connection) ⇒ Object
Return all of the Containers.
328 329 330 331 |
# File 'lib/docker/container.rb', line 328 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) ⇒ Object
Create a new Container.
311 312 313 314 315 316 317 318 |
# File 'lib/docker/container.rb', line 311 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) ⇒ Object
Return the container with specified ID
321 322 323 324 325 |
# File 'lib/docker/container.rb', line 321 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 |
Instance Method Details
#archive_in(inputs, output_path, opts = {}) ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/docker/container.rb', line 263 def archive_in(inputs, output_path, opts = {}) file_hash = Docker::Util.file_hash_from_paths([*inputs]) tar = StringIO.new(Docker::Util.create_tar(file_hash)) archive_in_stream(output_path, opts) do tar.read(Excon.defaults[:chunk_size]).to_s end end |
#archive_in_stream(output_path, opts = {}, &block) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/docker/container.rb', line 271 def archive_in_stream(output_path, opts = {}, &block) overwrite = opts[:overwrite] || opts['overwrite'] || false connection.put( path_for(:archive), { 'path' => output_path, 'noOverwriteDirNonDir' => !overwrite }, :headers => { 'Content-Type' => 'application/x-tar' }, &block ) self end |
#archive_out(path, &block) ⇒ Object
254 255 256 257 258 259 260 261 |
# File 'lib/docker/container.rb', line 254 def archive_out(path, &block) connection.get( path_for(:archive), { 'path' => path }, :response_block => block ) self end |
#attach(options = {}, &block) ⇒ Object
Attach to a container’s standard streams / logs.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/docker/container.rb', line 102 def attach( = {}, &block) stdin = .delete(:stdin) tty = .delete(:tty) opts = { :stream => true, :stdout => true, :stderr => true }.merge() # 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] = Docker::Util.hijack_for(stdin, block, msgs, tty) else excon_params[:response_block] = Docker::Util.attach_for(block, msgs, tty) end connection.post( path_for(:attach), opts, excon_params ) [msgs., msgs.] end |
#commit(options = {}) ⇒ Object
Create an Image from a Container’s change.s
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/docker/container.rb', line 133 def commit( = {}) .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 = .delete('run') hash = Docker::Util.parse_json(connection.post('/commit', , :body => config.to_json)) Docker::Image.send(:new, self.connection, hash) end |
#copy(path, &block) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/docker/container.rb', line 246 def copy(path, &block) connection.post(path_for(:copy), {}, :body => { "Resource" => path }.to_json, :response_block => block ) self end |
#exec(command, options = {}, &block) ⇒ Docker::Exec
Create an Exec instance inside the container
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/docker/container.rb', line 54 def exec(command, = {}, &block) # Establish values tty = .delete(:tty) || false detach = .delete(:detach) || false user = .delete(:user) stdin = .delete(:stdin) stdout = .delete(:stdout) || !detach stderr = .delete(:stderr) || !detach wait = .delete(:wait) opts = { 'Container' => self.id, 'User' => user, 'AttachStdin' => !!stdin, 'AttachStdout' => stdout, 'AttachStderr' => stderr, 'Tty' => tty, 'Cmd' => command }.merge() # Create Exec Instance instance = Docker::Exec.create( opts, self.connection ) start_opts = { :tty => tty, :stdin => stdin, :detach => detach, :wait => wait } if detach instance.start!(start_opts) return instance else instance.start!(start_opts, &block) end end |
#export(&block) ⇒ Object
Export the Container as a tar.
96 97 98 99 |
# File 'lib/docker/container.rb', line 96 def export(&block) connection.get(path_for(:export), {}, :response_block => block) self end |
#kill!(opts = {}) ⇒ Object
193 194 195 196 |
# File 'lib/docker/container.rb', line 193 def kill!(opts = {}) connection.post(path_for(:kill), opts) self end |
#logs(opts = {}) ⇒ Object
159 160 161 |
# File 'lib/docker/container.rb', line 159 def logs(opts = {}) connection.get(path_for(:logs), opts) end |
#read_file(path) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/docker/container.rb', line 285 def read_file(path) content = StringIO.new archive_out(path) do |chunk| content.write chunk end content.rewind Gem::Package::TarReader.new(content) do |tar| tar.each do |tarfile| return tarfile.read end end end |
#refresh! ⇒ Object
Update the @info hash, which is the only mutable state in this object.
e.g. if you would like a live status from the #info hash, call #refresh! first.
8 9 10 11 12 13 14 15 16 |
# File 'lib/docker/container.rb', line 8 def refresh! other = Docker::Container.all({all: true}, connection).find { |c| c.id.start_with?(self.id) || self.id.start_with?(c.id) } info.merge!(self.json) other && info.merge!(other.info) { |key, info_value, other_value| info_value } self end |
#remove(options = {}) ⇒ Object Also known as: delete
remove container
225 226 227 228 |
# File 'lib/docker/container.rb', line 225 def remove( = {}) connection.delete("/containers/#{self.id}", ) nil end |
#rename(new_name) ⇒ Object
168 169 170 171 172 |
# File 'lib/docker/container.rb', line 168 def rename(new_name) query = {} query['name'] = new_name connection.post(path_for(:rename), query) 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.
40 41 42 43 44 45 46 |
# File 'lib/docker/container.rb', line 40 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 = {}) ⇒ Object
188 189 190 191 |
# File 'lib/docker/container.rb', line 188 def start!(opts = {}) connection.post(path_for(:start), {}, :body => opts.to_json) self end |
#stats ⇒ Object
TODO: Implement Streaming stats
164 165 166 |
# File 'lib/docker/container.rb', line 164 def stats Docker::Util.parse_json(connection.get(path_for(:stats), {stream: 0})) end |
#store_file(path, file_content) ⇒ Object
300 301 302 303 304 305 306 307 308 |
# File 'lib/docker/container.rb', line 300 def store_file(path, file_content) output_io = StringIO.new( Docker::Util.create_tar( path => file_content ) ) archive_in_stream("/", overwrite: true) { output_io.read } end |
#streaming_logs(opts = {}, &block) ⇒ Object
178 179 180 181 182 183 184 185 186 |
# File 'lib/docker/container.rb', line 178 def streaming_logs(opts = {}, &block) stack_size = opts.delete('stack_size') || -1 tty = opts.delete('tty') || opts.delete(:tty) || false msgs = Docker::MessagesStack.new(stack_size) excon_params = {response_block: Docker::Util.attach_for(block, msgs, tty)} connection.get(path_for(:logs), opts, excon_params) msgs..join end |
#to_s ⇒ Object
Return a String representation of the Container.
147 148 149 |
# File 'lib/docker/container.rb', line 147 def to_s "Docker::Container { :id => #{self.id}, :connection => #{self.connection} }" end |
#top(opts = {}) ⇒ Object
Return a List of Hashes that represents the top running processes.
19 20 21 22 23 24 25 26 |
# File 'lib/docker/container.rb', line 19 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 |
#update(opts) ⇒ Object
174 175 176 |
# File 'lib/docker/container.rb', line 174 def update(opts) connection.post(path_for(:update), {}, body: opts.to_json) end |
#wait(time = nil) ⇒ Object
Wait for the current command to finish executing. Default wait time is ‘Excon.options`.
30 31 32 33 34 |
# File 'lib/docker/container.rb', line 30 def wait(time = nil) excon_params = { :read_timeout => time } resp = connection.post(path_for(:wait), nil, excon_params) Docker::Util.parse_json(resp) end |