3分钟将Python应用容器化
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int("5000"), debug=True)
python index.py
后,在浏览器上访问 http://127.0.0.1:5000
,会看到浏览器上显示了 'Hello World!' 说明程序没有问题。如果我们把这个文件发给别人,那么他需要做以下 3 步才能启动这个 web 应用。安装 Python 解释器
安装 flask 库
执行
python index.py
requirements.txt
文件中:Flask==1.1.2
FROM python:alpine3.8
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD python ./index.py
docker build
命令。docker build --tag somenzz-app ./
$ docker images | grep somenzz
somenzz-app latest b7e2359056bb 3 hours ago 89.1MB
somenzz/my-kali latest 975cbe3a4619 2 weeks ago 1.83GB
$ docker run --name python-app -p 5000:5000 somenzz-app
* Serving Flask app "index" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 201-219-912
172.17.0.1 - - [14/Oct/2020 22:32:05] "GET / HTTP/1.1" 200 -
127.0.0.0:5000
就相当于访问容器的 5000 端口。-d
参数:$ docker run -d --name python-app -p 5000:5000 somenzz-app
259d09fbbf77a6fd680cd7527600d055cb76fe3d6792063846103b47360210f9
$ docker container list | grep somenzz-app
259d09fbbf77 somenzz-app "/bin/sh -c 'python …" 20 minutes ago Up 20 minutes 0.0.0.0:5000->5000/tcp python-app
评论