一、构建镜像
- Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
ENTRYPOINT ["python"]
- requirements
PyMySQL
opencv-python
创建镜像
sudo docker build -t mypython .
二、程序运行
1.helloworld
- helloworld.py
print("Hello World")
- 运行
sudo docker run -it -v ~/docker_py/app:/usr/src/app --rm mypython helloworld.py
2.日历输出
- mycalendar.py
# 引入日历模块
import calendar
# 输入指定年月
yy = int(input("输入年份: "))
mm = int(input("输入月份: "))
# 显示日历
print(calendar.month(yy,mm))
- 运行
sudo docker run -it -v ~/docker_py/app:/usr/src/app --rm mypython mycalendar.py
3.mysql数据库操作
- sql.py
import pymysql
# 打开数据库连接
db = pymysql.connect(host="mymysql",user="docker",passwd="123456",db="docker_mysql",port=3306)
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
print ("Database version : %s " % data)
# 使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute("DROP TABLE IF EXISTS STUDENT")
# 使用预处理语句创建表
sql = """CREATE TABLE STUDENT (
ID CHAR(20) NOT NULL,
NAME CHAR(20) NOT NULL,
AGE INT,
SEX CHAR(10),
MAJOR CHAR(30) )"""
cursor.execute(sql)
# SQL 插入语句
sql = """INSERT INTO STUDENT(ID,
NAME, AGE, SEX, MAJOR)
VALUES ('111700312', 'hhn', 20, 'Male', 'computer science')"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 如果发生错误则回滚
db.rollback()
sql = """INSERT INTO STUDENT(ID,
NAME, AGE, SEX, MAJOR)
VALUES ('123456789', 'test', 18, 'Female', 'English')"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 如果发生错误则回滚
db.rollback()
# SQL 查询语句
sql = "SELECT * FROM STUDENT
WHERE AGE >= 18"
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
ID = row[0]
NAME = row[1]
AGE = row[2]
SEX = row[3]
MAJOR = row[4]
# 打印结果
print ("id=%s,name=%s,age=%s,sex=%s,major=%s" %
(ID, NAME, AGE, SEX, MAJOR ))
except:
print ("Error: unable to fetch data")
# SQL 更新语句
sql = "UPDATE STUDENT SET AGE = AGE + 1 WHERE SEX = '%s'" % ('Male')
try:
# 执行SQL语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 发生错误时回滚
db.rollback()
# SQL 查询语句
sql = "SELECT * FROM STUDENT
WHERE AGE >= 18"
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
ID = row[0]
NAME = row[1]
AGE = row[2]
SEX = row[3]
MAJOR = row[4]
# 打印结果
print ("id=%s,name=%s,age=%s,sex=%s,major=%s" %
(ID, NAME, AGE, SEX, MAJOR ))
except:
print ("Error: unable to fetch data")
# SQL 删除语句
sql = "DELETE FROM STUDENT WHERE AGE >= %s" % (20)
try:
# 执行SQL语句
cursor.execute(sql)
# 提交修改
db.commit()
except:
# 发生错误时回滚
db.rollback()
# SQL 查询语句
sql = "SELECT * FROM STUDENT
WHERE AGE >= 18"
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
ID = row[0]
NAME = row[1]
AGE = row[2]
SEX = row[3]
MAJOR = row[4]
# 打印结果
print ("id=%s,name=%s,age=%s,sex=%s,major=%s" %
(ID, NAME, AGE, SEX, MAJOR ))
except:
print ("Error: unable to fetch data")
# 关闭数据库连接
db.close()
运行之前的mysql容器
sudo docker run --name mymysql -d mymysql
- 运行
sudo docker run -it -v ~/docker_py/app:/usr/src/app --rm --link=mymysql mypython sql.py
4.opencv程序
- opencv.py
import cv2
import numpy as np
img = cv2.imread('test.jpg',0)
rows,cols = img.shape
M = np.float32([[1,0,100],[0,1,50]])
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imwrite("test-output.jpg", dst, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
- 运行
sudo docker run -it -v ~/docker_py/app:/usr/src/app --rm --link=mymysql mypython opencv.py
- tree
.
├── Dockerfile
├── app
│ ├── helloworld.py
│ ├── mycalendar.py
│ ├── opencv.py
│ ├── sql.py
│ ├── test-output.jpg
│ └── test.jpg
└── requirements.txt