Class: Dev::Docker
- Defined in:
- lib/firespring_dev_commands/docker.rb,
lib/firespring_dev_commands/docker/status.rb,
lib/firespring_dev_commands/docker/compose.rb
Overview
Class contains many useful methods for interfacing with the docker api
Defined Under Namespace
Classes: Compose, Config, Status
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 engine running on the system.
Instance Method Summary collapse
-
#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
deprecated
Deprecated.
Please use Compose#container_by_name instead
-
#copy_from_container(container, source_path, dest_path, required: true) ⇒ Object
Copies the source path on the container to the destination path on your local machine If required is set to true, the command will fail if the source path does not exist on the container.
-
#copy_to_container(container, source_path, dest_path) ⇒ Object
Copies the source path on your local machine to the destination path on the container.
-
#force_remove_images(name_and_tag) ⇒ Object
Remove docker images with the “force” option set to true This will remove the images even if they are currently in use and cause unintended side effects.
-
#initialize ⇒ Docker
constructor
A new instance of Docker.
-
#mapped_public_port(name, private_port) ⇒ Object
deprecated
Deprecated.
Please use Compose#mapped_public_port instead
-
#print_containers ⇒ Object
Display a nicely formatted table of containers and their associated information.
-
#print_images ⇒ Object
rubocop:disable Metrics/ParameterLists Display a nicely formatted table of images and their associated information.
-
#prune ⇒ Object
Prunes/removes all unused containers, networks, volumes, and images.
-
#prune_containers ⇒ Object
Prunes/removes all unused containers.
-
#prune_images ⇒ Object
Prunes/removes all unused images.
-
#prune_networks ⇒ Object
Prunes/removes all unused networks.
-
#prune_volumes ⇒ Object
Prunes/removes all unused volumes Specify ALL_VOLUMES=false in your environment to only clean anonymous volumes (docker version 23.x+).
Constructor Details
#initialize ⇒ Docker
Returns a new instance of Docker.
41 42 43 |
# File 'lib/firespring_dev_commands/docker.rb', line 41 def initialize check_version 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
26 27 28 29 30 |
# File 'lib/firespring_dev_commands/docker.rb', line 26 def config @config ||= Config.new yield(@config) if block_given? @config end |
.version ⇒ Object
Returns the version of the docker engine running on the system
36 37 38 |
# File 'lib/firespring_dev_commands/docker.rb', line 36 def version @version ||= JSON.parse(::Docker.connection.get('/version'))['Version'] end |
Instance Method Details
#check_version ⇒ Object
Checks the min and max version against the current docker version if they have been configured
46 47 48 49 50 51 52 |
# File 'lib/firespring_dev_commands/docker.rb', line 46 def check_version min_version = self.class.config.min_version raise "requires docker version >= #{min_version} (found #{self.class.version})" if min_version && !Dev::Common.new.version_greater_than(min_version, self.class.version) max_version = self.class.config.max_version raise "requires docker version < #{max_version} (found #{self.class.version})" if max_version && Dev::Common.new.version_greater_than(max_version, self.class.version) end |
#container_by_name(service_name, prefix = nil, status: [Docker::Status::RUNNING]) ⇒ Object
Please use Dev::Docker::Compose#container_by_name instead
Calls the docker compose method with the given inputs
129 130 131 132 |
# File 'lib/firespring_dev_commands/docker.rb', line 129 def container_by_name(service_name, prefix = nil, status: [Docker::Status::RUNNING]) warn '[DEPRECATION] `Docker#container_by_name` is deprecated. Please use `Docker::Compose#container_by_name` instead.' Docker::Compose.new.container_by_name(service_name, prefix, status) end |
#copy_from_container(container, source_path, dest_path, required: true) ⇒ Object
Copies the source path on the container to the destination path on your local machine If required is set to true, the command will fail if the source path does not exist on the container
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/firespring_dev_commands/docker.rb', line 156 def copy_from_container(container, source_path, dest_path, required: true) LOG.info "Copying #{source_path} to #{dest_path}... " tar = StringIO.new begin container.archive_out(source_path) do |chunk| tar.write(chunk) end rescue => e raise e if required puts 'Not Found' end Dev::Tar.new(tar).unpack(source_path, dest_path) end |
#copy_to_container(container, source_path, dest_path) ⇒ Object
Copies the source path on your local machine to the destination path on the container
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/firespring_dev_commands/docker.rb', line 142 def copy_to_container(container, source_path, dest_path) LOG.info "Copying #{source_path} to #{dest_path}... " container.archive_in(source_path, dest_path, overwrite: true) return unless File.directory?(source_path) dest_file = File.basename(source_path) # TODO: Can we find a better solution for this? Seems pretty brittle retcode = container.exec(['bash', '-c', "cd #{dest_path}; tar -xf #{dest_file}; rm -f #{dest_file}"])[-1] raise 'Unable to unpack on container' unless retcode.zero? end |
#force_remove_images(name_and_tag) ⇒ Object
Remove docker images with the “force” option set to true This will remove the images even if they are currently in use and cause unintended side effects.
122 123 124 125 |
# File 'lib/firespring_dev_commands/docker.rb', line 122 def force_remove_images(name_and_tag) images = ::Docker::Image.all(filter: name_and_tag) ::Docker::Image.remove(images[0].id, force: true) unless images.empty? end |
#mapped_public_port(name, private_port) ⇒ Object
Please use Dev::Docker::Compose#mapped_public_port instead
Calls the docker compose method with the given inputs
136 137 138 139 |
# File 'lib/firespring_dev_commands/docker.rb', line 136 def mapped_public_port(name, private_port) warn '[DEPRECATION] `Docker#mapped_public_port` is deprecated. Please use `Docker::Compose#mapped_public_port` instead.' Docker::Compose.new.mapped_public_port(name, private_port) end |
#print_containers ⇒ Object
Display a nicely formatted table of containers and their associated information
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/firespring_dev_commands/docker.rb', line 222 def print_containers idsize = 15 imagesize = 20 archsize = 7 commandsize = 20 createsize = 15 statussize = 15 portsize = 20 namesize = 16 total = idsize + imagesize + archsize + commandsize + createsize + statussize + portsize + namesize # If there is additional width available, add it to the repo and tag columns additional = [((Rake.application.terminal_width - total) / 3).floor, 0].max # If there's enough extra, give some to the name as well if additional > 40 namesize += 15 additional -= 5 end imagesize += additional commandsize += additional portsize += additional format = "%-#{idsize}s%-#{imagesize}s%-#{archsize}s%-#{commandsize}s%-#{createsize}s%-#{statussize}s%-#{portsize}s%-#{namesize}s" puts format(format, :'CONTAINER ID', :IMAGE, :ARCH, :COMMAND, :CREATED, :STATUS, :PORTS, :NAMES) ::Docker::Container.all.each do |container| id, image, arch, command, created, status, ports, names = container_info(container) puts format(format, id, image.truncate(imagesize - 5), arch, command.truncate(commandsize - 5), created, status, ports.truncate(portsize - 5), names) end end |
#print_images ⇒ Object
rubocop:disable Metrics/ParameterLists Display a nicely formatted table of images and their associated information
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/firespring_dev_commands/docker.rb', line 175 def print_images reposize = 70 = 70 archsize = 9 imagesize = 15 createsize = 20 sizesize = 10 total = reposize + + archsize + imagesize + createsize + sizesize # If there is additional width available, add it to the repo and tag columns additional = [((Rake.application.terminal_width - total) / 2).floor, 0].max reposize += additional += additional format = "%-#{reposize}s%-#{tagsize}s%-#{archsize}s%-#{imagesize}s%-#{createsize}s%s" puts format(format, :REPOSITORY, :TAG, :ARCH, :'IMAGE ID', :CREATED, :SIZE) ::Docker::Image.all.each do |image| image_info(image).each do |repo, tag, arch, id, created, size| puts format(format, repo, tag, arch, id, created, size) end end end |
#prune ⇒ Object
Prunes/removes all unused containers, networks, volumes, and images
55 56 57 58 59 60 |
# File 'lib/firespring_dev_commands/docker.rb', line 55 def prune prune_containers prune_networks prune_volumes prune_images end |
#prune_containers ⇒ Object
Prunes/removes all unused containers
63 64 65 |
# File 'lib/firespring_dev_commands/docker.rb', line 63 def prune_containers _prune('containers') end |
#prune_images ⇒ Object
Prunes/removes all unused images
81 82 83 |
# File 'lib/firespring_dev_commands/docker.rb', line 81 def prune_images _prune('images') end |
#prune_networks ⇒ Object
Prunes/removes all unused networks
68 69 70 |
# File 'lib/firespring_dev_commands/docker.rb', line 68 def prune_networks _prune('networks') end |
#prune_volumes ⇒ Object
Prunes/removes all unused volumes Specify ALL_VOLUMES=false in your environment to only clean anonymous volumes (docker version 23.x+)
74 75 76 77 78 |
# File 'lib/firespring_dev_commands/docker.rb', line 74 def prune_volumes opts = {} opts[:filters] = {all: ['true']}.to_json if Dev::Common.new.version_greater_than('22.9999.0', self.class.version) && ENV['ALL_VOLUMES'].to_s.strip != 'false' _prune('volumes', opts:) end |