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) ⇒ 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
# 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
)
  @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
  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

#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



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

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



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

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



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

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



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

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



135
136
137
# File 'lib/firespring_dev_commands/docker/compose.rb', line 135

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



111
112
113
114
115
# File 'lib/firespring_dev_commands/docker/compose.rb', line 111

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



170
171
172
173
174
# File 'lib/firespring_dev_commands/docker/compose.rb', line 170

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



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

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

#pushObject

Call the compose push method



145
146
147
# File 'lib/firespring_dev_commands/docker/compose.rb', line 145

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

#restartObject

Pull in supported env settings and call restart



130
131
132
# File 'lib/firespring_dev_commands/docker/compose.rb', line 130

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

#run(*args) ⇒ Object

Call the compose run method passing the given args after it



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

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



104
105
106
# File 'lib/firespring_dev_commands/docker/compose.rb', line 104

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

#stopObject

Pull in supported env settings and call stop



125
126
127
# File 'lib/firespring_dev_commands/docker/compose.rb', line 125

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



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

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