├── README.md └── create_docker /README.md: -------------------------------------------------------------------------------- 1 | # docker-tools 2 | docker-tools 3 | 记录了在工作使用中一些心得和尝试。 4 | -------------------------------------------------------------------------------- /create_docker: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import os 3 | import json 4 | import commands 5 | import sys 6 | import time 7 | 8 | if __name__ == "__main__": 9 | mem = "8192" 10 | cpuset = '0-5' 11 | timestr = time.strftime("%Y%m%d%H%M%S", time.localtime()) 12 | rmlimit = 20 13 | 14 | if len(sys.argv) <= 1: 15 | print "How to use:" 16 | print "\t [Command] image version project" 17 | print "\t e.g: [Command] docker-registry.xxxxx.com:5000/xxxxxx/nginx-php v1 test.xxxxxx.com" 18 | sys.exit(0) 19 | 20 | 21 | image = sys.argv[1] 22 | version = sys.argv[2] 23 | project = sys.argv[3] 24 | 25 | runcomm = "docker run -it -d -v /etc/localtime:/etc/localtime:ro --name "+project+"_"+version+"_"+timestr + " -m "+mem+"m --cpuset-cpus "+cpuset+ " " + image+":"+version 26 | print runcomm 27 | status,did = commands.getstatusoutput( runcomm ) 28 | if status != 0 : 29 | print "docker run error!",did 30 | sys.exit(1) 31 | 32 | file_list = "/home/code/"+project+".list" 33 | f=open(file_list,'a') 34 | f.write( did ) 35 | f.write("\n") 36 | 37 | dockerlist = open(file_list,'rU').readlines() 38 | #print dockerlist 39 | dockercount = len( dockerlist ) 40 | print "project total count is:", dockercount 41 | 42 | rmcount = dockercount - rmlimit 43 | if rmcount > 0 : 44 | i = 0 45 | while i < rmcount: 46 | rmdocker = dockerlist[i][0:24] 47 | rmcomm = "docker rm -f "+rmdocker 48 | print rmcomm 49 | commands.getstatusoutput( rmcomm ) 50 | del( dockerlist[i] ) 51 | i += 1 52 | 53 | fl=open(file_list, 'w') 54 | for dockerid in dockerlist: 55 | fl.write(dockerid) 56 | fl.close() 57 | 58 | #3, get port 59 | inspcomm = "docker inspect "+did[0:24] 60 | status,inspres = commands.getstatusoutput( inspcomm ) 61 | outres = json.loads( inspres ) 62 | ip = outres[0]["NetworkSettings"]["IPAddress"] 63 | #sshport = outres[0]["NetworkSettings"]["Ports"]["22/tcp"][0]["HostPort"] 64 | 65 | print "--------------done-----------------" 66 | print "------docker id is:",did 67 | print "\n\n" 68 | print "ssh command is: ssh work@"+ip 69 | print "\n\n" 70 | --------------------------------------------------------------------------------