class Docker::API::Container

This class represents the Docker API endpoints regarding containers. @see docs.docker.com/engine/api/v1.40/#tag/Container

Public Instance Methods

attach(name, params = {}) click to toggle source

Attach to a container.

Docker API: POST /containers/{id}/attach @see docs.docker.com/engine/api/v1.40/#operation/ContainerAttach

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL. @param &block: Replace the default output to stdout behavior.

# 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) click to toggle source

Get changes on a container’s filesystem.

Docker API: GET /containers/{id}/changes @see docs.docker.com/engine/api/v1.40/#operation/ContainerChanges

@param name [String]: The ID or name of the container.

# File lib/docker/api/container.rb, line 48
def changes name
    @connection.get("/containers/#{name}/changes")
end
create(params = {}) click to toggle source

Create a container.

Docker API: POST /containers/create @see docs.docker.com/engine/api/v1.40/#operation/ContainerCreate

@param params [Hash]: Parameters that are appended to the URL. @param body [Hash]: Request body to be sent as json.

# 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 = {}) click to toggle source

Return low-level information about a container.

Docker API: GET /containers/{id}/json @see docs.docker.com/engine/api/v1.40/#operation/ContainerInspect

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# 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) click to toggle source

Export the contents of a container as a tarball.

Docker API: GET /containers/{id}/export @see docs.docker.com/engine/api/v1.40/#operation/ContainerExport

@param name [String]: The ID or name of the container. @param path [String]: Path to the exportated file. @param &block: Replace the default file writing behavior.

# 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 = {}) click to toggle source

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 @see docs.docker.com/engine/api/v1.40/#operation/ContainerArchive

@param name [String]: The ID or name of the container. @param path [String]: Path to the exportated file. @param params [Hash]: Parameters that are appended to the URL. @param &block: Replace the default file writing behavior.

# 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 = {}) click to toggle source

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

Docker API: POST /containers/{id}/kill @see docs.docker.com/engine/api/v1.40/#operation/ContainerKill

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 96
def kill name, params = {}
    @connection.post(build_path("/containers/#{name}/kill", params))
end
list(params = {}) click to toggle source

Return a list of containers.

Docker API: GET /containers/json @see docs.docker.com/engine/api/v1.40/#operation/ContainerList

@param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 13
def list params = {}
    @connection.get(build_path("/containers/json", params))
end
logs(name, params = {}) click to toggle source

Get stdout and stderr logs from a container.

Docker API: GET /containers/{id}/logs @see docs.docker.com/engine/api/v1.40/#operation/ContainerLogs

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL. @param &block: Replace the default output to stdout behavior.

# 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) click to toggle source

Pause a container.

Docker API: POST /containers/{id}/pause @see docs.docker.com/engine/api/v1.40/#operation/ContainerPause

@param name [String]: The ID or name of the container.

# File lib/docker/api/container.rb, line 166
def pause name
    @connection.post("/containers/#{name}/pause")
end
prune(params = {}) click to toggle source

Delete stopped containers.

Docker API: POST /containers/prune @see docs.docker.com/engine/api/v1.40/#operation/ContainerPrune

@param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 155
def prune params = {}
    @connection.post(build_path("/containers/prune", params))
end
put_archive(name, path, params = {}) click to toggle source

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 @see docs.docker.com/engine/api/v1.40/#operation/PutContainerArchive

@param name [String]: The ID or name of the container. @param path [String]: Path of the tar file. @param params [Hash]: Parameters that are appended to the URL.

# 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 = {}) click to toggle source

Remove a container.

Docker API: DELETE /containers/{id} @see docs.docker.com/engine/api/v1.40/#operation/ContainerDelete

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 189
def remove name, params = {}
    @connection.delete(build_path("/containers/#{name}", params))
end
rename(name, params = {}) click to toggle source

Rename a container.

Docker API: POST /containers/{id}/rename @see docs.docker.com/engine/api/v1.40/#operation/ContainerRename

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 132
def rename name, params = {}
    @connection.post(build_path("/containers/#{name}/rename", params))
end
resize(name, params = {}) click to toggle source

Resize the TTY for a container.

Docker API: POST /containers/{id}/resize @see docs.docker.com/engine/api/v1.40/#operation/ContainerResize

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 144
def resize name, params = {}
    @connection.post(build_path("/containers/#{name}/resize", params))
end
restart(name, params = {}) click to toggle source

Restart a container.

Docker API: POST /containers/{id}/restart @see docs.docker.com/engine/api/v1.40/#operation/ContainerRestart

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 84
def restart name, params = {}
    @connection.post(build_path("/containers/#{name}/restart", params))
end
start(name, params = {}) click to toggle source

Start a container.

Docker API: POST /containers/{id}/start @see docs.docker.com/engine/api/v1.40/#operation/ContainerStart

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 60
def start name, params = {}
    @connection.post(build_path("/containers/#{name}/start", params))
end
stats(name, params = {}) click to toggle source

Get container stats based on resource usage.

Docker API: GET /containers/{id}/stats @see docs.docker.com/engine/api/v1.40/#operation/ContainerStats

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL. @param &block: Replace the default output to stdout behavior.

# 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 = {}) click to toggle source

Stop a container.

Docker API: POST /containers/{id}/stop @see docs.docker.com/engine/api/v1.40/#operation/ContainerStop

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 72
def stop name, params = {}
    @connection.post(build_path("/containers/#{name}/stop", params))
end
top(name, params = {}) click to toggle source

List processes running inside a container.

Docker API: GET /containers/{id}/top @see docs.docker.com/engine/api/v1.40/#operation/ContainerTop

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 37
def top name, params = {}
    @connection.get(build_path("/containers/#{name}/top", params))
end
unpause(name) click to toggle source

Resume a container which has been paused.

Docker API: POST /containers/{id}/unpause @see docs.docker.com/engine/api/v1.40/#operation/ContainerUnpause

@param name [String]: The ID or name of the container.

# File lib/docker/api/container.rb, line 177
def unpause name
    @connection.post("/containers/#{name}/unpause")
end
update(name, body = {}) click to toggle source

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

Docker API: POST /containers/{id}/update @see docs.docker.com/engine/api/v1.40/#operation/ContainerUpdate

@param name [String]: The ID or name of the container. @param body [Hash]: Request body to be sent as json.

# 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 = {}) click to toggle source

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

Docker API: POST /containers/{id}/wait @see docs.docker.com/engine/api/v1.40/#operation/ContainerWait

@param name [String]: The ID or name of the container. @param params [Hash]: Parameters that are appended to the URL.

# File lib/docker/api/container.rb, line 108
def wait name, params = {}
    @connection.post(build_path("/containers/#{name}/wait", params))
end