美文网首页
python docker快速入门3制作image

python docker快速入门3制作image

作者: python测试开发 | 来源:发表于2020-09-03 17:59 被阅读0次

    hello.py

    #!/usr/bin/python
    
    import sys
    
    print("hello there!")
    print(sys.version)
    

    Dockerfile

    #!/usr/bin/python
    
    FROM python:3.8
    COPY hello.py /tmp/
    CMD ["python", "/tmp/hello.py"]
    
    # docker build -t hello .
    # docker run hello
    

    命令行执行

    docker run -it python:slim bash
    python -c "import os; print(os.system('ls -l'))"
    python -c "import sys; print(sys.version)"
    
    • GET

    get_req.py

    #!/usr/bin/python
    
    import requests as req
    
    resp = req.get("http://webcode.me")
    
    print(resp.text)
    

    Dockerfile

    FROM python:slim
    RUN pip install requests
    COPY get_req.py /tmp/
    CMD ["python", "/tmp/get_req.py"]
    
    # docker build -t pygetreq .
    # docker run pygetreq
    

    flask

    app.py

    #!/usr/bin/python
    
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return 'Hello there!'
    

    Dockerfile

    FROM python:slim
    
    COPY app.py /app/
    WORKDIR /app
    RUN pip install flask
    RUN export FLASK_APP=app.py
    
    EXPOSE 5000
    CMD ["/usr/local/bin/flask", "run", "--host", "0.0.0.0"]
    
    
    # docker build -t flasksimple .
    # docker run -p 5000:5000 flasksimple
    

    参考资料

    MariaDB

    app.py

    #!/usr/bin/python
    
    import pymysql
    
    con = pymysql.connect(host='localhost', user='user7',
                password='7user', database='testdb', port=3306)
    
    try:
        with con.cursor() as cur:
    
            cur.execute('SELECT * FROM cities')
    
            rows = cur.fetchall()
    
            for row in rows:
                print(f'{row[0]}, {row[1]}, {row[2]}')
    
    finally:
    
        con.close()
    

    Dockerfile

    FROM mariadb
    
    RUN apt-get update && apt-get install -y \
        python3.8 \
        python3-pip
    
    RUN pip3 install pymysql
    
    ADD schema.sql /docker-entrypoint-initdb.d
    
    ENV MYSQL_USER=user7
    ENV MYSQL_PASSWORD=7user
    ENV MYSQL_DATABASE=testdb
    ENV MYSQL_ROOT_PASSWORD=s$cret
    
    EXPOSE 3306
    
    
    # docker run -p 3306:3306 pymaria-simple
    #  ./app.py 
    1, Bratislava, 432000
    2, Budapest, 1759000
    3, Prague, 1280000
    4, Warsaw, 1748000
    5, Los Angeles, 3971000
    6, New York, 8550000
    7, Edinburgh, 464000
    8, Berlin, 3671000
    

    相关文章

      网友评论

          本文标题:python docker快速入门3制作image

          本文链接:https://www.haomeiwen.com/subject/cleosktx.html