Class: Rascal::Docker::Container

Inherits:
Object
  • Object
show all
Includes:
IOHelper
Defined in:
lib/rascal/docker/container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IOHelper

#say, setup, #stderr, #stdin, #stdout

Constructor Details

#initialize(name, image) ⇒ Container

Returns a new instance of Container.



8
9
10
11
12
# File 'lib/rascal/docker/container.rb', line 8

def initialize(name, image)
  @name = name
  @prefixed_name = "#{NAME_PREFIX}#{name}"
  @image = image
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



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

def image
  @image
end

Instance Method Details

#cleanObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/rascal/docker/container.rb', line 93

def clean
  if running?
    say "Stopping container for #{@name}"
    stop_container
  end
  if exists?
    say "Removing container for #{@name}"
    remove_container
  end
end

#create(network: nil, network_alias: nil, volumes: [], env: {}, command: []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rascal/docker/container.rb', line 53

def create(network: nil, network_alias: nil, volumes: [], env: {}, command: [])
  @id = Docker.interface.run(
    'container',
    'create',
    '--name', @prefixed_name,
    *(volumes.flat_map { |v| ['-v', v.to_param] }),
    *env_args(env),
    *(['--network', network.id] if network),
    *(['--network-alias', network_alias] if network_alias),
    @image,
    *command,
    output: :id,
  )
end

#download_missingObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/rascal/docker/container.rb', line 14

def download_missing
  unless image_exists?
    say "Downloading image for #{@name}"
    Docker.interface.run(
      'pull',
      @image,
      stdout: stdout,
    )
  end
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  !!id
end

#idObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/rascal/docker/container.rb', line 115

def id
  @id ||= Docker.interface.run(
    'container',
    'ps',
    '--all',
    '--quiet',
    '--filter', "name=^/#{@prefixed_name}$",
    output: :id,
  )
end

#run_and_attach(*command, env: {}, network: nil, volumes: [], working_dir: nil, allow_failure: false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rascal/docker/container.rb', line 68

def run_and_attach(*command, env: {}, network: nil, volumes: [], working_dir: nil, allow_failure: false)
  Docker.interface.run_and_attach(
    'container',
    'run',
    '--rm',
    '-a', 'STDOUT',
    '-a', 'STDERR',
    '-a', 'STDIN',
    '--interactive',
    '--tty',
    *(['-w', working_dir] if working_dir),
    *(volumes.flat_map { |v| ['-v', v.to_param] }),
    *env_args(env),
    *(['--network', network.id] if network),
    @image,
    *command,
    redirect_io: {
      out: stdout,
      err: stderr,
      in: stdin,
    },
    allow_failure: allow_failure,
  )
end

#running?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rascal/docker/container.rb', line 25

def running?
  if id
    container_info = Docker.interface.run(
      'container',
      'inspect',
      id,
      output: :json,
    ).first
    !!container_info.dig('State', 'Running')
  else
    false
  end
end

#start(network: nil, network_alias: nil, volumes: [], env: {}, command: []) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/rascal/docker/container.rb', line 43

def start(network: nil, network_alias: nil, volumes: [], env: {}, command: [])
  say "Starting container for #{@name}"
  create(network: network, network_alias: network_alias, volumes: volumes, env: env, command: command) unless exists?
  Docker.interface.run(
    'container',
    'start',
    id,
  )
end

#update(skip: []) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/rascal/docker/container.rb', line 104

def update(skip: [])
  return if skip.include?(@image)
  say "Updating image #{@image}"
  Docker.interface.run(
    'pull',
    @image,
    stdout: stdout,
  )
  @image
end