Parent

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 118
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 145
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, &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 161
def build_from_dir(dir, opts = {}, connection = Docker.connection, &block)
  tar = Docker::Util.create_dir_tar(dir)

  # The response_block passed to Excon will build up this body variable.
  body = ""
  connection.post(
    '/build', opts,
    :headers => { 'Content-Type'      => 'application/tar',
                  'Transfer-Encoding' => 'chunked' },
    :response_block => response_block_for_build(body, &block)
  ) { tar.read(Excon.defaults[:chunk_size]).to_s }
  new(connection, 'id' => Docker::Util.extract_id(body))
ensure
  tar.close unless tar.nil?
end
create(opts = {}, creds = nil, conn = Docker.connection) click to toggle source

Create a new Image.

# File lib/docker/image.rb, line 98
def create(opts = {}, creds = nil, conn = Docker.connection)
  credentials = (creds.nil?) ? creds.to_json : Docker.creds
  headers = if credentials.nil?
    Docker::Util.build_auth_header(credentials)
  else
    {}
  end
  body = conn.post('/images/create', opts)
  id = Docker::Util.parse_json("[#{body.gsub(/}\s*{/, '},{')}]").last['id']
  new(conn, 'id' => id, :headers => headers)
end
get(id, opts = {}, conn = Docker.connection) click to toggle source

Return a specific image.

# File lib/docker/image.rb, line 111
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(file, options = {}, connection = Docker.connection) click to toggle source

Import an Image from the output of Docker::Container#export.

# File lib/docker/image.rb, line 132
def import(file, options = {}, connection = Docker.connection)
  File.open(file, 'r') do |io|
    body = connection.post(
      '/images/create',
       options.merge('fromSrc' => '-'),
       :headers => { 'Content-Type' => 'application/tar',
                     'Transfer-Encoding' => 'chunked' }
    ) { io.read(Excon.defaults[:chunk_size]).to_s }
    new(connection, 'id'=> Docker::Util.parse_json(body)['status'])
  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 125
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() click to toggle source
Alias for: remove
insert(query = {}) click to toggle source

Insert a file into the Image, returns a new Image that has that file.

# File lib/docker/image.rb, line 49
def insert(query = {})
  body = connection.post(path_for(:insert), query)
  if (id = body.match(/{"status":"([a-f0-9]+)"}\z/)).nil? || id[1].empty?
    raise UnexpectedResponseError, "Could not find Id in '#{body}'"
  else
    self.class.send(:new, connection, 'id' => id[1])
  end
end
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 60
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 24
def push(creds = nil, options = {})
  repository = self.info['RepoTags'].first.split(/:/)[0] rescue nil

  raise ArgumentError, "Image does not have a name to push." unless repository

  credentials = creds || Docker.creds
  headers = Docker::Util.build_auth_header(credentials)
  connection.post(
    "/images/#{repository}/push",
    options,
    :headers => headers
  )
  self
end
remove() click to toggle source

Remove the Image from the server.

# File lib/docker/image.rb, line 76
def remove
  connection.delete("/images/#{self.id}")
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 8
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
tag(opts = {}) click to toggle source

Tag the Image.

# File lib/docker/image.rb, line 40
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 82
def to_s
  "Docker::Image { :id => #{self.id}, :info => #{self.info.inspect}, "       ":connection => #{self.connection} }"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.