第5次系统综合实践
2、构建容器
Dockfile:
FROM python MAINTAINER czh WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install -r requirements.txt -i https://pypi.douban.com/simple #修改源并安装依赖 ENTRYPOINT [ "python" ] CMD [ "hello.py" ]
requirements.txt:
PyMySQL
cryptography #连接sql
opencv-python
建立镜像:
3、程序部署
1.hello.py:
print('hello world')
docker run --rm -v /home/ubuntu/Documents/py/myfile:/usr/src/app py hello.py
2.date.py:
# 引入日历模块 import calendar # 输入指定年月 yy = int(input("输入年份: ")) mm = int(input("输入月份: ")) # 显示日历 print(calendar.month(yy,mm))
docker run -it -v /home/ubuntu/Documents/py/myfile:/usr/src/app --rm python:v1 date.py
3.mysql.py:
import pymysql # 打开数据库连接 db = pymysql.connect("pymysql","root","123456","python" ) # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # 使用 execute() 方法执行 SQL 查询 cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法获取单条数据. data = cursor.fetchone() print ("Database version : %s " % data) # 关闭数据库连接 db.close()
docker run --rm -v
/home/ubuntu/Documents/py/myfile:/usr/src/app --link=mysql:mysql py mysql.py
4.opencv.py
import cv2 import numpy as np img = cv2.imread('test.jpg',0) ret,thresh = cv2.threshold(img,127,255,0) contours,hierarchy = cv2.findContours(thresh, 1, 2) cnt = contours[0] M = cv2.moments(cnt) print (M)
docker run -it --rm -v /home/ubuntu/Documents/py/myfile:/usr/src/app py opencv.py
4、总结
总共花费了大概3个小时,实验内容比上次较少