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.
- #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
- #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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/docker/cli/command_factory.rb', line 164 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”
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/docker/cli/command_factory.rb', line 116 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?) cmd << "--name \"#{opts[:container_name]}\"" end cmd << process_mount(opts) cmd << process_port(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]}\"" 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 |
#delete_container(container, opts = { }) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/docker/cli/command_factory.rb', line 194 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/docker/cli/command_factory.rb', line 94 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}\"" 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 |
# 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" cmd << "name=\"#{name}\"" logger.debug "Find container: #{cmd.join(" ")}" Command.new(cmd) end |
#run_command_in_running_container(container, command, opts = { }) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/docker/cli/command_factory.rb', line 207 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
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/docker/cli/command_factory.rb', line 151 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
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/docker/cli/command_factory.rb', line 181 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 |