伸缩自如的Flask [day 18] Docker image化--运行篇

首先,我们可以先拉一下底层的image,再把我们的app内容叠上去,从终端机(cmd)打上:

docker pull python

成功的话,可以在介面上看到新的image
http://img2.58codes.com/2024/20122678lJHdPo4C9i.png

再来,我们可以开始写dockerfile(可以放在主程式的旁边)http://img2.58codes.com/2024/20122678MPst4llzTw.png:

FROM python:3.9-busterWORKDIR /appCOPY . /appRUN apt-get updateRUN apt-get install nanoRUN pip install -r requirements.txtCMD python3 main.py 

可以看到python:3.9-buster为我们刚刚拉到的image名称,再来将目前路径的程式都放到未来container中/app的地方。更新套件及装上nano,未来如果有小地方要修改的话,可以方便我们使用

docker exec -it <container ID> bash

来进行小幅度的修改(注意,小心不要引起git版本的冲突)。
再来藉由requirements.txt来安装想使用的python套件。

在测试阶段可以先使用CMD python3 main.py ,
但是在未来需要正式营运时可以开始使用gunicorn(如果你比较喜欢uwsgi,也可以用这个套件,但是我比较喜欢绿色独角兽)。

CMD  nohup gunicorn -w 4 main:app --access-logfile gunicorn_access.txt --error-logfile gunicorn_error.txt -b :5000

nohup可以帮助我们避免关闭session时产生input/output Error,当然你如果都不print东西,也可以避免这个错误。

我们可以将那些路由被接触了,写在gunicorn_access.txt,
错误讯息写在gunicorn_error.txt,
而对外port设定为5000。

进行build image的程序,. 代表现在的路径,代表我们在dockerfile的路径上执行:

docker image build -t <喜欢的image name> .

最后将image run成container,以提供我们的服务:

docker run -p 3000:5000  <喜欢的image name>

http://img2.58codes.com/2024/20122678Axo3GS7aJE.png
对内port为3000,对外为5000,我们可以从localhost:3000来看到我们的服务。

[day 1] 开场白[day 2] blue_print[day 3] Factory mode[day 4] JWT token[day 5] session[day 6] Jinja[day 7] Post data with Form tag[day 8] ajax with jquery[day 9] request[day 10] 将资料写进DB(pymongo)[day 11] log with mongoDB[day 12] Sql Database[day 13] 档案上传[day 14] 档案下载 及 其他传值方法[day 15] boostrap 4.6[day 16] API[day 17] Docker image化--安装篇[day 18] Docker image化--运行篇[day 19] Nginx[day 20] Docker compose[day 21] Nginx with https[day 22] pythonanywhere 部署[day 23] GCP app engine (介绍)[day 24] GCP app engine (deploy)[day 25] Flask with web cam[day 26] Flask with ML[day 27] Supervisor[day 28] Flask-Mail[day 29] Line Messaging API[day 30] 结语

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章