lib/eac_docker/container.rb in eac_docker-0.1.0 vs lib/eac_docker/container.rb in eac_docker-0.2.0
- old
+ new
@@ -6,18 +6,36 @@
class Container
enable_immutable
immutable_accessor :interactive, :temporary, :tty, type: :boolean
immutable_accessor :env, type: :hash
immutable_accessor :command_arg, :volume, type: :array
+ attr_reader :id
common_constructor :image
def immutable_constructor_args
[image]
end
alias immutable_volume volume
+ def hostname
+ ::EacDocker::Executables.docker.command(
+ 'inspect', '--format={{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}',
+ id
+ ).execute!.strip
+ end
+
+ def on_detached
+ command = ::EacDocker::Executables.docker.command(*(%w[run --detach] + run_command_args))
+ self.id = command.execute!.strip
+ begin
+ yield(self)
+ ensure
+ stop
+ end
+ end
+
def volume(left_part, right_part = null)
immutable_volume(right_part.if_present(left_part) { |v| "#{left_part}:#{v}" })
end
def run_command
@@ -27,10 +45,16 @@
def run_command_args
run_command_boolean_args + run_command_envs_args + run_command_volumes_args + [image.id] +
command_args
end
+ def stop
+ ::EacDocker::Executables.docker.command('stop', id).execute!
+ end
+
private
+
+ attr_writer :id
def run_command_boolean_args
r = []
r << '--interactive' if interactive?
r << '--tty' if tty?