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.

Parameters:

  • options (Hash) (defaults to: {})

    Parameters to pass in to the API.

  • conn (Docker::Connection) (defaults to: Docker.connection)

    Connection to Docker Remote API

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/docker/exec.rb', line 22

def self.create(options = {}, conn = Docker.connection)
  container = options.delete('Container')

  # Podman does not attach these by default but does require them to be attached
  if ::Docker.podman?(conn)
    options['AttachStderr'] = true if options['AttachStderr'].nil?
    options['AttachStdout'] = true if options['AttachStdout'].nil?
  end

  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



39
40
41
# File 'lib/docker/exec.rb', line 39

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

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

Resize the TTY associated with the Exec instance

Parameters:

  • query (Hash) (defaults to: {})

    API query parameters

Options Hash (query):

  • h (Fixnum)

    Height of the TTY

  • w (Fixnum)

    Width of the TTY

Returns:



101
102
103
104
# File 'lib/docker/exec.rb', line 101

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.

Parameters:

  • options (Hash) (defaults to: {})

    Options to dictate behavior of the instance

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.

Returns:

  • (Array, Array, Int)

    The STDOUT, STDERR and exit code



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/docker/exec.rb', line 54

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

Returns:

  • (String)

    String representation of the Exec instance object



10
11
12
# File 'lib/docker/exec.rb', line 10

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