这星期师兄安排工作跑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')://比较md5writeFile("./data/error.txt","this container occur error!")//如果不同,则写入error.txt文件breaktime.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 partnerdeb 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 sleepdef 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

Docker容器启动自动化脚本(五)相关推荐

  1. Docker容器启动时运行 sh 脚本

    1.docker开机运行 对于Ubuntu18.04以上的系统,如果是使用命令sudo apt-get install -y docker.io安装的docker,都可以使用下列命令设置开机启动doc ...

  2. Docker容器的自动化监控实现

    本文由  网易云 发布. 近年来容器技术不断成熟并得到应用.Docker作为容器技术的一个代表,目前也在快速发展中,基于 Docker的各种应用也正在普及,与此同时 Docker对传统的运维体系也带来 ...

  3. 如何在Mysql的Docker容器启动时初始化数据库

    1.前言 Docker在开发中使用的越来越多了,最近搞了一个Spring Boot应用,为了方便部署将Mysql也放在Docker中运行.那么怎么初始化 SQL脚本以及数据呢? 我这里有两个传统方案. ...

  4. docker 容器启动顺序_Docker容器启动时初始化Mysql数据库

    1. 前言 Docker在开发中使用的越来越多了,最近搞了一个Spring Boot应用,为了方便部署将Mysql也放在Docker中运行.那么怎么初始化 SQL脚本以及数据呢? 我这里有两个传统方案 ...

  5. docker mysql数据库初始化_如何在Mysql的Docker容器启动时初始化数据库

    1.前言 Docker在开发中使用的越来越多了,最近搞了一个Spring Boot应用,为了方便部署将Mysql也放在Docker中运行.那么怎么初始化 SQL脚本以及数据呢? 我这里有两个传统方案. ...

  6. docker 容器启动后立马退出的解决方法

    docker 容器启动后立马退出的解决方法 参考文章: (1)docker 容器启动后立马退出的解决方法 (2)https://www.cnblogs.com/wangbaojun/p/1071181 ...

  7. docker容器启动几分钟之后自动退出

    2018-11-06 问题: docker容器启动几分钟之后自动退出 log日志报错 WARNING: overlay2: the backing xfs filesystem is formatte ...

  8. 解决docker容器启动时候无法映射端口的问题

    解决docker容器启动时候无法映射端口的问题 参考文章: (1)解决docker容器启动时候无法映射端口的问题 (2)https://www.cnblogs.com/Ivan-Wu/p/110828 ...

  9. Docker容器启动参数大全与详细说明

    < Docker容器启动参数大全与详细说明 > 语法: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Docker 容器启动示例,这儿以启动 ...

最新文章

  1. Ubuntu磁盘空间如何扩容
  2. linux pbr模块安装,PBR with Linux :Linux Advanced Routing Traffic Control HOWTO
  3. 图解sqlserver 2000 还原数据库的基本操作
  4. java开源图像处理ku_83 项开源视觉 SLAM 方案够你用了吗?
  5. 图论 —— 网络流 —— 最大流 —— FF 算法与 EK 算法
  6. hibernateTemplate update 修改指定字段
  7. python调用hive与java调用区别_python和hive结合使用
  8. C#调用windows API实现 smallpdf客户端程序进行批量压缩
  9. 只写c语言,求助C语言大佬 , 只会写到一个.c文件里 ,不会用.h头文件
  10. 一网打尽位图与矢量图
  11. sftp非交互式每日定时拉取增量数据文件至本地合并至存量
  12. Android APP - GPS定位并获取地理位置
  13. 风险价值VaR(Value at Risk)和损失期望值ES(Expected shortfall)的估计
  14. Python基础-day02
  15. win10 pip安装mmcv-full
  16. airtest-poco获取元素属性值attr
  17. 计算机网络社会政策环境,计算机网络设备项目申请报告范文
  18. OpenVINO 2021r1 超分辨率重建 INT8量化 - Waifu2x
  19. 第26讲 可编程逻辑器件
  20. 孜然导航系统V2.5.5_站长导航系统源码_网址导航源码

热门文章

  1. python语言if语句-Python入门教程之if语句的用法
  2. 零基础自学python的建议-关于零基础学习 Python 有什么好的建议?
  3. python软件代码示例-Python学习示例源码
  4. 21天学通python第二版-电子工业出版社-网上书店
  5. python入门经典例题-Python入门练习题(适合Python初学者做的练习题)
  6. 怎样学好python-如何短时间学习好Python?老男孩Python入门培训
  7. python循环语句-python循环语句(第十节)
  8. python编程语法-Python基础及语法(十三)
  9. opencv python 中cv2.putText()函数的用法
  10. 用PIL读取保存图片错误 :OSError: cannot write mode RGBA as JPEG