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

    一、镜像构建

    (1)编写Dockerfile

    FROM python
    MAINTAINER cathy
    WORKDIR /usr/local/app
    COPY requirements.txt ./ 
    RUN pip install -r requirements.txt -i https://pypi.douban.com/simple  
    

    (2)声明所需依赖

    requirements.txt

    PyMySQL
    opencv-python
    

    (3)构建镜像

    sudo docker build -t my-python .
    

    二、程序的部署运行

    (1)运行mysql容器

    这里使用第二次实践中的mysql容器

    sudo docker run --name kmysql -d kmysql
    

    (2)运行并进入python容器,查看容器内当前目录下的py程序

    sudo docker run -it -v /home/cathy/ex5_all/ex5/app:/usr/local/app --link=kmysql:kmysql -d my-python
    sudo docker ps # 查看id
    sudo docker exec -it [id] /bin/bash # 进入容器
    ls # 进入容器后查看
    

    (3)helloworld

    hello.py

    print('hello world')
    

    运行

    (4)日历输出

    date.py

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

    运行

    (5)mysql数据库操作

    db.py

    import pymysql
     
    # 打开数据库连接
    db = pymysql.connect("kmysql", "root", "123456", "docker_mysql")
    
    # 使用 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 user")
    
    sql = """CREATE TABLE IF NOT EXISTS user (
             id  varchar(20) NOT NULL,
             name varchar(20) DEFAULT NULL,
             sex varchar(10) DEFAULT NULL)"""
    
    cursor.execute(sql)
    
    # SQL 插入语句
    sql = """INSERT INTO user(id,
             name,sex)
             VALUES ('666', 'kkk','female')"""
    try:
       # 执行sql语句
       cursor.execute(sql)
       # 提交到数据库执行
       db.commit()
    except:
       # 如果发生错误则回滚
       db.rollback()
    
    # SQL 查询语句
    sql = "SELECT * FROM user"
    
    try:
       # 执行SQL语句
       cursor.execute(sql)
       # 获取所有记录列表
       results = cursor.fetchall()
       for row in results:
          id = row[0]
          name = row[1]
          sex = row[2]
           # 打印结果
          print ("id=%s,name=%s,sex=%s" % 
                 (id, name, sex))
    except:
       print ("Error: unable to fetch data")
    
    # 关闭数据库连接
    db.close()
    
    

    运行

    进入数据库的容器确认

    (6)opencv

    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.')
    

    运行

    原图和旋转后的图片:

    项目结构如下:

    三、遇到的问题及解决方法

    (1)在python容器内运行db.py时遇到如下问题:

    1.首先确定密码、数据库等设置是否正确

    看到可以用设定好的密码登陆到数据库,同时也存在docker_mysql数据库

    2.测试两个容器的连通性

    可以看到两个容器是连通的

    3.在博客中找到了如下方法

    4.解决了以上问题后就剩语法问题了

    检查了一下发现少了一个)

    之后就可以运行db.py啦

    四、花费时间

    作业名称 耗时(小时)
    镜像构建 2
    程序的部署运行 6
    博客编写 1.5
    总计 9.5

    五、小结

      在查阅资料,确定操作步骤的时候觉得相比起实践四,这次算是比较容易的,认为出错可能性比较的一步是连接数据库,确实在进行这个操作的时候出了问题。这边还花了挺多时间,首先是一开始不确定哪里出了问题,考虑了有连接问题,配置,权限问题等等等等,刚准备爬坑的时候还挺头痛的,乱折腾了一阵子以后才觉得不行,还是决定耐心一点一步一步来,先确定密码等是否正确,再检查容器连接,错误一个一个被解决了,开心。
      碰到问题还是得多查阅资料,分析哪里出了问题后再解决,会比较有思路。这次实践是学习了使用docker容器运行Python程序,并且进一步熟悉了之前学习的相关操作,对理论也有了进一步的认识。

  • 相关阅读:
    Hdu 4221 Greedy?
    Hdu 2955 Robberies
    Hdu 3309 Roll The Cube
    Hdu 2602 Bone Collector
    Hdu 2844 Coins
    Hdu 2255奔小康赚大钱
    Hdu 2120 Ice_cream's world I
    Hdu 2159 FATE
    Hdu 2102 A计划
    Hdu 2098分拆素数和
  • 原文地址:https://www.cnblogs.com/cathyccathy/p/12913635.html
Copyright © 2011-2022 走看看