Class: Docker::Exec

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/docker/exec.rb

Overview

This class represents a Docker Exec Instance.

Instance Attribute Summary

Attributes included from Base

#connection, #id, #info

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#initialize, #normalize_hash

Class Method Details

.create(options = {}, conn = Docker.connection) ⇒ Docker::Exec

Create a new Exec instance in a running container. Please note, this does NOT execute the instance - you must run #start. Also, each instance is one-time use only.



20
21
22
23
24
25
26
# File 'lib/docker/exec.rb', line 20

def self.create(options = {}, conn = Docker.connection)
  container = options.delete('Container')
  resp = conn.post("/containers/#{container}/exec", {},
    body: MultiJson.dump(options))
  hash = Docker::Util.parse_json(resp) || {}
  new(conn, hash)
end

Instance Method Details

#jsonObject

Get info about the Exec instance



30
31
32
# File 'lib/docker/exec.rb', line 30

def json
  Docker::Util.parse_json(connection.get(path_for(:json), {}))
end

#resize(query = {}) ⇒ Docker::Exec

Resize the TTY associated with the Exec instance

Options Hash (query):

  • h (Fixnum)

    Height of the TTY

  • w (Fixnum)

    Width of the TTY



92
93
94
95
# File 'lib/docker/exec.rb', line 92

def resize(query = {})
  connection.post(path_for(:resize), query)
  self
end

#start!(options = {}, &block) ⇒ Array, Int

Start the Exec instance. The Exec instance is deleted after this so this command can only be run once.

Options Hash (options):

  • :stdin (Object) — default: nil

    The object to pass to STDIN.

  • :detach (TrueClass, FalseClass) — default: false

    Whether to attach to STDOUT/STDERR.

  • :tty (TrueClass, FalseClass) — default: false

    Whether to attach using a pseudo-TTY.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/docker/exec.rb', line 45

def start!(options = {}, &block)

  # Parse the Options
  tty = !!options.delete(:tty)
  detached = !!options.delete(:detach)
  stdin = options[:stdin]
  read_timeout = options[:wait]

  # Create API Request Body
  body = MultiJson.dump(
    'Tty' => tty,
    'Detach' => detached
  )
  excon_params = { body: body }

  msgs = Docker::Messages.new
  unless detached
    if stdin
      excon_params[:hijack_block] = Docker::Util.hijack_for(stdin, block,
        msgs, tty)
    else
      excon_params[:response_block] = Docker::Util.attach_for(block,
        msgs, tty)
    end
  end

  excon_params[:read_timeout] = read_timeout unless read_timeout.nil?

  connection.post(path_for(:start), nil, excon_params)
  [msgs.stdout_messages, msgs.stderr_messages, self.json['ExitCode']]
end

#to_sString

Convert details about the object into a string



8
9
10
# File 'lib/docker/exec.rb', line 8

def to_s
  "Docker::Exec { :id => #{self.id}, :connection => #{self.connection} }"
end