• 第5次实践作业


    容器内运行python程序

    一、基础文档编写

    1.文件夹内编写Dockfile

    Dockerfile

    FROM python:3
    MAINTAINER caifeng
    WORKDIR /usr/local/app 
    COPY requirement.txt ./
    RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirement.txt  
    

    2.声明所需依赖

    requirement.txt

    PyMySQL
    opencv-python
    

    3.构建镜像

    docker build -t temp_python .
    

    二、程序的部署运行

    (1)运行mysql容器

    sudo docker run --name titinmysql -p 3306:3306 -d titinmysql
    

    直接用上次实践二用过的容器

    (2)运行python容器

    sudo docker run -it -v /home/caifeng/ex5/ex5_part/app:/usr/local/app --link=titinmysql:titinmysql -d temp_python
    

    三、编辑各类py文档

    1.helloworld

    hello.py

    print('hello world')
    

    2.日历输出

    date.py

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

    3.mysql

    import pymysql
    # 打开数据库连接
    db = pymysql.connect(host="titinmysql",user="root",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)
    # 使用预处理语句创建表
    sql = """CREATE TABLE IF NOT EXISTS user (
             id  CHAR(20) NOT NULL,
             name  CHAR(20) DEFAULT NULL,
             sex CHAR(20) DEFAULT NULL)"""
    cursor.execute(sql)
    # SQL 插入语句
    sql = """INSERT INTO user(id,
             name,sex)
             VALUES ('2408', 'titin','male')"""
    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()
    

    4.opencv

    import cv2
    img=cv2.imread('pussy.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.')
    

    三、运行py程序

    1.查看容器内当前目录下的py程序

    sudo docker exec -it 93f218055a3b /bin/bash
    ls #查看文件
    


    2.运行各类文档
    halo.py

    ph.py

    可以看到出现了test-rotated.jpg

    date.py

    db.py

    查看表结构

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

    出现以下错误

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    

    解决方法:
    1.#ps -A|grep mysql
    显示类似:
    1829 ? 00:00:00 mysqld_safe
    1876 ? 00:00:31 mysqld
    2.#kill -9 1829
    3.#kill -9 1876
    4.#/etc/init.d/mysql restart
    5.#mysql -u root -p
    ok!
    参考博客:https://blog.csdn.net/lmss82/article/details/4414178

    四、小结

      这次做实验花费时间不是很多,大概5个小时,其实可以更快的,我很早就完成了大部分的工作,只剩最后一些步骤卡在一些bug上面,所以其实这次实践我大部分时间都在debug,因为自己的粗心大意,最后一步不是十分顺利。但是总体还是收获满满,尤其是这两次的实验,让我一扫之前关于下载的想法,换源真的十分神奇,之后下载东西的第一步一定是换源,这对整个实验效率都能够提升许多,这也是之前几次实践积攒下来的经验。看到注意事项里是在容器里运行python,但是看到很多人在外部终端进行操作,也不确定自己做对了没有。总之,完成了这一次实验还是比较轻松的(相对于上一次)。

  • 相关阅读:
    HTML简介
    web服务器的原理
    静态网页和动态网页的区别
    HTTP协议相关原理
    C/S,B/S的应用的区别
    git撤销commit但未push的文件
    表单提交不刷新页面
    httpclient请求中文乱码问题
    web项目,@return@see@param等注解Maven install报错,不能识别
    Hive 基本操作
  • 原文地址:https://www.cnblogs.com/TITIN24/p/12913632.html
Copyright © 2020-2023  润新知