python3 image_python docker快速入门3制作image

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值