第五次实践作业

一、搭建Python镜像

Dockerfile

FROM python
MAINTAINER James
WORKDIR /app     #工作目录
COPY ./requirements.txt /requirements.txt  #添加依赖文件
RUN pip install -r /requirements.txt -i https://pypi.douban.com/simple  #修改源并安装依赖
ENTRYPOINT [ "python" ]  # 实现命令行式调用容器
CMD [ "test.py" ]  #设置ENTRYPOINT默认参数

requirements.txt

PyMySQL
opencv-python

搭建镜像

sudo docker build -t docker-python .

二、运行简单py文件

hello world

print('hello world')

输出日历

import calendar
yy = int(input("输入年份: "))
mm = int(input("输入月份: "))
print(calendar.month(yy,mm))

mysql数据库操作

#mysql1.py
#!/usr/bin/python3
 
import pymysql
 
# 打开数据库连接
db = pymysql.connect(host="192.168.0.110",user="docker",password="123456",database="docker_mysql" )

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
 
# 使用 execute()  方法执行 SQL 查询 
cursor.execute("SELECT VERSION()")
 
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
 
print ("Database version : %s " % data)
 
# 关闭数据库连接
db.close()
sudo docker run --rm -v /home/y00/docker-python/app:/app --link=mysql1:mysql1 docker-python mysql1.py

插入数据

#mysql3.py
#!/usr/bin/python3
 
import pymysql
 
# 打开数据库连接
db = pymysql.connect(host="192.168.0.110",user="docker",password="123456",database="docker_mysql" )
 
# 使用cursor()方法获取操作游标 
cursor = db.cursor()
 
# SQL 插入语句
sql = """INSERT INTO EMPLOYEE(id,name)
         VALUES (31702427,frx)"""
try:
   # 执行sql语句
   cursor.execute(sql)
   # 提交到数据库执行
   db.commit()
except:
   # 如果发生错误则回滚
   db.rollback()
 
# 关闭数据库连接
db.close()

删除数据

#mysql6.py
#!/usr/bin/python3
 
import pymysql
 
# 打开数据库连接
db = pymysql.connect(host="192.168.0.110",user="docker",password="123456",database="docker_mysql" )
 
# 使用cursor()方法获取操作游标 
cursor = db.cursor()
 
# SQL 删除语句
sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (20)
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 提交修改
   db.commit()
except:
   # 发生错误时回滚
   db.rollback()
 
# 关闭连接
db.close()

opencv部署运行

import cv2
import numpy as np

img = cv2.imread("mssi5.jpg", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]

dst = np.zeros(imgInfo, np.uint8)

for i in range( height ):
    for j in range( width - 100 ):
        dst[i, j + 100] = img[i, j]

cv2.imwrite("test-rotated.jpg", dst, [int(cv2.IMWRITE_JPEG_QUALITY), 100])

三、心得

总共花了三个多小时,比上次简单了许多,主要是前面有搭建好的数据库镜像还有成功的经验

posted @ 2020-05-22 21:58  Zoe丨  阅读(141)  评论(0编辑  收藏  举报