Class: Docker::Compose::Session
- Inherits:
-
Object
- Object
- Docker::Compose::Session
- Defined in:
- lib/docker/compose/session.rb
Overview
A Ruby OOP interface to a docker-compose session. A session is bound to a particular directory and docker-compose file (which are set at initialize time) and invokes whichever docker-compose command is resident in $PATH.
Run docker-compose commands by calling instance methods of this class and passing 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-compose CLI, and that options are sometimes renamed for clarity, e.g. the “-d” flag always becomes the “detached:” kwarg.
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#initialize(shell = Backticks::Runner.new(interactive:true), dir: Dir.pwd, file: 'docker-compose.yml') ⇒ Session
constructor
A new instance of Session.
-
#logs(*services) ⇒ true
Monitor the logs of one or more containers.
-
#port(service, port, protocol: 'tcp', index: 1) ⇒ Object
Figure out which host a port a given service port has been published to.
-
#run(service, *cmd, detached: false, no_deps: false, env_vars: [], rm: false) ⇒ Object
Idempotently run a service in the project.
-
#run!(*args) ⇒ String
Run a docker-compose command without validating that the CLI parameters make sense.
-
#stop(*services, timeout: 10) ⇒ Object
Stop running services.
-
#up(*services, detached: false, timeout: 10, no_build: false, no_deps: false) ⇒ true
Idempotently up the given services in the project.
-
#version(short: false) ⇒ String, Hash
Determine the installed version of docker-compose.
Constructor Details
#initialize(shell = Backticks::Runner.new(interactive:true), dir: Dir.pwd, file: 'docker-compose.yml') ⇒ Session
18 19 20 21 22 23 |
# File 'lib/docker/compose/session.rb', line 18 def initialize(shell=Backticks::Runner.new(interactive:true), dir:Dir.pwd, file:'docker-compose.yml') @shell = shell @dir = dir @file = file end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
16 17 18 |
# File 'lib/docker/compose/session.rb', line 16 def dir @dir end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
16 17 18 |
# File 'lib/docker/compose/session.rb', line 16 def file @file end |
Instance Method Details
#logs(*services) ⇒ true
Monitor the logs of one or more containers.
29 30 31 32 |
# File 'lib/docker/compose/session.rb', line 29 def logs(*services) run!('logs', services) true end |
#port(service, port, protocol: 'tcp', index: 1) ⇒ Object
Figure out which host a port a given service port has been published to.
83 84 85 |
# File 'lib/docker/compose/session.rb', line 83 def port(service, port, protocol:'tcp', index:1) run!('port', {protocol:protocol, index:index}, service, port) end |
#run(service, *cmd, detached: false, no_deps: false, env_vars: [], rm: false) ⇒ Object
Idempotently run a service in the project.
63 64 65 66 67 |
# File 'lib/docker/compose/session.rb', line 63 def run(service, *cmd, detached:false, no_deps:false, env_vars:[], rm:false) formated_vars = env_vars.map{|v| {e: v}} run!('run', {d:detached, no_deps:no_deps, rm:rm}, *formated_vars, service, cmd) end |
#run!(*args) ⇒ String
Run a docker-compose command without validating that the CLI parameters make sense. Prepend project and file options if suitable.
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/docker/compose/session.rb', line 116 def run!(*args) project_opts = { file: @file } Dir.chdir(@dir) do cmd = @shell.run('docker-compose', project_opts, *args).join status, out, err = cmd.status, cmd.captured_output, cmd.captured_error status.success? || raise(Error.new(args.first, status, err)) out end end |
#stop(*services, timeout: 10) ⇒ Object
Stop running services.
73 74 75 |
# File 'lib/docker/compose/session.rb', line 73 def stop(*services, timeout:10) run!('stop', {timeout:timeout}, services) end |
#up(*services, detached: false, timeout: 10, no_build: false, no_deps: false) ⇒ true
Idempotently up the given services in the project.
45 46 47 48 49 50 51 |
# File 'lib/docker/compose/session.rb', line 45 def up(*services, detached:false, timeout:10, no_build:false, no_deps:false) run!('up', {d:detached, timeout:timeout, no_build:no_build, no_deps:no_deps}, services) true end |
#version(short: false) ⇒ String, Hash
Determine the installed version of docker-compose.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/docker/compose/session.rb', line 92 def version(short:false) result = run!('version', short:short, file:false, dir:false) if short result.strip else lines = result.split(/[\r\n]+/) lines.inject({}) do |h, line| kv = line.split(/: +/, 2) h[kv.first] = kv.last h end end end |