class Dockerbash::Client

Public Class Methods

new() click to toggle source
# File lib/dockerbash.rb, line 20
def initialize()
  banner
  @docker_path = find_docker()
end

Public Instance Methods

select_docker_container() click to toggle source
# File lib/dockerbash.rb, line 25
def select_docker_container()
  counter = 0
  docker_ids = []
  ids = get_docker_containers_ids()
  ids.each do |i|
    cn_stdout, cn_stderr, cn_status = Open3.capture3("#{@docker_path} inspect --format={{.Name}} #{i}")
    container_name = cn_stdout.gsub(/\n/,'')
    container_name.delete!("/")

    ci_stdout, ci_stderr, ci_status = Open3.capture3("#{@docker_path} inspect --format={{.NetworkSettings.IPAddress}} #{i}")
    container_ip = ci_stdout.gsub(/\n/,'')
    container_ip.delete("/")

    cb_stdout, cb_stderr, cb_status = Open3.capture3("#{@docker_path} inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' #{i}")
    container_bindings = cb_stdout.gsub(/\n/,'')

    container_ids = i
    puts "#{counter}. Container:#{container_name}\tIp:#{container_ip}\tPorts:#{container_bindings}"
    docker_ids << container_ids
    counter += 1
  end

  selected_id = ask("Container? ", Integer) { |q| q.in = 0..docker_ids.count }
  command = "#{@docker_path} exec -t -i #{docker_ids[selected_id]} /bin/bash"
  exec(command)
end

Private Instance Methods

banner() click to toggle source
find_docker() click to toggle source
# File lib/dockerbash.rb, line 65
def find_docker()
  docker_path = find_executable('docker', path = nil)
  if docker_path.nil? || docker_path.empty?
    msg = " -- Docker not found, is the docker package installed?".red
    abort(msg)
  else
    return docker_path
  end
end
get_docker_containers_ids() click to toggle source
# File lib/dockerbash.rb, line 75
def get_docker_containers_ids()
  docker_containers_ids = []
  stdout, stdeerr, status = Open3.capture3("#{@docker_path} ps -aq -f status=running")
  if stdout.nil? || stdout.empty?
    msg = "  -- Is the Docker daemon running and the containers up?".red
    abort(msg)
  else
    concat_stdout = stdout.gsub(/\n/,' ')
    docker_containers_ids = concat_stdout.split(" ")
    return docker_containers_ids
  end
end