Class: Dev::Docker::Compose

Inherits:
Object show all
Defined in:
lib/firespring_dev_commands/docker/compose.rb

Overview

Class containing methods for interfacing with the docker compose cli

Defined Under Namespace

Classes: Config

Constant Summary collapse

EXECUTABLE_NAME =

The name of the docker compose executable

%w(docker compose).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compose_files: self.class.config.compose_files, environment: [], options: [], project_dir: self.class.config.project_dir, project_name: self.class.config.project_name, services: [], user: nil, volumes: [], capture: false, running_silent: false) ⇒ Compose

Returns a new instance of Compose.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/firespring_dev_commands/docker/compose.rb', line 46

def initialize(
  compose_files: self.class.config.compose_files,
  environment: [],
  options: [],
  project_dir: self.class.config.project_dir,
  project_name: self.class.config.project_name,
  services: [],
  user: nil,
  volumes: [],
  capture: false,
  running_silent: false
)
  @compose_files = Array(compose_files)
  @environment = environment
  @options = Array(options)
  @project_dir = project_dir
  @project_name = project_name
  @services = Array(services)
  @user = user
  @volumes = Array(volumes)
  @capture = capture
  @running_silent = running_silent
  check_version
end

Instance Attribute Details

#captureObject

Returns the value of attribute capture.



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

def capture
  @capture
end

#compose_filesObject

Returns the value of attribute compose_files.



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

def compose_files
  @compose_files
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#project_dirObject

Returns the value of attribute project_dir.



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

def project_dir
  @project_dir
end

#project_nameObject

Returns the value of attribute project_name.



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

def project_name
  @project_name
end

#running_silentObject

Returns the value of attribute running_silent.



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

def running_silent
  @running_silent
end

#servicesObject

Returns the value of attribute services.



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

def services
  @services
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

#volumesObject

Returns the value of attribute volumes.



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

def volumes
  @volumes
end

Class Method Details

.config {|@config| ... } ⇒ Object Also known as: configure

Instantiates a new top level config object if one hasn’t already been created Yields that config object to any given block Returns the resulting config object

Yields:



28
29
30
31
32
# File 'lib/firespring_dev_commands/docker/compose.rb', line 28

def config
  @config ||= Config.new
  yield(@config) if block_given?
  @config
end

.versionObject

Returns the version of the docker-compose executable on the system



38
39
40
41
# File 'lib/firespring_dev_commands/docker/compose.rb', line 38

def version
  version_cmd = EXECUTABLE_NAME.dup << 'version'
  @version ||= `#{version_cmd.join(' ')}`.match(/version v?([0-9.]+)/)[1]
end

Instance Method Details

#buildObject

Pull in supported env settings and call build Specify PULL=true to force compose to pull all backing images as part of the build Specify NO_CACHE=true to force compose to build from scratch rather than using build cache



85
86
87
88
89
90
91
# File 'lib/firespring_dev_commands/docker/compose.rb', line 85

def build
  merge_options('--parallel')
  merge_env_pull_option
  merge_env_push_option
  merge_env_cache_option
  execute_command(build_command('build'))
end

#check_versionObject

Checks the min and max version against the current docker version if they have been configured



72
73
74
75
76
77
78
79
80
# File 'lib/firespring_dev_commands/docker/compose.rb', line 72

def check_version
  min_version = self.class.config.min_version
  version_too_low = min_version && !Dev::Common.new.version_greater_than(min_version, self.class.version)
  raise "requires docker compose version >= #{min_version} (found #{self.class.version})" if version_too_low

  max_version = self.class.config.max_version
  version_too_high = max_version && Dev::Common.new.version_greater_than(max_version, self.class.version)
  raise "requires docker compose version < #{max_version} (found #{self.class.version})" if version_too_high
end

#container_by_name(service_name, prefix = nil, status: [Docker::Status::RUNNING]) ⇒ Object

Get the first container matching the given name If prefix is specified then this method will filter for compose services in the given project only If status is specified then this method will filter containers in the given status only



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/firespring_dev_commands/docker/compose.rb', line 159

def container_by_name(service_name, prefix = nil, status: [Docker::Status::RUNNING])
  prefix ||= project_name
  containers = ::Docker::Container.all(filters: {status: Array(status), label: ["com.docker.compose.service=#{service_name}"]}.to_json)
  containers.each do |container|
    container&.info&.dig('Names')&.each do |name|
      return container if name.start_with?("/#{prefix}")
    end
  end

  raise "Container not found for #{service_name} with prefix #{prefix}"
end

#downObject

Pull in supported env settings and call down Specify REMOVE_VOLUMES=true to also remove any unused volumes when the containers are stopped



121
122
123
124
# File 'lib/firespring_dev_commands/docker/compose.rb', line 121

def down
  merge_env_volumes_option
  execute_command(build_command('down'))
end

#exec(*args) ⇒ Object

Call the compose exec method passing the given args after it



137
138
139
# File 'lib/firespring_dev_commands/docker/compose.rb', line 137

def exec(*args)
  execute_command(build_command('exec', *args))
end

#logsObject

Pull in supported env settings and call logs Specify NO_FOLLOW=true if you want to print current logs and exist Specify TAIL to pass tail options to the logs command



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

def logs
  merge_env_follow_option
  merge_env_tail_option
  execute_command(build_command('logs'))
end

#mapped_public_port(service_name, private_port) ⇒ Object

Gets the dynamic port which was assigned to the compose service on the original private port



172
173
174
175
176
# File 'lib/firespring_dev_commands/docker/compose.rb', line 172

def mapped_public_port(service_name, private_port)
  container = container_by_name(service_name)
  port_mapping = container.info['Ports'].find { |it| it['PrivatePort'] == private_port }
  port_mapping['PublicPort']
end

#pullObject

Call the compose pull method



152
153
154
# File 'lib/firespring_dev_commands/docker/compose.rb', line 152

def pull
  execute_command(build_command('pull'))
end

#pushObject

Call the compose push method



147
148
149
# File 'lib/firespring_dev_commands/docker/compose.rb', line 147

def push
  execute_command(build_command('push'))
end

#restartObject

Pull in supported env settings and call restart



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

def restart
  execute_command(build_command('restart'))
end

#run(*args) ⇒ Object

Call the compose run method passing the given args after it



142
143
144
# File 'lib/firespring_dev_commands/docker/compose.rb', line 142

def run(*args)
  execute_command(build_command('run', *args))
end

#sh(shell_commands = ['bash']) ⇒ Object

Exec into a running container and run the given shell commands Default to running ‘bash’ which will start a terminal in the running container



106
107
108
# File 'lib/firespring_dev_commands/docker/compose.rb', line 106

def sh(shell_commands = ['bash'])
  execute_command(build_command('exec', *shell_commands))
end

#stopObject

Pull in supported env settings and call stop



127
128
129
# File 'lib/firespring_dev_commands/docker/compose.rb', line 127

def stop
  execute_command(build_command('stop'))
end

#upObject

Pull in supported env settings and call up Specify BUILD=true to force/allow service builds before startup Specify NO_DEPS=true to only start the given service and ignore starting it’s dependencies Specify DETACHED=false to not detach from the started processes



97
98
99
100
101
102
# File 'lib/firespring_dev_commands/docker/compose.rb', line 97

def up
  merge_env_build_option
  merge_env_deps_option
  merge_env_detach_option
  execute_command(build_command('up'))
end