module Docker::Porcelain::Container

Attributes

last_exitstatus[R]

@return [Fixnum] exit status of the last command ran with {#system}

or {#`}

Public Instance Methods

`(command) click to toggle source

Execute a command in the container, returning the output @param [String] command the command line to execute,

passed to +/bin/sh -c+

@return [String] the standard output @raise (Docker::Porcelain::CommandError) if there’s a problem executing

the command (ie. the shell returns 127)

@note The exit code is available in {#last_exitstatus}. @see system

# File lib/docker/porcelain/container.rb, line 36
def ` command
  # unweird syntax highlighters: `
  stdout, stderr, status = self.exec ['/bin/sh', '-c', command]
  case @last_exitstatus = status
  when 127
    fail CommandError, stderr.join
  else
    stdout.join
  end
end
Also aliased as: backtick
backtick(command)
Alias for: `
delete!() click to toggle source

Force delete a container (even if it is running) @return [void]

# File lib/docker/porcelain/container.rb, line 91
def delete!
  delete force: true
end
system(*args) click to toggle source

Execute a command in the container, discarding the output @overload system(command…)

@param [String] command the command line to execute;
  passed to +/bin/sh -c+
@param [String...] command... the command to execute and its arguments

@return [true] if the command exits with zero exit status. @return [false] if the command exits with a nonzero status. @return [nil] if the command execution fails. @note The exit code is available in {#last_exitstatus}. @see #‘ @raise [NotImplementedError] if the argument list contains options

or environments (as allowed by {Kernel#system}).
# File lib/docker/porcelain/container.rb, line 61
def system *args
  env = args.shift if args.first.is_a? Hash
  opts = args.pop if args.last.is_a? Hash

  cmdline = args

  raise NotImplementedError, "setting env or opts is not implemented" \
      if [env, opts].any?

  fail NotImplementedError, 'overriding argv0 is not implemented' \
      if cmdline.first.is_a? Array

  if cmdline.length == 1
    cmdline = ['/bin/sh', '-c', cmdline.first]
  end

  _stdout, _stderr, status = self.exec cmdline

  @last_exitstatus = status

  case status
  when 127
    nil
  else
    status == 0
  end
end
write(path, content) click to toggle source

Write to a file in the container @note Executes tee internally, which has to be available inside the

container.

@param path [String] full path of the file to write @param content [String] data to be written @return [Fixnum] number of bytes written

# File lib/docker/porcelain/container.rb, line 16
def write path, content
  stdout, stderr, code = exec ['tee', path], stdin: StringIO.new(content)
  case code
  when 0
    stdout.map(&:length).inject :+
  when 127
    raise MisconfiguredContainerError, "error when trying to call tee: " + (stdout + stderr).join
  else
    raise CommandError, (stdout + stderr).join
  end
end