Class: Docker::API::Container

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

Overview

This class represents the Docker API endpoints regarding containers.

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

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

Instance Method Details

#attach(name, params = {}, &block) ⇒ Object

Attach to a container.

Docker API: POST /containers/id/attach

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

  • &block:

    Replace the default output to stdout behavior.

See Also:



222
223
224
# File 'lib/docker/api/container.rb', line 222

def attach name, params = {}, &block
    @connection.request(method: :post, path: build_path("/containers/#{name}/attach", params) , response_block: block_given? ? block : default_streamer)
end

#changes(name) ⇒ Object

Get changes on a container’s filesystem.

Docker API: GET /containers/id/changes

Parameters:

  • name (String)

    : The ID or name of the container.

See Also:



48
49
50
# File 'lib/docker/api/container.rb', line 48

def changes name
    @connection.get("/containers/#{name}/changes")
end

#create(params = {}, body = {}) ⇒ Object

Create a container.

Docker API: POST /containers/create

Parameters:

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

  • body (Hash) (defaults to: {})

    : Request body to be sent as json.

See Also:



234
235
236
# File 'lib/docker/api/container.rb', line 234

def create params = {}, body = {}
    @connection.request(method: :post, path: build_path("/containers/create", params), headers: {"Content-Type": "application/json"}, body: body.to_json)
end

#details(name, params = {}) ⇒ Object

Return low-level information about a container.

Docker API: GET /containers/id/json

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



25
26
27
# File 'lib/docker/api/container.rb', line 25

def details name, params = {}
    @connection.get(build_path("/containers/#{name}/json", params))
end

#export(name, path, &block) ⇒ Object

Export the contents of a container as a tarball.

Docker API: GET /containers/id/export

Parameters:

  • name (String)

    : The ID or name of the container.

  • path (String)

    : Path to the exportated file.

  • &block:

    Replace the default file writing behavior.

See Also:



265
266
267
268
269
# File 'lib/docker/api/container.rb', line 265

def export name, path, &block
    response = self.details(name)
    return response unless response.status == 200
    @connection.request(method: :get, path: "/containers/#{name}/export" , response_block: block_given? ? block : default_writer(path))
end

#get_archive(name, path, params = {}, &block) ⇒ Object

Get an archive of a filesystem resource in a container.

Get a tar archive of a resource in the filesystem of container id.

Docker API: GET /containers/id/archive

Parameters:

  • name (String)

    : The ID or name of the container.

  • path (String)

    : Path to the exportated file.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

  • &block:

    Replace the default file writing behavior.

See Also:



283
284
285
286
287
288
289
290
291
# File 'lib/docker/api/container.rb', line 283

def get_archive name, path, params = {}, &block
    response = @connection.head(build_path("/containers/#{name}/archive", params))
    return response unless response.status == 200 
    
    file = File.open( File.expand_path( path ) , "wb")
    response = @connection.request(method: :get, path: build_path("/containers/#{name}/archive", params) , response_block: block_given? ? block : lambda { |chunk, remaining_bytes, total_bytes| file.write(chunk) })
    file.close
    response
end

#kill(name, params = {}) ⇒ Object

Send a POSIX signal to a container, defaulting to killing to the container.

Docker API: POST /containers/id/kill

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



96
97
98
# File 'lib/docker/api/container.rb', line 96

def kill name, params = {}
    @connection.post(build_path("/containers/#{name}/kill", params))
end

#list(params = {}) ⇒ Object

Return a list of containers.

Docker API: GET /containers/json

Parameters:

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



13
14
15
# File 'lib/docker/api/container.rb', line 13

def list params = {}
    @connection.get(build_path("/containers/json", params))
end

#logs(name, params = {}, &block) ⇒ Object

Get stdout and stderr logs from a container.

Docker API: GET /containers/id/logs

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

  • &block:

    Replace the default output to stdout behavior.

See Also:



202
203
204
205
206
207
208
209
210
211
# File 'lib/docker/api/container.rb', line 202

def logs name, params = {}, &block
    
    path = build_path("/containers/#{name}/logs", params)

    if [true, 1 ].include? params[:follow]
        @connection.request(method: :get, path: path , response_block: block_given? ? block : default_streamer)
    else
        @connection.get(path)
    end
end

#pause(name) ⇒ Object

Pause a container.

Docker API: POST /containers/id/pause

Parameters:

  • name (String)

    : The ID or name of the container.

See Also:



166
167
168
# File 'lib/docker/api/container.rb', line 166

def pause name
    @connection.post("/containers/#{name}/pause")
end

#prune(params = {}) ⇒ Object

Delete stopped containers.

Docker API: POST /containers/prune

Parameters:

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



155
156
157
# File 'lib/docker/api/container.rb', line 155

def prune params = {}
    @connection.post(build_path("/containers/prune", params))
end

#put_archive(name, path, params = {}) ⇒ Object

Extract an archive of files or folders to a directory in a container.

Upload a tar archive to be extracted to a path in the filesystem of container id.

Docker API: PUT /containers/id/archive

Parameters:

  • name (String)

    : The ID or name of the container.

  • path (String)

    : Path of the tar file.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



304
305
306
307
308
309
# File 'lib/docker/api/container.rb', line 304

def put_archive name, path, params = {}
    file = File.open( File.expand_path( path ) , "r")
    response = @connection.request(method: :put, path: build_path("/containers/#{name}/archive", params) , request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s} )
    file.close
    response
end

#remove(name, params = {}) ⇒ Object

Remove a container.

Docker API: DELETE /containers/id

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



189
190
191
# File 'lib/docker/api/container.rb', line 189

def remove name, params = {}
    @connection.delete(build_path("/containers/#{name}", params))
end

#rename(name, params = {}) ⇒ Object

Rename a container.

Docker API: POST /containers/id/rename

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



132
133
134
# File 'lib/docker/api/container.rb', line 132

def rename name, params = {}
    @connection.post(build_path("/containers/#{name}/rename", params))
end

#resize(name, params = {}) ⇒ Object

Resize the TTY for a container.

Docker API: POST /containers/id/resize

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



144
145
146
# File 'lib/docker/api/container.rb', line 144

def resize name, params = {}
    @connection.post(build_path("/containers/#{name}/resize", params))
end

#restart(name, params = {}) ⇒ Object

Restart a container.

Docker API: POST /containers/id/restart

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



84
85
86
# File 'lib/docker/api/container.rb', line 84

def restart name, params = {}
    @connection.post(build_path("/containers/#{name}/restart", params))
end

#start(name, params = {}) ⇒ Object

Start a container.

Docker API: POST /containers/id/start

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



60
61
62
# File 'lib/docker/api/container.rb', line 60

def start name, params = {}
    @connection.post(build_path("/containers/#{name}/start", params))
end

#stats(name, params = {}, &block) ⇒ Object

Get container stats based on resource usage.

Docker API: GET /containers/id/stats

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

  • &block:

    Replace the default output to stdout behavior.

See Also:



247
248
249
250
251
252
253
254
# File 'lib/docker/api/container.rb', line 247

def stats name, params = {}, &block
    path = build_path("/containers/#{name}/stats", params)
    if [true, 1 ].include? params[:stream]
        @connection.request(method: :get, path: path , response_block: block_given? ? block : default_streamer)
    else
        @connection.get(path)
    end
end

#stop(name, params = {}) ⇒ Object

Stop a container.

Docker API: POST /containers/id/stop

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



72
73
74
# File 'lib/docker/api/container.rb', line 72

def stop name, params = {}
    @connection.post(build_path("/containers/#{name}/stop", params))
end

#top(name, params = {}) ⇒ Object

List processes running inside a container.

Docker API: GET /containers/id/top

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



37
38
39
# File 'lib/docker/api/container.rb', line 37

def top name, params = {}
    @connection.get(build_path("/containers/#{name}/top", params))
end

#unpause(name) ⇒ Object

Resume a container which has been paused.

Docker API: POST /containers/id/unpause

Parameters:

  • name (String)

    : The ID or name of the container.

See Also:



177
178
179
# File 'lib/docker/api/container.rb', line 177

def unpause name
    @connection.post("/containers/#{name}/unpause")
end

#update(name, body = {}) ⇒ Object

Change various configuration options of a container without having to recreate it.

Docker API: POST /containers/id/update

Parameters:

  • name (String)

    : The ID or name of the container.

  • body (Hash) (defaults to: {})

    : Request body to be sent as json.

See Also:



120
121
122
# File 'lib/docker/api/container.rb', line 120

def update name, body = {}
    @connection.request(method: :post, path: "/containers/#{name}/update", headers: {"Content-Type": "application/json"}, body: body.to_json)
end

#wait(name, params = {}) ⇒ Object

Block until a container stops, then returns the exit code.

Docker API: POST /containers/id/wait

Parameters:

  • name (String)

    : The ID or name of the container.

  • params (Hash) (defaults to: {})

    : Parameters that are appended to the URL.

See Also:



108
109
110
# File 'lib/docker/api/container.rb', line 108

def wait name, params = {}
    @connection.post(build_path("/containers/#{name}/wait", params))
end