2020系统综合实践 第5次实践作业

hello world

dockerfile

FROM python
RUN touch /usr/src/helloworld.py 
	&& echo "print("hello world")" >> /usr/src/helloworld.py
WORKDIR /usr/src
ENTRYPOINT ["python"]
CMD ["./helloworld.py"]

使用docker build -t helloworld . 构建镜像后

使用docker run --rm helloword命令就能运行 hello world 容器执行py程序


日历输出

dockerfile

FROM python
COPY shcalendar.py /usr/src/
WORKDIR /usr/src
ENTRYPOINT ["python"]
CMD ["./shcalendar.py"]

python源代码

import calendar

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

使用docker build -t showcalendar .构建镜像

使用docker run -it --rm showcalendar启动容器


mysql数据库操作

pip.conf

[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn  
# trusted-host 此参数是为了避免麻烦,否则使用的时候可能会提示不受信任

Dockerfile

FROM python

RUN mkdir /usr/src/mysqlexam 
	&& mkdir ~/.pip
COPY pip.conf ~/.pip/
COPY sql.py /usr/src/mysqlexam/
WORKDIR /usr/src/mysqlexam
RUN touch requirements.txt 
	&& echo "PyMySQL" >> requirements.txt 
	&& pip install --no-cache-dir -r requirements.txt

ENTRYPOINT [ "python" ]
CMD [ "./sql.py" ]

sql.py

import pymysql
# 打开数据库连接
db = pymysql.connect("mysql_image, "gp", "123456", "Nil")
#host="...",user="...",password="...",db="...",port=...,use_unicode=..., charset="utf8"
#创建游标对象

cursor = db.cursor()
#先查询一次数据库数据
sql = """select * FROM test"""
cursor.execute(sql)
results = cursor.fetchall()
print(results)

#SQL插入语句
sql="""insert test(id,name)
values(111,'X')"""
cursor.execute(sql)
db.commit()

#插入完成后再读取一次数据库数据
sql = """select * FROM test"""
cursor.execute(sql)
results = cursor.fetchall()
print(results)

# 关闭数据库连接
db.close()

使用docker build -t pymysql.

命令构建完成镜像后,先不着急启动该容器,而是先启动需要使用到的数据库容器

在数据库准备完成后,使用docker run --rm --link mydb pymysql命令运行python程序(之前容器做完顺手删了没有结果)


opencv

python源代码

import cv2
# 转换为灰度图片
grayimage = cv2.imread("./temp.jpeg",cv2.IMREAD_GRAYSCALE)
# 保存图片
cv2.imwrite("./_temp.jpeg",grayimage)
print("Picture has been converted to gray.")

Dockerfile

FROM python
RUN mkdir /usr/src/myopencv 
	&& mkdir ~/.pip
COPY pip.conf ~/.pip/
VOLUME /usr/src/myopencv
WORKDIR /usr/src/myopencv
RUN touch requirements.txt 
	&& echo "opencv-python" >> requirements.txt 
	&& pip install --no-cache-dir -r requirements.txt
ENTRYPOINT [ "python" ]
CMD [ "convertToGray.py" ]

pip.conf

[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn  
# trusted-host 此参数是为了避免麻烦,否则使用的时候可能会提示不受信任

使用docker build -t myopencv .构建镜像并使用

docker run -v /var/lib/myopencvlib:/usr/src/myopencv myopencv启动容器:
结果:


总结

​ 本次实验前前后后主要用去了4个小时的时间完成。其中数据库由于之前顺手删容器没有做下去,其他相对比较顺利。实验主要时间都花费在py编写和build的时候等待上。

​ 遇到一些细节上的问题。

​ The command '/bin/sh -c apt-get install -y git' returned a non-zero code,忘记顺手换源,虽然本机pip是换过源的。

​ [docker] invalid argument "" for "-t, --tag" flag: invalid reference format: repository name must be编写dockerfile时可能用了中文拼音,导致build的时候报了找不到dockerfile的错误,重命名之后就没事了。

原文地址:https://www.cnblogs.com/gp131415/p/12936601.html