Class: Docker::API::Containers

Inherits:
Collection show all
Defined in:
lib/docker/api/collections/containers.rb

Overview

The containers on a daemon.

Examples:

client.containers.all(all: true)
client.containers.create(image: "alpine:3.20", name: "scratch", cmd: %w{sleep 30})

Instance Attribute Summary

Attributes inherited from Collection

#client

Instance Method Summary collapse

Methods inherited from Collection

#exist?, #find, #initialize, #to_s

Constructor Details

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

Instance Method Details

#all(all: false, limit: nil, size: false, filters: nil) ⇒ Array<Docker::API::Container>

List containers.

Parameters:

  • all (Boolean) (defaults to: false)

    include containers that are not running

  • limit (Integer, nil) (defaults to: nil)

    return only the most recent N

  • size (Boolean) (defaults to: false)

    include the filesystem size of each

  • filters (Hash, nil) (defaults to: nil)

    daemon-side filters, e.g. { "status" => ["running"] }

Returns:

  • (Array<Docker::API::Container>)

    partial resources; accessors that need detail the list did not carry will fetch it



22
23
24
25
# File 'lib/docker/api/collections/containers.rb', line 22

def all(all: false, limit: nil, size: false, filters: nil)
  operations.container_list(all: all, limit: limit, size: size, filters: filters)
    .json!.map { |payload| Container.new(client: client, raw: payload, partial: true) }
end

#create(name: nil, platform: nil, **attributes) ⇒ Docker::API::Container

Create a container.

Body attributes may be given in snake_case and are converted to the daemon's spelling; keys already in the daemon's convention are passed through untouched, so a configuration copied out of Docker's own documentation works as-is. Only top-level keys are converted -- nested maps such as Labels and PortBindings have keys that are data.

Examples:

client.containers.create(
  image: "alpine:3.20",
  name: "worker",
  cmd: %w{sleep 3600},
  env: ["LOG_LEVEL=debug"],
  host_config: { "Binds" => ["/data:/data:ro"] }
)

Parameters:

  • name (String, nil) (defaults to: nil)

    a name for the container

  • platform (String, nil) (defaults to: nil)

    "os[/arch[/variant]]" to create for

  • attributes (Hash)

    the container configuration

Returns:



62
63
64
65
66
67
# File 'lib/docker/api/collections/containers.rb', line 62

def create(name: nil, platform: nil, **attributes)
  response = operations.container_create(
    body: Body.build(attributes), name: name, platform: platform
  )
  get(response.json!["Id"])
end

#get(id, size: false) ⇒ Docker::API::Container

Fetch one container.

Parameters:

  • id (String)

    a name or id

  • size (Boolean) (defaults to: false)

    include filesystem size information

Returns:

Raises:



33
34
35
36
37
38
39
# File 'lib/docker/api/collections/containers.rb', line 33

def get(id, size: false)
  Container.new(
    client: client,
    raw: operations.container_inspect(id: id, size: size).json,
    partial: false
  )
end

#prune(filters: nil) ⇒ Hash

Remove stopped containers.

Parameters:

  • filters (Hash, nil) (defaults to: nil)

    which containers to consider

Returns:

  • (Hash)

    what was deleted and how much space it freed



73
74
75
# File 'lib/docker/api/collections/containers.rb', line 73

def prune(filters: nil)
  operations.container_prune(filters: filters).json
end