跑docker踩坑

这星期师兄安排工作跑docker,需要开100个docker容器,然后在里面执行一个如下操作:

在其下新建一个25M文件,计算其md5,copy到另外一个目录下,再次计算md5,比较两个md5是否相等来判断磁盘是否稳定,并且计算copy工作的时间,比较copy时间是否稳定。

首先新建mydocker文件夹,并写了一个setupbob.py的文件,代码如下:

import hashlib
import time
import string
import random 
from shutil import copyfile

//生成一个任意大小的文件,在文件末尾加上随机数
def genFile(path,size):
	file=open(path,'w')
	file.seek(1024*1024*size)
	ran_str=''.join(random.sample(string.ascii_letters+string.digits,random.randint(5,5)))
	file.write(ran_str+'\x00')
	file.close()

//对文件生成md5值
def getMd5(filepath):
	f=open(filepath,'rb')
	md5obj=hashlib.md5()
	md5obj.update(f.read())
	hash=md5obj.hexdigest()
	f.close()
	return str(hash).upper()

//向文件写入内容,以追加方式
def writeFile(filepath,content):
	file=open(filepath,"a")
	file.write(content)
	file.write('\n')
	file.close()

if __name__=="__main__":
	for i in range(100):
		genFile('./test',25)//生成一个25M的文件
		start=time.clock()//开始计时
		copyfile('./test','/tmp/test')//copy文件到另外一个目录
		elapsed=(time.clock()-start)//结束计时
		writeFile("./data/time.txt",str(elapsed))//将时间写入一个文件
		if getMd5('./test') != getMd5('/tmp/test')://比较md5
			writeFile("./data/error.txt","this container occur error!")//如果不同,则写入error.txt文件
			break
		time.sleep(600)
	

由于要开100个docker容器,我要在容器中执行上面的脚本,但是我不能一个一个输入,于是我决定定制自己的镜像,写如下Dockerfile:

FROM	ubuntu:16.04
ADD ./sources.list /etc/apt/
COPY ./setupbob.py ~/setbob.py
RUN apt-get update
RUN apt-get install -y python
CMD ["python","~/setbob.py"]

由于编译总是出错,于是决定换国内源,再当前目录下新建sources.list,写入如下内容

# deb cdrom:[Ubuntu 17.04 _xenial Zapus_ - Release amd64 (20170412)]/ xenial main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial universe
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner

deb http://security.ubuntu.com/ubuntu xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
# deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

最后,我准备写一个脚本执行生成100个docker容器,一开始准备用gnome-terminal生成100个终端,后来觉得太蠢了,还是准备用多线程,如下create.py:

import subprocess
import threading
from time import sleep

def dockerRun():
	subprocess.call('docker run -t -i -v /home/z/mydocker:/data myubuntu:v1',shell=True)

if __name__=="__main__":
	for i in range(100):
		my_thread=threading.Thread(target=dockerRun)
		my_thread.start()
		print "thread run: "+str(i)+"\n"
		sleep(1)

最后目录下一共有如下文件:

create.py Dockerfile setupbob.py sources.list

最后在当前目录下执行

docker build -t myubuntu:v1 .
python create.py

即可

使用docker ps -a可看见生成100个容器
并且在mydocker目录下会生成time.txt文件
如果发现copy错误,同时会生成error.txt

猜你喜欢

转载自blog.csdn.net/qq_20817327/article/details/81779569