Class: Docker::API::Images
- Inherits:
-
Collection
- Object
- Collection
- Docker::API::Images
- Defined in:
- lib/docker/api/collections/images.rb
Overview
The images on a daemon.
Instance Attribute Summary
Attributes inherited from Collection
Instance Method Summary collapse
-
#all(all: false, filters: nil, digests: false) ⇒ Array<Docker::API::Image>
List images.
-
#build(context: nil, dockerfile: nil, tag: nil, platform: nil, nocache: false, rm: true, pull: false, buildargs: nil, labels: nil, target: nil) {|event| ... } ⇒ Docker::API::Image
Build an image.
-
#ensure(reference, platform: nil, auth: nil) {|event| ... } ⇒ Docker::API::Image
Fetch an image, pulling it first if the daemon does not have it.
-
#exist?(name, platform: nil) ⇒ Boolean
Whether an image is present locally.
-
#get(name, platform: nil) ⇒ Docker::API::Image
Fetch one image.
-
#prune(filters: nil) ⇒ Hash
Remove unused images.
-
#pull(reference, platform: nil, auth: nil) {|event| ... } ⇒ Docker::API::Image
Pull an image from a registry.
-
#search(term, limit: nil, filters: nil) ⇒ Array<Hash>
Search Docker Hub.
Methods inherited from Collection
Constructor Details
This class inherits a constructor from Docker::API::Collection
Instance Method Details
#all(all: false, filters: nil, digests: false) ⇒ Array<Docker::API::Image>
List images.
20 21 22 23 |
# File 'lib/docker/api/collections/images.rb', line 20 def all(all: false, filters: nil, digests: false) operations.image_list(all: all, filters: filters, digests: digests) .json!.map { |payload| Image.new(client: client, raw: payload, partial: true) } end |
#build(context: nil, dockerfile: nil, tag: nil, platform: nil, nocache: false, rm: true, pull: false, buildargs: nil, labels: nil, target: nil) {|event| ... } ⇒ Docker::API::Image
Build an image.
The context may be a directory, which is tarred honouring its .dockerignore, or a Dockerfile given as a string for the common case of a short generated build with no accompanying files.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/docker/api/collections/images.rb', line 126 def build(context: nil, dockerfile: nil, tag: nil, platform: nil, nocache: false, rm: true, pull: false, buildargs: nil, labels: nil, target: nil, &block) archive, dockerfile_name = build_context(context, dockerfile) image_id = nil failure = nil stream = Stream::JSONLines.new do |event| image_id ||= event.dig("aux", "ID") failure ||= event["error"] block&.call(event) end operations.image_build( body: archive, content_type: "application/x-tar", t: tag, platform: Platform.string(platform), nocache: nocache, rm: rm, pull: pull, dockerfile: dockerfile_name, target: target, buildargs: buildargs, labels: labels ) { |chunk| stream << chunk } # The daemon reports build failures inside a 200 response rather than # as a status code, so a build that failed looks like a success to # anything that only checks HTTP. raise Error.new("image build failed: #{failure}", operation: "image_build") if failure get(built_reference(tag, image_id)) end |
#ensure(reference, platform: nil, auth: nil) {|event| ... } ⇒ Docker::API::Image
Fetch an image, pulling it first if the daemon does not have it.
60 61 62 63 64 |
# File 'lib/docker/api/collections/images.rb', line 60 def ensure(reference, platform: nil, auth: nil, &block) get(reference, platform: platform) rescue NotFound pull(reference, platform: platform, auth: auth, &block) end |
#exist?(name, platform: nil) ⇒ Boolean
Whether an image is present locally.
47 48 49 50 51 |
# File 'lib/docker/api/collections/images.rb', line 47 def exist?(name, platform: nil) !get(name, platform: platform).nil? rescue NotFound false end |
#get(name, platform: nil) ⇒ Docker::API::Image
Fetch one image.
32 33 34 35 36 37 38 39 40 |
# File 'lib/docker/api/collections/images.rb', line 32 def get(name, platform: nil) Image.new( client: client, # This endpoint wants the OCI object form, unlike pull and build, # which want the string. See Docker::API::Platform. raw: operations.image_inspect(name: name, platform: Platform.oci(platform)).json, partial: false ) end |
#prune(filters: nil) ⇒ Hash
Remove unused images.
157 158 159 |
# File 'lib/docker/api/collections/images.rb', line 157 def prune(filters: nil) operations.image_prune(filters: filters).json end |
#pull(reference, platform: nil, auth: nil) {|event| ... } ⇒ Docker::API::Image
Pull an image from a registry.
Credentials are resolved for the registry in the reference, so pulling
from two private registries in one process needs no setup between
calls. Pass auth: to override.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/docker/api/collections/images.rb', line 81 def pull(reference, platform: nil, auth: nil, &block) repo, tag = Image.split_reference(reference) credentials = auth || Auth.resolve(Image.registry_for(repo)) stream = block ? Stream::JSONLines.new(&block) : nil # Pull wants the string form; inspect below wants the object form. operations.image_create( from_image: repo, tag: tag, platform: Platform.string(platform), x_registry_auth: credentials ) { |chunk| stream << chunk if stream } # Reassembled rather than reusing the caller's string, because the tag # may have been defaulted to "latest" along the way -- and because a # digest has to go back together with "@" rather than ":". get(Image.join_reference(repo, tag), platform: platform) end |
#search(term, limit: nil, filters: nil) ⇒ Array<Hash>
Search Docker Hub.
167 168 169 |
# File 'lib/docker/api/collections/images.rb', line 167 def search(term, limit: nil, filters: nil) operations.image_search(term: term, limit: limit, filters: filters).json end |