hello.py
#!/usr/bin/pythonimport sysprint("hello there!")print(sys.version)
Dockerfile
#!/usr/bin/pythonFROM python:3.8COPY hello.py /tmp/CMD ["python", "/tmp/hello.py"]
# docker build -t hello .# docker run hello
- 命令行执行
docker run -it python:slim bashpython -c "import os; print(os.system('ls -l'))"python -c "import sys; print(sys.version)"
- GET
get_req.py
#!/usr/bin/pythonimport requests as reqresp = req.get("http://webcode.me")print(resp.text)
Dockerfile
FROM python:slimRUN pip install requestsCOPY get_req.py /tmp/CMD ["python", "/tmp/get_req.py"]
# docker build -t pygetreq .# docker run pygetreq
- flask
app.py
#!/usr/bin/pythonfrom flask import Flaskapp = Flask(__name__)@app.route('/')def hello(): return 'Hello there!'
Dockerfile
FROM python:slimCOPY app.py /app/WORKDIR /appRUN pip install flaskRUN export FLASK_APP=app.pyEXPOSE 5000CMD ["/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/pythonimport pymysqlcon = 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 mariadbRUN apt-get update && apt-get install -y \ python3.8 \ python3-pipRUN pip3 install pymysqlADD schema.sql /docker-entrypoint-initdb.dENV MYSQL_USER=user7ENV MYSQL_PASSWORD=7userENV MYSQL_DATABASE=testdbENV MYSQL_ROOT_PASSWORD=s$cretEXPOSE 3306
# docker build -t flasksimple .# docker run -p 3306:3306 pymaria-simple
文章转载于:https://www.jianshu.com/p/856713b10f96
原著是一个有趣的人,若有侵权,请通知删除
还没有人抢沙发呢~