zoukankan      html  css  js  c++  java
  • 第5次实践作业

    1.项目结构

    2.Python容器&文件

    (1)文件

    • requirements.txt
    PyMySQL
    opencv-python
    
    • Dockerfile
    FROM python
    WORKDIR /usr/src/app
    
    COPY requirements.txt ./
    
    RUN pip install  --default-timeout=100 --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
    
    VOLUME /usr/src/app
    ENTRYPOINT [ "python" ] # 实现命令行式调用容器
    CMD [ "hello.py" ] # ENTRYPOINT默认参数
    
    • hello.py
    print('hello world')
    
    • date.py
    # Filename : test.py
    # author by : HuangYH
    # 引入日历模块
    import calendar
     
    # 输入指定年月
    yy = int(input("输入年份: "))
    mm = int(input("输入月份: "))
    
    # 显示日历
    print(calendar.month(yy,mm))
    
    • cv.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.') 
    

    实现图片翻转

    • db.py
    import pymysql
    
    # 打开数据库连接
    db = pymysql.connect("mysqlhyh", "hyh", "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(250,'Wulin')"""
    cursor.execute(sql)
    db.commit()
    
    #插入完成后再读取一次数据库数据
    sql = """select * FROM test"""
    cursor.execute(sql)
    results = cursor.fetchall()
    print(results)
    
    # 关闭数据库连接
    db.close()
    

    (2)构建镜像

    • 命令:sudo docker build -t ex5_image .

    • 命令sudo docker run --rm -v /home/exam5/python:/usr/src/app exam5_image

      • 参数说明
        • 使用-v将本地目录挂载到python
          工作目录以方便代码修改
        • --rm运行完毕后自动移除容器
        • 默认使用Dockerfile中指定的hello.py作为参数
    • date.py
      sudo docker run -it --rm -v /home/exam5/python:/usr/src/app exam5_image date.py

      • 参数说明
        • date.pyENTRYPOINT的参数
        • 由于要实现交互,所以要加-it参数
    • 数据库操作

      • 运行实验2的数据库 第2次实验

      • 命令sudo docker run -it --rm -v /home/exam5/python:/usr/src/app --link=msql:msql exam5_image db.py

      • 参数说明:

        • --link=容器名:容器别名` 实现容器间的互访
    • OpenCV

      • 命令sudo docker run -it --rm -v /home/ex5/python:/usr/src/app ex5_image cv.py

    3.用时&心得

    时间统计

    • 查资料0.5小时
    • 动手1.5小时
    • 写博客0.5小时
    • 合计3小时左右

    心得

      这次实验比较轻松yeah!
    
  • 相关阅读:
    Luogu3119 [USACO15JAN]Grass Cownoisseur G
    BZOJ4361 isn
    洛谷1330 封锁阳光大学
    codechef AUG17 T2 Chef and Mover
    codechef AUG17 T1 Chef and Rainbow Array
    【bzoj3211】花神游历各国&&【bzoj3038】上帝造题的七分钟2
    noip 瑞士轮 ————归并排序解法
    记录string的妙用
    洛谷P1550 [USACO08OCT]打井Watering Hole
    汕头市队赛 SRM 09 C 撕书
  • 原文地址:https://www.cnblogs.com/HuangYH723/p/12937715.html
Copyright © 2011-2022 走看看