Class: Docker::Cli::CommandFactory
- Inherits:
-
Object
- Object
- Docker::Cli::CommandFactory
- Includes:
- TR::CondUtils
- Defined in:
- lib/docker/cli/command_factory.rb
Instance Method Summary collapse
- #attach_container(container, opts = { }) ⇒ Object
- #build_image(name = "", opts = { }, &block) ⇒ Object
-
#create_container_from_image(image, opts = { }) ⇒ Object
Create container from image directly e.g.
- #create_network(network_name, opts = { }) ⇒ Object
- #delete_container(container, opts = { }) ⇒ Object
-
#delete_image(name, tag = "", opts = { }) ⇒ Object
find_image.
-
#find_from_all_container(name, opts = { }) ⇒ Object
Find from container even if it is already stopped.
- #find_image(name, tag = "", opts = { }) ⇒ Object
- #find_running_container(name, opts = { }) ⇒ Object
- #remove_network(name) ⇒ Object
- #run_command_in_running_container(container, command, opts = { }) ⇒ Object
-
#start_container(container, opts = { }) ⇒ Object
run_container_from_image.
- #stop_container(container, opts = { }) ⇒ Object
Instance Method Details
#attach_container(container, opts = { }) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/docker/cli/command_factory.rb', line 182 def attach_container(container, opts = { }) opts = {} if opts.nil? cmd = [] cmd << Cli.docker_exe cmd << "container" cmd << "attach" cmd << container logger.debug "Attach Container : #{cmd.join(" ")}" # this is a bit difficult to juggle # it depending on the previous docker configuration # but to be save, just open up a new terminal Command.new(cmd, true) end |
#build_image(name = "", opts = { }, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/docker/cli/command_factory.rb', line 12 def build_image(name = "", opts = { }, &block) opts = { } if opts.nil? cmd = [] cmd << Cli.docker_exe cmd << "build" if not_empty?(name) cmd << "-t #{name}" end if not_empty?(opts[:dockerfile]) cmd << "-f #{opts[:dockerfile]}" end root = opts[:context_root] root = "." if is_empty?(root) cmd << root logger.debug "Build Image : #{cmd.join(" ")}" Command.new(cmd) end |
#create_container_from_image(image, opts = { }) ⇒ Object
Create container from image directly e.g. > docker run -it <image> “/bin/bash”
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/docker/cli/command_factory.rb', line 127 def create_container_from_image(image, opts = { }) opts = {} if opts.nil? cmd = [] cmd << Cli.docker_exe cmd << "run" cmd << "-i" if opts[:interactive] == true cmd << "-t" if opts[:tty] == true cmd << "-d" if opts[:detached] == true cmd << "--rm" if opts[:del] == true if not (opts[:container_name].nil? or opts[:container_name].empty?) if TR::RTUtils.on_mac? # double '\' is required when forking new terminal on mac cmd << "--name \\\"#{opts[:container_name]}\\\"" else cmd << "--name \"#{opts[:container_name]}\"" end end cmd << process_mount(opts) cmd << process_port(opts) cmd << process_network(opts) if opts[:match_user] == true ui = UserInfo.user_info gi = UserInfo.group_info cmd << "-u #{ui[:uid]}:#{gi[:gid]}" end cmd << image if not_empty?(opts[:command]) #cmd << "\"#{opts[:command]}\"" cmd << opts[:command] end interactive = false interactive = true if opts[:interactive] or opts[:tty] logger.debug "Run Container from Image : #{cmd.join(" ")}" Command.new(cmd, (interactive ? true : false)) end |
#create_network(network_name, opts = { }) ⇒ Object
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 252 253 254 255 256 |
# File 'lib/docker/cli/command_factory.rb', line 225 def create_network(network_name, opts = { }) cmd = [] cmd << Cli.docker_exe cmd << "network" cmd << "create" cmd << network_name cmd << "--driver=#{opts[:driver]}" if not_empty?(opts[:driver]) if not_empty?(opts[:subnet]) subnet = opts[:subnet] if subnet.is_a?(Array) cmd += subnet.map { |v| "--subnet=#{v}"} else cmd << "--subnet=#{opts[:subnet]}" end end if not_empty?(opts[:gateway]) gateway = opts[:gateway] if gateway.is_a?(Array) cmd += gateway.map { |v| "--gateway=#{v}"} else cmd << "--gateway=#{opts[:gateway]}" end end cmd << "--ip-range=#{opts[:ip_range]}" if not_empty?(opts[:ip_range]) logger.debug "Create Network : #{cmd.join(" ")}" Command.new(cmd) end |
#delete_container(container, opts = { }) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/docker/cli/command_factory.rb', line 212 def delete_container(container, opts = { }) cmd = [] cmd << Cli.docker_exe cmd << "container" cmd << "rm" cmd << container logger.debug "Delete Container : #{cmd.join(" ")}" Command.new(cmd) end |
#delete_image(name, tag = "", opts = { }) ⇒ Object
find_image
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/docker/cli/command_factory.rb', line 53 def delete_image(name, tag = "", opts = { }) if not_empty?(name) cmd = [] cmd << Cli.docker_exe cmd << "rmi" if not_empty?(tag) cmd << "#{name}:#{tag}" else cmd << name end logger.debug "Delete image: #{cmd.join(" ")}" Command.new(cmd) else raise Error, "Name is required for delete operation" end end |
#find_from_all_container(name, opts = { }) ⇒ Object
Find from container even if it is already stopped
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/docker/cli/command_factory.rb', line 99 def find_from_all_container(name, opts = { }) raise Error, "Name is required" if is_empty?(name) cmd = [] cmd << Cli.docker_exe cmd << "ps" # return all info instead of only the container ID #cmd << "-a" cmd << "-aq" cmd << "-f" # From little testing seems the command by default already support regex formatting # So can use the regex marker to get exact match # e.g. if want exact match, pass in ^#{name}\z #cmd << "name=\"#{name}\"" if opts[:exact_name] == true cmd << "name=\"^/#{name}$\"" else cmd << "name=\"#{name}\"" end logger.debug "Find from all container: #{cmd.join(" ")}" Command.new(cmd) end |
#find_image(name, tag = "", opts = { }) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/docker/cli/command_factory.rb', line 36 def find_image(name, tag = "", opts = { }) name = "" if name.nil? cmd = [] cmd << Cli.docker_exe cmd << "images" cmd << "-q" if not_empty?(tag) cmd << "\"#{name}:#{tag}\"" else cmd << "\"#{name}\"" end logger.debug "Find image: #{cmd.join(" ")}" Command.new(cmd) end |
#find_running_container(name, opts = { }) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/docker/cli/command_factory.rb', line 76 def find_running_container(name, opts = { }) raise Error, "Name is mandatory" if is_empty?(name) cmd = [] cmd << Cli.docker_exe cmd << "ps" cmd << "-q" cmd << "-f" if opts[:exact_name] == true cmd << "name=\"^/#{name}$\"" else cmd << "name=\"#{name}\"" end logger.debug "Find container: #{cmd.join(" ")}" Command.new(cmd) end |
#remove_network(name) ⇒ Object
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/docker/cli/command_factory.rb', line 259 def remove_network(name) cmd = [] cmd << Cli.docker_exe cmd << "network" cmd << "rm" cmd << name logger.debug "Remove Network : #{cmd.join(" ")}" Command.new(cmd) end |
#run_command_in_running_container(container, command, opts = { }) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/docker/cli/command_factory.rb', line 271 def run_command_in_running_container(container, command, opts = { }) cmd = [] cmd << Cli.docker_exe cmd << "container" cmd << "exec" isTty = false isInteractive = false if not_empty?(opts[:tty]) and opts[:tty] == true cmd << "-t" isTty = true end if not_empty?(opts[:interactive]) and opts[:interactive] == true cmd << "-i" isInteractive = true end cmd << container if is_empty?(command) cmd << "/bin/bash --login" else cmd << command end logger.debug "Run command in running container : #{cmd.join(" ")}" Command.new(cmd, ((isTty or isInteractive) ? true : false)) end |
#start_container(container, opts = { }) ⇒ Object
run_container_from_image
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/docker/cli/command_factory.rb', line 169 def start_container(container, opts = { }) opts = {} if opts.nil? cmd = [] cmd << Cli.docker_exe cmd << "container" cmd << "start" cmd << container logger.debug "Start Container : #{cmd.join(" ")}" Command.new(cmd) end |
#stop_container(container, opts = { }) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/docker/cli/command_factory.rb', line 199 def stop_container(container, opts = { }) cmd = [] cmd << Cli.docker_exe cmd << "container" cmd << "stop" cmd << container logger.debug "Stop Container : #{cmd.join(" ")}" Command.new(cmd) end |