Class: Docker::Session
- Inherits:
-
Object
- Object
- Docker::Session
- Defined in:
- lib/docker/session.rb
Overview
A Ruby OOP interface to a docker session. A session is bound to a particular docker host (which is set at initialize time) time) and invokes whichever docker command is resident in $PATH.
Run docker commands by calling instance methods of this class and passing positional and kwargs that are equivalent to the CLI options you would pass to the command-line tool.
Note that the Ruby command methods usually expose a subset of the options allowed by the docker CLI, and that options are sometimes renamed for clarity. Each command method is extensively documented.
Constant Summary collapse
- PS_HEADER =
Hint that we are able to parse ps output
/ID\s+IMAGE\s+COMMAND\s+CREATED\s+STATUS\s+PORTS\s+NAMES$/
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
URL of the Docker host associated with this session.
- #shell ⇒ #command readonly
Instance Method Summary collapse
-
#initialize(shell = Backticks::Runner.new(cli: Docker::CLI::Getopt), host: ENV['DOCKER_HOST']) ⇒ Session
constructor
A new instance of Session.
-
#inspect(container_s) ⇒ Container, Array
Get detailed information about a container(s).
-
#kill(container, signal: nil) ⇒ Object
Kill a running container.
-
#ps(all: false, before: nil, latest: false, since: nil) ⇒ Array
List containers.
-
#rm(container, force: false, volumes: false) ⇒ Object
Remove a container.
-
#run(image, *command_and_args, add_host: [], attach: [], cpu_period: nil, cpu_quota: nil, detach: false, env: {}, env_file: nil, expose: [], hostname: nil, interactive: false, link: [], memory: nil, name: nil, publish: [], publish_all: false, restart: false, rm: false, tty: false, user: nil, volume: [], volumes_from: nil) {|stream, data| ... } ⇒ Object
Run a command in a new container.
-
#run!(*args) {|stream, data| ... } ⇒ String
Run a docker command without validating that the CLI parameters make sense.
-
#start(container, attach: false, interactive: false) ⇒ Object
Start a stopped container.
-
#stop(container, time: nil) ⇒ Object
Stop a running container.
-
#version ⇒ Hash
Provide version information about the Docker client and server.
Constructor Details
#initialize(shell = Backticks::Runner.new(cli: Docker::CLI::Getopt), host: ENV['DOCKER_HOST']) ⇒ Session
Returns a new instance of Session.
26 27 28 29 |
# File 'lib/docker/session.rb', line 26 def initialize(shell=Backticks::Runner.new(cli: Docker::CLI::Getopt), host:ENV['DOCKER_HOST']) @host = host @shell = shell end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns URL of the Docker host associated with this session.
18 19 20 |
# File 'lib/docker/session.rb', line 18 def host @host end |
#shell ⇒ #command (readonly)
21 22 23 |
# File 'lib/docker/session.rb', line 21 def shell @shell end |
Instance Method Details
#inspect(container_s) ⇒ Container, Array
Get detailed information about a container(s).
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/docker/session.rb', line 35 def inspect(container_s) containers = container_s containers = [containers] unless container_s.is_a?(Array) return [] if containers.empty? out = run!('inspect', containers) result = JSON.parse(out).map { |c| Container.new(c, session:self)} if container_s.is_a?(Array) result else result.first end end |
#kill(container, signal: nil) ⇒ Object
Kill a running container.
52 53 54 |
# File 'lib/docker/session.rb', line 52 def kill(container, signal:nil) run!('kill', {signal:signal}, container) end |
#ps(all: false, before: nil, latest: false, since: nil) ⇒ Array
List containers. This actually does a ‘ps` followed by a very large `inspect`, so it’s expensive but super detailed.
61 62 63 64 65 66 67 |
# File 'lib/docker/session.rb', line 61 def ps(all:false, before:nil, latest:false, since:nil) out = run!('ps', all:all,before:before,latest:latest,since:since) lines = out.split(/[\n\r]+/) header = lines.shift ids = lines.map { |line| line.split(/\s+/).first } inspect(ids) end |
#rm(container, force: false, volumes: false) ⇒ Object
Remove a container.
161 162 163 |
# File 'lib/docker/session.rb', line 161 def rm(container, force:false, volumes:false) run!('rm', {force:force,volumes:volumes},container).strip end |
#run(image, *command_and_args, add_host: [], attach: [], cpu_period: nil, cpu_quota: nil, detach: false, env: {}, env_file: nil, expose: [], hostname: nil, interactive: false, link: [], memory: nil, name: nil, publish: [], publish_all: false, restart: false, rm: false, tty: false, user: nil, volume: [], volumes_from: nil) {|stream, data| ... } ⇒ Object
Run a command in a new container.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/docker/session.rb', line 108 def run(image, *command_and_args, add_host:[], attach:[], cpu_period:nil, cpu_quota:nil, detach:false, env:{}, env_file:nil, expose:[], hostname:nil, interactive:false, link:[], memory:nil, name:nil, publish:[], publish_all:false, restart:false, rm:false, tty:false, user:nil, volume:[], volumes_from:nil, &block) cmd = [] # if env was provided as a hash, turn it into an array env = env.map { |k, v| "#{k}=#{v}" } if env.is_a?(Hash) # our keyword args are formatted properly for run! to handle them; echo # them into the command line verbatim. # TODO find a way to DRY out this repetitive mess... cmd << {add_host:add_host, attach:attach, cpu_period:cpu_period, cpu_quota:cpu_quota, detach:detach, env_file:env_file, env:env, expose:expose, hostname:hostname, interactive:interactive, link:link, memory:memory, name:name, publish:publish, publish_all:publish_all, restart:restart, rm:rm, tty:tty, user:user, volume:volume, volumes_from:volumes_from }.reject { |k, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) } # after the options come the image and command cmd << image cmd.concat(command_and_args) # return the output of `docker run` minus extra whitespace run!('run', *cmd, &block).strip end |
#run!(*args) {|stream, data| ... } ⇒ String
Run a docker command without validating that the CLI parameters make sense. Prepend implicit options if suitable.
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/docker/session.rb', line 219 def run!(*args, &block) # STDERR.puts "+ " + (['docker'] + args).inspect cmd = @shell.run('docker', *args) cmd.tap(&block) if block_given? cmd.join status, out, err = cmd.status, cmd.captured_output, cmd.captured_error status.success? || raise(Error.new(args.first, status, err)) out end |
#start(container, attach: false, interactive: false) ⇒ Object
Start a stopped container.
178 179 180 |
# File 'lib/docker/session.rb', line 178 def start(container, attach:false, interactive:false) run!('start', {attach:attach,interactive:interactive}, container).strip end |
#stop(container, time: nil) ⇒ Object
Stop a running container.
169 170 171 |
# File 'lib/docker/session.rb', line 169 def stop(container, time:nil) run!('stop', {time:time}, container).strip end |
#version ⇒ Hash
Provide version information about the Docker client and server.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/docker/session.rb', line 186 def version result = run!('version') lines = result.split(/[\r\n]+/) info = {} prefix = '' lines.each do |line| if line =~ /^Client/ prefix = 'Client ' elsif line =~ /^Server/ prefix = 'Server ' else pair = line.split(':',2).map { |e| e.strip } info["#{prefix}#{pair[0]}"] = pair[1] end end info end |