├── .gitignore ├── Gemfile ├── Gemfile.lock ├── README.md ├── bin └── docker-console └── lib ├── command ├── container.rb ├── help.rb ├── image.rb └── machine.rb ├── function ├── format.rb └── run_container.rb └── ripl └── like_shell.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .byebug_history 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gem 'ripl' 3 | gem 'ripl-shell_commands' 4 | gem 'ripl-color_result' 5 | gem 'ripl-color_streams' 6 | gem 'ripl-commands' 7 | gem 'docker-api' 8 | gem 'json' 9 | gem 'terminal-table' 10 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | bond (0.5.1) 5 | docker-api (1.23.0) 6 | excon (>= 0.38.0) 7 | json 8 | excon (0.45.4) 9 | json (1.8.3) 10 | paint (1.0.0) 11 | ripl (0.7.1) 12 | bond (~> 0.5.1) 13 | ripl-color_result (0.4.1) 14 | ripl (>= 0.4.1) 15 | wirb (>= 0.4.0) 16 | ripl-color_streams (0.1.2) 17 | ripl (>= 0.2.8) 18 | ripl-commands (0.2.1) 19 | ripl (>= 0.2.9) 20 | ripl-shell_commands (0.1.0) 21 | ripl (~> 0.3) 22 | terminal-table (1.5.2) 23 | wirb (2.0.0) 24 | paint (>= 0.9, < 2.0) 25 | 26 | PLATFORMS 27 | ruby 28 | 29 | DEPENDENCIES 30 | docker-api 31 | json 32 | ripl 33 | ripl-color_result 34 | ripl-color_streams 35 | ripl-commands 36 | ripl-shell_commands 37 | terminal-table 38 | 39 | BUNDLED WITH 40 | 1.10.6 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-console 2 | 3 | This is a conosle tools for docker! 4 | 5 | ## 0. Quick Start 6 | 7 | **First, install docker & docer-machine & ruby!** 8 | **Ruby >2.0 should be required** 9 | 10 | ``` 11 | $ git clone https://github.com/zhuangbiaowei/docker-console 12 | $ cd docker-console 13 | $ bundle install 14 | $ bundle exec ./bin/docker-console 15 | unix:///var/run/docker.sock > _ 16 | ``` 17 | 18 | ## 1. machine commands 19 | 20 | ### 1.1. list machine (or search machine) 21 | 22 | ``` 23 | unix:///var/run/docker.sock > lm 24 | +--------+----------------+---------------------------+--------+ 25 | | Number | Name | URL | Labels | 26 | +--------+----------------+---------------------------+--------+ 27 | | 0 | local | tcp://192.168.99.100:2376 | web | 28 | | 1 | swarm-agent-00 | tcp://192.168.99.102:2376 | | 29 | | 2 | swarm-agent-01 | tcp://192.168.99.103:2376 | | 30 | | 3 | swarm-master | tcp://192.168.99.101:2376 | db | 31 | +--------+----------------+---------------------------+--------+ 32 | => "Total 4 machines." 33 | unix:///var/run/docker.sock > lm swarm 34 | +--------+----------------+---------------------------+--------+ 35 | | Number | Name | URL | Labels | 36 | +--------+----------------+---------------------------+--------+ 37 | | 0 | swarm-agent-00 | tcp://192.168.99.102:2376 | | 38 | | 1 | swarm-agent-01 | tcp://192.168.99.103:2376 | | 39 | | 2 | swarm-master | tcp://192.168.99.101:2376 | db | 40 | +--------+----------------+---------------------------+--------+ 41 | => "Total 3 machines." 42 | unix:///var/run/docker.sock > lm * web 43 | +--------+-------+---------------------------+--------+ 44 | | Number | Name | URL | Labels | 45 | +--------+-------+---------------------------+--------+ 46 | | 0 | local | tcp://192.168.99.100:2376 | web | 47 | +--------+-------+---------------------------+--------+ 48 | => "Total 1 machines." 49 | ``` 50 | 51 | ### 1.2 connect machine 52 | 53 | ``` 54 | unix:///var/run/docker.sock > cm local 55 | => "Connected Docker: tcp://192.168.99.101:2376" 56 | unix:///var/run/docker.sock > lm swarm 57 | +--------+----------------+---------------------------+--------+ 58 | | Number | Name | URL | Labels | 59 | +--------+----------------+---------------------------+--------+ 60 | | 0 | swarm-agent-00 | tcp://192.168.99.102:2376 | | 61 | | 1 | swarm-agent-01 | tcp://192.168.99.103:2376 | | 62 | | 2 | swarm-master | tcp://192.168.99.101:2376 | db | 63 | +--------+----------------+---------------------------+--------+ 64 | => "Total 3 machines." 65 | tcp://192.168.99.101:2376 > cm 2 66 | => "Connected Docker: tcp://192.168.99.101:2376" 67 | tcp://192.168.99.101:2376 > cm tcp://192.168.99.102:2376 68 | => "Connected Docker: tcp://192.168.99.102:2376" 69 | ``` 70 | 71 | # 2. container commands 72 | 73 | ## 2.1. list container 74 | ``` 75 | tcp://192.168.99.100:2376 > lc 76 | +----------+-------+-----------------------------+------------------------+-----------+----------------------+ 77 | | ID | Image | Name | Command | Status | Port | 78 | +----------+-------+-----------------------------+------------------------+-----------+----------------------+ 79 | | 8ad0ae80 | nginx | /compassionate_varahamihira | nginx -g 'daemon off;' | Up 7 days | 0.0.0.0:443->443/tcp | 80 | | 8ad0ae80 | nginx | /compassionate_varahamihira | nginx -g 'daemon off;' | Up 7 days | 0.0.0.0:80->80/tcp | 81 | +----------+-------+-----------------------------+------------------------+-----------+----------------------+ 82 | => "Total 1 containers." 83 | ``` 84 | ### 2.2. rm container 85 | ``` 86 | tcp://192.168.99.100:2376 > rm 3edb26a6 87 | => "Container 3edb26a6 Deleted!" 88 | ``` 89 | ### 2.3. run container 90 | ``` 91 | tcp://192.168.99.100:2376 > run nginx -p 80:80,443:443 92 | => nil 93 | tcp://192.168.99.100:2376 > run swarm --rm create 94 | !fc23ab04571c9e6f388a60f914ba697e 95 | => nil 96 | ``` 97 | ### 2.4. multi run containers 98 | ``` 99 | tcp://192.168.99.100:2376 > lm 100 | +--------+----------------+---------------------------+--------+ 101 | | Number | Name | URL | Labels | 102 | +--------+----------------+---------------------------+--------+ 103 | | 0 | local | tcp://192.168.99.100:2376 | web | 104 | | 1 | swarm-agent-00 | tcp://192.168.99.102:2376 | | 105 | | 2 | swarm-agent-01 | tcp://192.168.99.103:2376 | | 106 | | 3 | swarm-master | tcp://192.168.99.101:2376 | db | 107 | +--------+----------------+---------------------------+--------+ 108 | => "Total 4 machines." 109 | tcp://192.168.99.100:2376 > mrun nginx -p 80:80,443:443 110 | => "Run command at 4 machines." 111 | ``` 112 | # 3. image commands 113 | # 3.1. list image 114 | ``` 115 | tcp://192.168.99.100:2376 > li 116 | +--------------+------------------+--------+----------+ 117 | | ID | Image | Tag | VSize | 118 | +--------------+------------------+--------+----------+ 119 | | fac9d14f6fe0 | composerails_web | latest | 914.3 MB | 120 | | 09897cd2cc05 | postgres | latest | 265.1 MB | 121 | | 81415e35fc6c | nginx | latest | 132.7 MB | 122 | | 6b40fe7724bd | swarm | 1.0.0 | 15.6 MB | 123 | | 6b40fe7724bd | swarm | latest | 15.6 MB | 124 | | 0a6ba66e537a | hello-world | latest | 960 B | 125 | | ac90cee00759 | ruby | 2.2.0 | 774.6 MB | 126 | +--------------+------------------+--------+----------+ 127 | => "Total 6 images." 128 | ``` 129 | -------------------------------------------------------------------------------- /bin/docker-console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'json' 3 | require 'docker' 4 | require 'ripl' 5 | require 'ripl/readline' 6 | require 'ripl/shell_commands' 7 | require 'ripl/color_result' 8 | require 'ripl/color_streams' 9 | require 'ripl/commands' 10 | 11 | require './lib/ripl/like_shell' 12 | require './lib/function/format' 13 | require './lib/function/run_container' 14 | require './lib/command/help' 15 | require './lib/command/machine' 16 | require './lib/command/image' 17 | require './lib/command/container' 18 | 19 | Ripl.config[:color_streams_stdout] = :light_blue 20 | Ripl.config[:color_streams_stderr] = :light_red 21 | Ripl.config[:prompt] = lambda {"#{Docker.url} > " } 22 | 23 | class DockerConsole 24 | include Help 25 | include Machine 26 | include Image 27 | include Container 28 | end 29 | 30 | dc = DockerConsole.new 31 | dc.load_machines "~/.docker/machine/machines" 32 | 33 | version = "0.0.1" 34 | 35 | Ripl.start :binding => dc.instance_eval{ binding } 36 | -------------------------------------------------------------------------------- /lib/command/container.rb: -------------------------------------------------------------------------------- 1 | module Container 2 | def lc(cmd=nil) 3 | all = (cmd=="-a")? true : false 4 | list = Docker::Container.all(:all=>all) 5 | format_container(list) 6 | return "Total #{list.count} containers." 7 | end 8 | def rm(id) 9 | container = Docker::Container.get(id) 10 | container.kill! 11 | container.delete 12 | "Container #{id} Deleted!" 13 | end 14 | def run(img,cmd=[]) 15 | run_container(img,cmd) 16 | end 17 | def mrun(img,cmd=[]) 18 | url = Docker.url 19 | @machines.each do |machine| 20 | ENV["DOCKER_TLS_VERIFY"]="1" 21 | ENV["DOCKER_HOST"]=machine["url"] 22 | ENV["DOCKER_CERT_PATH"]=File.expand_path(@docker_machine_data)+"/"+machine["name"] 23 | ENV["DOCKER_MACHINE_NAME"]=machine["name"] 24 | Docker.url = machine["url"] 25 | run_container(img,cmd.clone) 26 | end 27 | Docker.url = url 28 | return "Run command at #{@machines.count} machines." 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/command/help.rb: -------------------------------------------------------------------------------- 1 | module Help 2 | def help 3 | puts "cm: connect machine\n"+ 4 | "lm: list or search machines\n"+ 5 | "li: list images\n"+ 6 | "lc: list containers\n"+ 7 | "rm: remove container\n"+ 8 | "run: run image with cmd\n"+ 9 | "mrun: multi run image with cmd" 10 | return nil 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /lib/command/image.rb: -------------------------------------------------------------------------------- 1 | module Image 2 | def li 3 | list = Docker::Image.all 4 | format_image(list) 5 | return "Total #{list.count} images." 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/command/machine.rb: -------------------------------------------------------------------------------- 1 | module Machine 2 | def load_machines config_dir 3 | @docker_machine_data = config_dir 4 | @all_machines = [] 5 | @machine = [] 6 | machines = `ls #{@docker_machine_data}`.split("\n") 7 | machines.each do |machine| 8 | f = File.open(File.expand_path(@docker_machine_data)+"/"+machine+"/config.json","r") 9 | data = f.read 10 | json = JSON.parse(data) 11 | labels = json["HostOptions"]["EngineOptions"]["Labels"] 12 | @all_machines << { 13 | "name"=>machine, 14 | "url"=>"tcp://"+json["Driver"]["IPAddress"]+":2376", 15 | "labels"=>labels 16 | } 17 | end 18 | end 19 | 20 | def lm(name="*",label="*") 21 | @machines = [] 22 | machines = `ls #{@docker_machine_data}`.split("\n") 23 | num = 0 24 | machines.each do |machine| 25 | f = File.open(File.expand_path(@docker_machine_data)+"/"+machine+"/config.json","r") 26 | data = f.read 27 | json = JSON.parse(data) 28 | labels = json["HostOptions"]["EngineOptions"]["Labels"] 29 | if (name=="*" || machine.include?(name)) && (label=="*" || labels.include?(label)) 30 | url="tcp://"+json["Driver"]["IPAddress"]+":2376" 31 | @machines << { 32 | "num" => num, 33 | "name" => machine, 34 | "url" => url, 35 | "labels" => labels.join(",") 36 | } 37 | num = num + 1 38 | end 39 | end 40 | format_machine(@machines) 41 | return "Total #{@machines.count} machines." 42 | end 43 | 44 | def cm(url) 45 | if url.to_i.to_s==url 46 | if @machines.empty? 47 | machine = @all_machines[url.to_i] 48 | else 49 | machine = @machines[url.to_i] 50 | end 51 | ENV["DOCKER_TLS_VERIFY"]="1" 52 | ENV["DOCKER_HOST"]=machine["url"] 53 | ENV["DOCKER_CERT_PATH"]=File.expand_path(@docker_machine_data)+"/"+machine["name"] 54 | ENV["DOCKER_MACHINE_NAME"]=machine["name"] 55 | Docker.url=machine["url"] 56 | else 57 | @all_machines.each do |machine| 58 | if machine["name"]==url or machine["url"]==url 59 | ENV["DOCKER_TLS_VERIFY"]="1" 60 | ENV["DOCKER_HOST"]=machine["url"] 61 | ENV["DOCKER_CERT_PATH"]=File.expand_path(@docker_machine_data)+"/"+machine["name"] 62 | ENV["DOCKER_MACHINE_NAME"]=machine["name"] 63 | Docker.url=machine["url"] 64 | end 65 | end 66 | end 67 | "Connected Docker: #{Docker.url}" 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /lib/function/format.rb: -------------------------------------------------------------------------------- 1 | require 'terminal-table' 2 | 3 | def human_size(size) 4 | if size>1000*1000 5 | return "#{(size/1000.0/1000.0*10).to_i/10.0} MB" 6 | elsif size>1000 7 | return "#{(size/1000.0*10).to_i/10.0} KB" 8 | else 9 | return "#{size} B" 10 | end 11 | end 12 | 13 | def format_machine(machines) 14 | table = Terminal::Table.new do |t| 15 | t.headings = ['Number','Name','URL','Labels'] 16 | machines.each do |m| 17 | t << [m["num"],m["name"],m["url"],m["labels"]] 18 | end 19 | end 20 | puts table 21 | end 22 | 23 | def format_image(list) 24 | table = Terminal::Table.new do |t| 25 | t.headings = ['ID','Image','Tag','VSize'] 26 | list.each do |img| 27 | img.info["RepoTags"].each do |rt| 28 | t << [img.id[0..11], rt.split(":")[0..-2].join(":"), rt.split(":")[-1],human_size(img.info["VirtualSize"])] 29 | end 30 | end 31 | end 32 | puts table 33 | end 34 | 35 | def format_container(list) 36 | table = Terminal::Table.new do |t| 37 | t.headings = ["ID","Image","Name","Command","Status","Port"] 38 | list.each do |con| 39 | id = con.id[0..7] 40 | img = con.info["Image"] 41 | cmd = con.info["Command"] 42 | status = con.info["Status"] 43 | name = con.info["Names"][0] 44 | ports = con.info["Ports"] 45 | if ports.size>0 46 | ports.each do |port| 47 | port_str = "#{port["IP"]}:#{port["PrivatePort"]}->#{port["PublicPort"]}/#{port["Type"]}" 48 | t << [id,img,name,cmd,status,port_str] 49 | end 50 | else 51 | t << [id,img,name,cmd,status,""] 52 | end 53 | end 54 | end 55 | puts table 56 | end 57 | -------------------------------------------------------------------------------- /lib/function/run_container.rb: -------------------------------------------------------------------------------- 1 | def run_container(img,cmd=[]) 2 | if cmd.class == String 3 | cmd = [cmd] 4 | end 5 | if cmd.include?("--rm") 6 | rm=true 7 | cmd.delete("--rm") 8 | end 9 | port_index = cmd.index("-p") 10 | if port_index 11 | port = cmd[port_index+1] 12 | cmd.delete("-p") 13 | cmd.delete(port) 14 | end 15 | if port 16 | host_config={"PortBindings"=>{}} 17 | port.split(",").each do |port_pair| 18 | publish_port,inner_port = port_pair.split(":") 19 | host_config["PortBindings"]["#{publish_port}/tcp"]=[{"HostPort"=>"#{inner_port}"}] 20 | end 21 | end 22 | container = Docker::Container.create('Image' => img, 'Cmd' => cmd, "HostConfig"=>host_config) 23 | container.start 24 | if rm==true 25 | while container.info["State"]==nil 26 | sleep 1 27 | container = Docker::Container.get(container.id) 28 | end 29 | while container.info["State"]["Status"]=="running" 30 | sleep 1 31 | container = Docker::Container.get(container.id) 32 | end 33 | puts container.logs(stdout: true,stderr: true) 34 | container.stop 35 | container.kill 36 | container.delete(:force => true) 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/ripl/like_shell.rb: -------------------------------------------------------------------------------- 1 | module Ripl::Readline 2 | def get_input 3 | input = Readline.readline prompt, true 4 | if input.include?(" ") 5 | arr = input.split(" ") 6 | params = DockerConsole.instance_method(arr[0].to_sym).parameters 7 | if arr.length-1 > params.length 8 | return arr[0] + " " + arr[1..params.length-1].collect{|k| "\"#{k}\""}.join(",") +","+ "[#{arr[params.length..-1].collect{|k| "\"#{k}\""}.join(",")}]" 9 | else 10 | return arr[0]+" "+arr[1..-1].collect{|k| "\"#{k}\""}.join(",") 11 | end 12 | else 13 | return input 14 | end 15 | end 16 | end 17 | 18 | Ripl::Shell.include Ripl::Readline 19 | --------------------------------------------------------------------------------