class Rascal::Docker::Container
Attributes
image[R]
Public Class Methods
new(name, image)
click to toggle source
# File lib/rascal/docker/container.rb, line 8 def initialize(name, image) @name = name @prefixed_name = "#{NAME_PREFIX}#{name}" @image = image end
Public Instance Methods
clean()
click to toggle source
# File lib/rascal/docker/container.rb, line 92 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: {})
click to toggle source
# File lib/rascal/docker/container.rb, line 53 def create(network: nil, network_alias: nil, volumes: [], env: {}) @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, output: :id, ) end
download_missing()
click to toggle source
# 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?()
click to toggle source
# File lib/rascal/docker/container.rb, line 39 def exists? !!id end
run_and_attach(*command, env: {}, network: nil, volumes: [], working_dir: nil, allow_failure: false)
click to toggle source
# File lib/rascal/docker/container.rb, line 67 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?()
click to toggle source
# 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: {})
click to toggle source
# File lib/rascal/docker/container.rb, line 43 def start(network: nil, network_alias: nil, volumes: [], env: {}) say "Starting container for #{@name}" create(network: network, network_alias: network_alias, volumes: volumes, env: env) unless exists? Docker.interface.run( 'container', 'start', id, ) end
update(skip: [])
click to toggle source
# File lib/rascal/docker/container.rb, line 103 def update(skip: []) return if skip.include?(@image) say "Updating image #{@image}" Docker.interface.run( 'pull', @image, stdout: stdout, ) @image end
Private Instance Methods
env_args(env)
click to toggle source
# File lib/rascal/docker/container.rb, line 153 def env_args(env) env.flat_map { |key, value| ['-e', "#{key}=#{value}"] } end
id()
click to toggle source
# File lib/rascal/docker/container.rb, line 116 def id @id ||= Docker.interface.run( 'container', 'ps', '--all', '--quiet', '--filter', "name=^/#{@prefixed_name}$", output: :id, ) end
image_exists?()
click to toggle source
# File lib/rascal/docker/container.rb, line 127 def image_exists? Docker.interface.run( 'image', 'inspect', @image, output: :json, allow_failure: true, ).first end
remove_container()
click to toggle source
# File lib/rascal/docker/container.rb, line 145 def remove_container Docker.interface.run( 'container', 'rm', id, ) end
stop_container()
click to toggle source
# File lib/rascal/docker/container.rb, line 137 def stop_container Docker.interface.run( 'container', 'stop', id, ) end