Class: Docker::Compose::Session

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(shell = Backticks::Runner.new(buffered: [:stderr], interactive: true), dir: Dir.pwd, file: 'docker-compose.yml') ⇒ Session

Returns a new instance of Session.



24
25
26
27
28
29
# File 'lib/docker/compose/session.rb', line 24

def initialize(shell = Backticks::Runner.new(buffered: [:stderr], interactive: true),
               dir: Dir.pwd, file: 'docker-compose.yml')
  @shell = shell
  @dir = dir
  @file = file
end

Instance Attribute Details

#dirObject (readonly)

Working directory (determines compose project name); default is Dir.pwd



19
20
21
# File 'lib/docker/compose/session.rb', line 19

def dir
  @dir
end

#fileObject (readonly)

Project file; default is ‘docker-compose.yml’



22
23
24
# File 'lib/docker/compose/session.rb', line 22

def file
  @file
end

Instance Method Details

#build(*services, force_rm: false, no_cache: false, pull: false) ⇒ Object



201
202
203
204
205
206
# File 'lib/docker/compose/session.rb', line 201

def build(*services, force_rm: false, no_cache: false, pull: false)
  o = opts(force_rm: [force_rm, false],
           no_cache: [no_cache, false],
           pull: [pull, false])
  result = run!('build', services, o)
end

#config(*args) ⇒ Hash

Validate docker-compose file and return it as Hash

Returns:

  • (Hash)

    the docker-compose config file

Raises:

  • (Error)

    if command fails



34
35
36
37
# File 'lib/docker/compose/session.rb', line 34

def config(*args)
  config = run!('config', *args)
  YAML.load(config)
end

#downObject

Take the stack down



88
89
90
# File 'lib/docker/compose/session.rb', line 88

def down
  run!('down')
end

#kill(*services, signal: 'KILL') ⇒ Object

Forcibly stop running services.

Parameters:

  • services (Array)

    list of String service names to stop

  • name (String)

    of murderous signal to use, default is ‘KILL’

See Also:

  • for a list of acceptable signal names


149
150
151
152
# File 'lib/docker/compose/session.rb', line 149

def kill(*services, signal: 'KILL')
  o = opts(signal: [signal, 'KILL'])
  run!('kill', o, services)
end

#logs(*services) ⇒ true

Monitor the logs of one or more containers.

Parameters:

  • services (Array)

    list of String service names to show logs for

Returns:

  • (true)

    always returns true

Raises:

  • (Error)

    if command fails



43
44
45
46
# File 'lib/docker/compose/session.rb', line 43

def logs(*services)
  run!('logs', services)
  true
end

#pause(*services) ⇒ Object

Pause running services.

Parameters:

  • services (Array)

    list of String service names to run



126
127
128
# File 'lib/docker/compose/session.rb', line 126

def pause(*services)
  run!('pause', *services)
end

#port(service, port, protocol: 'tcp', index: 1) ⇒ String?

Figure out which interface(s) and port a given service port has been published to.

NOTE: if Docker Compose is communicating with a remote Docker host, this method returns IP addresses from the point of view of that host and its interfaces. If you need to know the address as reachable from localhost, you probably want to use Mapper.

Parameters:

  • service (String)

    name of service from docker-compose.yml

  • port (Integer)

    number of port

  • protocol (String) (defaults to: 'tcp')

    ‘tcp’ or ‘udp’

  • index (Integer) (defaults to: 1)

    of container (if multiple instances running)

Returns:

  • (String, nil)

    an ip:port pair such as “0.0.0.0:32176” or nil if the service is not running

Raises:

  • (Error)

    if command fails

See Also:



169
170
171
172
173
174
175
176
177
178
# File 'lib/docker/compose/session.rb', line 169

def port(service, port, protocol: 'tcp', index: 1)
  inter = @shell.interactive
  @shell.interactive = false

  o = opts(protocol: [protocol, 'tcp'], index: [index, 1])
  s = run!('port', o, service, port).strip
  (!s.empty? && s) || nil
ensure
  @shell.interactive = inter
end

#psObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/docker/compose/session.rb', line 48

def ps()
  inter = @shell.interactive
  @shell.interactive = false

  lines = run!('ps', q: true).split(/[\r\n]+/)
  containers = Collection.new

  lines.each do |id|
    containers << docker_ps(id)
  end

  containers
ensure
  @shell.interactive = inter
end

#pull(*services) ⇒ Object

Pull images of services

Parameters:

  • services (Array)

    list of String service names to pull



94
95
96
# File 'lib/docker/compose/session.rb', line 94

def pull(*services)
  run!('pull', *services)
end

#restart(*services, timeout: 10) ⇒ Object



119
120
121
122
# File 'lib/docker/compose/session.rb', line 119

def restart(*services, timeout:10)
  o = opts(timeout: [timeout, 10])
  run!('restart', o, *services)
end

#rm(*services, force: false, volumes: false) ⇒ Object



98
99
100
101
# File 'lib/docker/compose/session.rb', line 98

def rm(*services, force: false, volumes: false)
  o = opts(f: [force, false], v: [volumes, false])
  run!('rm', o, services)
end

#run(service, *cmd, detached: false, no_deps: false, env: [], rm: false) ⇒ Object

Idempotently run an arbitrary command with a service container.

Parameters:

  • service (String)

    name to run

  • cmd (String)

    command statement to run

  • detached (Boolean) (defaults to: false)

    if true, to start services in the background; otherwise, monitor logs in the foreground and shutdown on Ctrl+C

  • no_deps (Boolean) (defaults to: false)

    if true, just run specified services without running the services that they depend on

  • env (Array) (defaults to: [])

    a list of environment variables (see: -e flag)

  • rm (Boolean) (defaults to: false)

    remove the container when done

Raises:

  • (Error)

    if command fails



113
114
115
116
117
# File 'lib/docker/compose/session.rb', line 113

def run(service, *cmd, detached: false, no_deps: false, env: [], rm: false)
  o = opts(detached: [detached, false], no_deps: [no_deps, false], env: [env, []], rm: [rm, false])
  env_params = env.map { |v| { e: v } }
  run!('run', o, *env_params, 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.

Parameters:

  • args (Array)

    command-line arguments in the format accepted by Backticks::Runner#command

Returns:

  • (String)

    output of the command

Raises:

  • (Error)

    if command fails

See Also:

  • Docker::Compose::Shell#command


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/docker/compose/session.rb', line 217

def run!(*args)
  file_args = case @file
  when 'docker-compose.yml'
    []
  when Array
    # backticks sugar can't handle array values; build a list of hashes
    # IMPORTANT: preserve the order of the files so overrides work correctly
    file_args = @file.map { |filepath| { :file => filepath } }
  else
    # a single String (or Pathname, etc); use normal sugar to add it
    [{ file: @file.to_s }]
  end

  @shell.chdir = dir
  cmd = @shell.run('docker-compose', *file_args, *args).join
  status = cmd.status
  out = cmd.captured_output
  err = cmd.captured_error
  status.success? || fail(Error.new(args.first, status, out+err))
  out
end

#stop(*services, timeout: 10) ⇒ Object

Stop running services.

Parameters:

  • services (Array)

    list of String service names to stop

  • timeout (Integer) (defaults to: 10)

    how long to wait for each service to stop

Raises:

  • (Error)

    if command fails



140
141
142
143
# File 'lib/docker/compose/session.rb', line 140

def stop(*services, timeout: 10)
  o = opts(timeout: [timeout, 10])
  run!('stop', o, services)
end

#unpause(*services) ⇒ Object

Unpause running services.

Parameters:

  • services (Array)

    list of String service names to run



132
133
134
# File 'lib/docker/compose/session.rb', line 132

def unpause(*services)
  run!('unpause', *services)
end

#up(*services, detached: false, timeout: 10, build: false, no_build: false, no_deps: false) ⇒ true

Idempotently up the given services in the project.

Parameters:

  • services (Array)

    list of String service names to run

  • detached (Boolean) (defaults to: false)

    if true, to start services in the background; otherwise, monitor logs in the foreground and shutdown on Ctrl+C

  • timeout (Integer) (defaults to: 10)

    how long to wait for each service to start

  • build (Boolean) (defaults to: false)

    if true, build images before starting containers

  • no_build (Boolean) (defaults to: false)

    if true, don’t build images, even if they’re missing

  • no_deps (Boolean) (defaults to: false)

    if true, just run specified services without running the services that they depend on

Returns:

  • (true)

    always returns true

Raises:

  • (Error)

    if command fails



76
77
78
79
80
81
82
83
84
85
# File 'lib/docker/compose/session.rb', line 76

def up(*services,
       detached: false, timeout: 10, build: false, no_build: false, no_deps: false)
  o = opts(d: [detached, false],
           timeout: [timeout, 10],
           build: [build, false],
           no_build: [no_build, false],
           no_deps: [no_deps, false])
  run!('up', o, services)
  true
end

#version(short: false) ⇒ String, Hash

Determine the installed version of docker-compose.

Parameters:

  • short (Boolean) (defaults to: false)

    whether to return terse version information

Returns:

  • (String, Hash)

    if short==true, returns a version string; otherwise, returns a Hash of component-name strings to version strings

Raises:

  • (Error)

    if command fails



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/docker/compose/session.rb', line 185

def version(short: false)
  o = opts(short: [short, false])
  result = run!('version', o, 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