Class: Dockit::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/dockit/container.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Container

Returns a new instance of Container.



33
34
35
36
# File 'lib/dockit/container.rb', line 33

def initialize(options)
  @tty = options['Tty']
  @container = Docker::Container.create(options)
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



3
4
5
# File 'lib/dockit/container.rb', line 3

def container
  @container
end

Class Method Details

.clean(force: false) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/dockit/container.rb', line 10

def clean(force: false)
  list(
    all: force,
    filters: force ? nil : {status: [:exited]}
  ).each do |container|
    puts "  #{container.id}"
    container.delete(force: true, v: force)
  end
end

.find(name: nil, id: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dockit/container.rb', line 20

def find(name: nil, id: nil)
  unless name || id
    STDERR.puts "Must specify name or id"
    exit 1
  end
  list().find do |container|
    name && container.info['Names'].include?(name) ||
      id && container.id == id
  end

end

.list(all: false, filters: nil) ⇒ Object



6
7
8
# File 'lib/dockit/container.rb', line 6

def list(all: false, filters: nil)
  Docker::Container.all(all: all, filters: JSON.dump(filters))
end

Instance Method Details

#destroyObject



57
58
59
60
# File 'lib/dockit/container.rb', line 57

def destroy
  puts "Deleting container #{container.id}"
  container.delete(force: true, v: true)
end

#start(options = {}, verbose: true, transient: false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dockit/container.rb', line 38

def start(options={}, verbose: true, transient: false)
  container.start!(options)
  if transient
    if @tty
      trap("INT") {}
      STDIN.raw!
    end
    container.attach(tty: @tty, stdin: @tty ? STDIN : nil) do |*args|
      if @tty then
        print args[0]
      else
        msg(*args)
      end
    end
    STDIN.cooked!
    destroy
  end
end