Parent

Included Modules

Docker::Image

This class represents a Docker Image.

Public Class Methods

all(opts = {}, conn = Docker.connection) click to toggle source

Return every Image.

# File lib/docker/image.rb, line 160
def all(opts = {}, conn = Docker.connection)
  hashes = Docker::Util.parse_json(conn.get('/images/json', opts)) || []
  hashes.map { |hash| new(conn, hash) }
end
build(commands, opts = {}, connection = Docker.connection, &block) click to toggle source

Given a Dockerfile as a string, builds an Image.

# File lib/docker/image.rb, line 197
def build(commands, opts = {}, connection = Docker.connection, &block)
  body = ""
  connection.post(
    '/build', opts,
    :body => Docker::Util.create_tar('Dockerfile' => commands),
    :response_block => response_block_for_build(body, &block)
  )
  new(connection, 'id' => Docker::Util.extract_id(body))
rescue Docker::Error::ServerError
  raise Docker::Error::UnexpectedResponseError
end
build_from_dir(dir, opts = {}, connection = Docker.connection, creds = nil, &block) click to toggle source

Given a directory that contains a Dockerfile, builds an Image.

If a block is passed, chunks of output produced by Docker will be passed to that block.

# File lib/docker/image.rb, line 235
def build_from_dir(dir, opts = {}, connection = Docker.connection,
                   creds = nil, &block)

  tar = Docker::Util.create_dir_tar(dir)
  build_from_tar tar, opts, connection, creds, &block
ensure
  unless tar.nil?
    tar.close
    FileUtils.rm(tar.path, force: true)
  end
end
build_from_tar(tar, opts = {}, connection = Docker.connection, creds = nil, &block) click to toggle source

Given File like object containing a tar file, builds an Image.

If a block is passed, chunks of output produced by Docker will be passed to that block.

# File lib/docker/image.rb, line 213
def build_from_tar(tar, opts = {}, connection = Docker.connection,
                   creds = nil, &block)

  headers = build_headers(creds)

  # The response_block passed to Excon will build up this body variable.
  body = ""
  connection.post(
    '/build', opts,
    :headers => headers,
    :response_block => response_block_for_build(body, &block)
  ) { tar.read(Excon.defaults[:chunk_size]).to_s }

  new(connection,
      'id' => Docker::Util.extract_id(body),
      :headers => headers)
end
create(opts = {}, creds = nil, conn = Docker.connection) click to toggle source

Create a new Image.

# File lib/docker/image.rb, line 103
def create(opts = {}, creds = nil, conn = Docker.connection)
  credentials = creds.nil? ? Docker.creds : creds.to_json
  headers = !credentials.nil? && Docker::Util.build_auth_header(credentials)
  headers ||= {}
  body = conn.post('/images/create', opts, :headers => headers)
  json = Docker::Util.fix_json(body)
  image = json.reverse_each.find { |el| el && el.key?('id') }
  new(conn, 'id' => image && image.fetch('id'), :headers => headers)
end
exist?(id, opts = {}, conn = Docker.connection) click to toggle source

Check if an image exists.

# File lib/docker/image.rb, line 152
def exist?(id, opts = {}, conn = Docker.connection)
  get(id, opts, conn)
  true
rescue Docker::Error::NotFoundError
  false
end
get(id, opts = {}, conn = Docker.connection) click to toggle source

Return a specific image.

# File lib/docker/image.rb, line 114
def get(id, opts = {}, conn = Docker.connection)
  image_json = conn.get("/images/#{URI.encode(id)}/json", opts)
  hash = Docker::Util.parse_json(image_json) || {}
  new(conn, hash)
end
import(imp, opts = {}, conn = Docker.connection) click to toggle source

Import an Image from the output of Docker::Container#export. The first argument may either be a File or URI.

# File lib/docker/image.rb, line 175
def import(imp, opts = {}, conn = Docker.connection)
  open(imp) do |io|
    import_stream(opts, conn) do
      io.read(Excon.defaults[:chunk_size]).to_s
    end
  end
rescue StandardError
  raise Docker::Error::IOError, "Could not import '#{imp}'"
end
import_stream(options = {}, connection = Docker.connection, &block) click to toggle source
# File lib/docker/image.rb, line 185
def import_stream(options = {}, connection = Docker.connection, &block)
  body = connection.post(
    '/images/create',
     options.merge('fromSrc' => '-'),
     :headers => { 'Content-Type' => 'application/tar',
                   'Transfer-Encoding' => 'chunked' },
     &block
  )
  new(connection, 'id'=> Docker::Util.parse_json(body)['status'])
end
save(names, filename = nil, conn = Docker.connection) click to toggle source

Save the raw binary representation or one or more Docker images

@param names [String, Array#String] The image(s) you wish to save @param filename [String] The file to export the data to. @param conn [Docker::Connection] The Docker connection to use

@return [NilClass, String] If filename is nil, return the string representation of the binary data. If the filename is not nil, then return nil.

# File lib/docker/image.rb, line 129
def save(names, filename = nil, conn = Docker.connection)
  # By using compare_by_identity we can create a Hash that has
  # the same key multiple times.
  query = {}
  query.compare_by_identity
  Array(names).each do |name|
    query['names'] = URI.encode(name)
  end

  if filename
    file = File.open(filename, 'wb')
    conn.get(
      '/images/get', query,
      :response_block => response_block_for_save(file)
    )
    file.close
    nil
  else
    conn.get('/images/get', query)
  end
end
search(query = {}, connection = Docker.connection) click to toggle source

Given a query like `{ :term => 'sshd' }`, queries the Docker Registry for a corresponding Image.

# File lib/docker/image.rb, line 167
def search(query = {}, connection = Docker.connection)
  body = connection.get('/images/search', query)
  hashes = Docker::Util.parse_json(body) || []
  hashes.map { |hash| new(connection, 'id' => hash['name']) }
end

Public Instance Methods

delete(opts = {}) click to toggle source
Alias for: remove
insert_local(opts = {}) click to toggle source

Given a path of a local file and the path it should be inserted, creates a new Image that has that file.

# File lib/docker/image.rb, line 49
def insert_local(opts = {})
  local_paths = opts.delete('localPath')
  output_path = opts.delete('outputPath')

  local_paths = [ local_paths ] unless local_paths.is_a?(Array)

  file_hash = Docker::Util.file_hash_from_paths(local_paths)

  file_hash['Dockerfile'] = dockerfile_for(file_hash, output_path)

  tar = Docker::Util.create_tar(file_hash)
  body = connection.post('/build', opts, :body => tar)
  self.class.send(:new, connection, 'id' => Docker::Util.extract_id(body))
end
push(creds = nil, options = {}) click to toggle source

Push the Image to the Docker registry.

# File lib/docker/image.rb, line 25
def push(creds = nil, options = {})
  repo_tag = options.delete(:repo_tag) || ensure_repo_tags.first
  raise ArgumentError, "Image is untagged" if repo_tag.nil?
  repo, tag = Docker::Util.parse_repo_tag(repo_tag)
  raise ArgumentError, "Image does not have a name to push." if repo.nil?

  credentials = creds || Docker.creds || {}
  headers = Docker::Util.build_auth_header(credentials)
  opts = {:tag => tag}.merge(options)
  connection.post("/images/#{repo}/push", opts, :headers => headers)
  self
end
refresh!() click to toggle source

Update the @info hash, which is the only mutable state in this object.

# File lib/docker/image.rb, line 91
def refresh!
  img = Docker::Image.all({:all => true}, connection).find { |image|
    image.id.start_with?(self.id) || self.id.start_with?(image.id)
  }
  info.merge!(self.json)
  img && info.merge!(img.info)
  self
end
remove(opts = {}) click to toggle source

Remove the Image from the server.

# File lib/docker/image.rb, line 65
def remove(opts = {})
  name = opts.delete(:name) || self.id
  connection.delete("/images/#{name}", opts)
end
Also aliased as: delete
run(cmd=nil) click to toggle source

Given a command and optional list of streams to attach to, run a command on an Image. This will not modify the Image, but rather create a new Container to run the Image. If the image has an embedded config, no command is necessary, but it will fail with 500 if no config is saved with the image

# File lib/docker/image.rb, line 9
def run(cmd=nil)
  opts = { 'Image' => self.id }
  opts["Cmd"] = cmd.is_a?(String) ? cmd.split(/\s+/) : cmd
  begin
    Docker::Container.create(opts, connection)
                     .tap(&:start!)
  rescue ServerError => ex
    if cmd
      raise ex
    else
      raise ServerError, "No command specified."
    end
  end
end
save(filename = nil) click to toggle source

Save the image as a tarball

# File lib/docker/image.rb, line 86
def save(filename = nil)
  self.class.save(self.id, filename, connection)
end
tag(opts = {}) click to toggle source

Tag the Image.

# File lib/docker/image.rb, line 39
def tag(opts = {})
  self.info['RepoTags'] ||= []
  connection.post(path_for(:tag), opts)
  repo = opts['repo'] || opts[:repo]
  tag = opts['tag'] || opts[:tag] || 'latest'
  self.info['RepoTags'] << "#{repo}:#{tag}"
end
to_s() click to toggle source

Return a String representation of the Image.

# File lib/docker/image.rb, line 72
def to_s
  "Docker::Image { :id => #{self.id}, :info => #{self.info.inspect}, "       ":connection => #{self.connection} }"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.