一.python镜像布置
dockerfile
FROM python:3
MAINTAINER lx
WORKDIR /app
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt -i https://pypi.douban.com/simple #修改源并安装依赖
ENTRYPOINT ["python"]
CMD ["helloworld.py"] #默认打开文件
requirements
PyMySQL
opencv-python
helloworld.py
print('hello world')
date.py
import calendar
# 输入指定年月
yy = int(input("输入年份: "))
mm = int(input("输入月份: "))
# 显示日历
print(calendar.month(yy,mm))
db.py
import pymysql
# 打开数据库连接
db = pymysql.connect("mysql", "docker", "123456", "docker_mysql")
#创建游标对象
cursor = db.cursor()
#先查询一次数据库数据
sql = """select * FROM test"""
cursor.execute(sql)
results = cursor.fetchall()
print(results)
#SQL插入语句
sql="""insert test(id,name)
values(123456,'A')"""
cursor.execute(sql)
db.commit()
#插入完成后再读取一次数据库数据
sql = """select * FROM test"""
cursor.execute(sql)
results = cursor.fetchall()
print(results)
# 关闭数据库连接
db.close()
op.py
#做一个图片翻转功能
import cv2
img=cv2.imread('test.jpg',flags=1)
rows,cols=img.shape[:2]
M=cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
dst=cv2.warpAffine(img,M,(cols,rows))
cv2.imwrite("test-rotated.jpg", dst, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
print('rotated and saved.')
二.Hello World
sudo docker run -it --rm -v /home/lx/python/app:/app python:test1 helloworld.py
三.日历输出
sudo docker run -it --rm -v /home/lx/python/app:/app python:test1 date.py
四.mysql数据库操作
sudo docker run -it --rm -v /home/lx/python/app:/app --link=mysql:mysql python:test1 db.py
未执行py文件前
执行后
五.opencv程序
sudo docker run -it --rm -v /home/lx/python/app:/app python:test1 op.py