Class: Dev::Docker::Compose
- 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
-
#capture ⇒ Object
Returns the value of attribute capture.
-
#compose_files ⇒ Object
Returns the value of attribute compose_files.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#options ⇒ Object
Returns the value of attribute options.
-
#project_dir ⇒ Object
Returns the value of attribute project_dir.
-
#project_name ⇒ Object
Returns the value of attribute project_name.
-
#services ⇒ Object
Returns the value of attribute services.
-
#user ⇒ Object
Returns the value of attribute user.
-
#volumes ⇒ Object
Returns the value of attribute volumes.
Class Method Summary collapse
-
.config {|@config| ... } ⇒ Object
(also: 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.
-
.version ⇒ Object
Returns the version of the docker-compose executable on the system.
Instance Method Summary collapse
-
#build ⇒ Object
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.
-
#check_version ⇒ Object
Checks the min and max version against the current docker version if they have been configured.
-
#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.
-
#down ⇒ Object
Pull in supported env settings and call down Specify REMOVE_VOLUMES=true to also remove any unused volumes when the containers are stopped.
-
#exec(*args) ⇒ Object
Call the compose exec method passing the given args after it.
-
#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
constructor
A new instance of Compose.
-
#logs ⇒ Object
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.
-
#mapped_public_port(service_name, private_port) ⇒ Object
Gets the dynamic port which was assigned to the compose service on the original private port.
-
#pull ⇒ Object
Call the compose pull method.
-
#push ⇒ Object
Call the compose push method.
-
#restart ⇒ Object
Pull in supported env settings and call restart.
-
#run(*args) ⇒ Object
Call the compose run method passing the given args after it.
-
#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.
-
#stop ⇒ Object
Pull in supported env settings and call stop.
-
#up ⇒ Object
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.
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() @project_dir = project_dir @project_name = project_name @services = Array(services) @user = user @volumes = Array(volumes) @capture = capture check_version end |
Instance Attribute Details
#capture ⇒ Object
Returns the value of attribute capture.
44 45 46 |
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44 def capture @capture end |
#compose_files ⇒ Object
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 |
#environment ⇒ Object
Returns the value of attribute environment.
44 45 46 |
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44 def environment @environment end |
#options ⇒ Object
Returns the value of attribute options.
44 45 46 |
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44 def @options end |
#project_dir ⇒ Object
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_name ⇒ Object
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 |
#services ⇒ Object
Returns the value of attribute services.
44 45 46 |
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44 def services @services end |
#user ⇒ Object
Returns the value of attribute user.
44 45 46 |
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44 def user @user end |
#volumes ⇒ Object
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
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 |
.version ⇒ Object
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
#build ⇒ Object
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 ('--parallel') merge_env_pull_option merge_env_push_option merge_env_cache_option execute_command(build_command('build')) end |
#check_version ⇒ Object
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 |
#down ⇒ Object
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 |
#logs ⇒ Object
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 |
#pull ⇒ Object
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 |
#push ⇒ Object
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 |
#restart ⇒ Object
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 |
#stop ⇒ Object
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 |
#up ⇒ Object
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 |