docker 部署 flask(三)高级编写及生成镜像,安装requirements.txt
简介:
上一篇,我写了如何使用别人的docker基础镜像,生成我们的docker镜像。
也就最基本的flask,没有别的库(包)支持。连数据库支持都没有。
也就让大家了解一下怎么生成镜像而已。
本篇介绍如何在docker镜像中安装库(包)支持。
其实我也被卡了2天,没搞定。
https://github.com/tiangolo/uwsgi-nginx-flask-docker/issues/38
在作者的 github问了,还是自己回答。哥真NB,自己提问,自己解答。
一:选择性的忽略
作者网站的使用说明中有这么一段:
Working with submodules
After adding all your modules you could end up with a file structure similar to (taken from the example project):
.
├── app
│ ├── app
│ │ ├── api
│ │ │ ├── api.py
│ │ │ ├── endpoints
│ │ │ │ ├── __init__.py
│ │ │ │ └── user.py
│ │ │ ├── __init__.py
│ │ │ └── utils.py
│ │ ├── core
│ │ │ ├── app_setup.py
│ │ │ ├── database.py
│ │ │ └── __init__.py
│ │ ├── __init__.py
│ │ ├── main.py
│ │ └── models
│ │ ├── __init__.py
│ │ └── user.py
│ └── uwsgi.ini
└── Dockerfile
Make sure you follow the offical docs while importing your modules:
For example, if you are in app/app/main.py and want to import the module in app/app/core/app_setup.py you would wirte it like:
from .core import app_setup
or
from app.core import app_setup
And if you are in app/app/api/endpoints/user.py and you want to import the users object from app/app/core/database.py you would write it like:
from ...core.database import users
or
from app.core.database import users
也就是我们上一篇做的示例二吧。
在我们只有main这一个文件的时候,flask工作良好。
我尝试把以前编写好的flask放进去,就出现无法引用的情况。
按网站使用说明,是要给引用前面加. 或者加app/
这可真不方便。
本地调试良好的flask,上传还要改几行?文件要是多了,还不乱套。
忽略这种方法吧。或许以后写更大规模的flask,会用这种方式。
二:方便使用的方法
QuickStart for bigger projects structured as a Python package Note: You can download the example-flask-package-python3.6.zip project example and use it as an example or template for your project from the section Examples above. You should be able to follow the same instructions as in the "QuickStart" section above, with some minor modifications: Instead of putting your code in the app/ directory, put it in a directory app/app/. Add an empty file __init__.py inside of that app/app/ directory. Add a file uwsgi.ini inside your app/ directory (that is copied to /app/uwsgi.ini inside the container). In your uwsgi.ini file, add: [uwsgi] module = app.main callable = app The explanation of the uwsgi.ini is as follows: The module in where my Python web app lives is app.main. So, in the package app (/app/app), get the main module (main.py). The Flask web application is the app object (app = Flask(__name__)). Your file structure would look like: . ├── app │ ├── app │ │ ├── __init__.py │ │ ├── main.py │ └── uwsgi.ini └── Dockerfile ...instead of: . ├── app │ ├── main.py └── Dockerfile ...after that, everything should work as expected. All the other instructions would apply normally.
其中第二个:
.
├── app
│ ├── main.py
└── Dockerfile
这个方式是我使用良好的。
三:演示一下
[root@localhost temp]# tree . ├── app │ ├── alembic.ini │ ├── build requirement.py │ ├── DBconfig.py │ ├── excel │ │ └── 2017101618431654.xls │ ├── ghostdriver.log │ ├── main.py │ ├── migrate │ │ ├── env.py │ │ ├── pycache │ │ │ └── env.cpython-36.pyc │ │ ├── README │ │ └── script.py.mako │ ├── Plan.db │ ├── requirements.txt │ ├── static │ │ ├── css │ │ │ └── bootstrap-reboot.min.css.map │ │ ├── DatePicker │ │ │ ├── css │ │ │ │ └── bootstrap-datepicker.standalone.min.css │ │ │ ├── js │ │ │ │ └── bootstrap-datepicker.min.js │ │ │ └── locales │ │ │ └── bootstrap-datepicker.zh-TW.min.js │ │ ├── favicon.ico │ │ ├── js │ │ │ └── respond.min.js │ │ ├── test.png │ │ └── timg.jpg │ ├── templates │ │ └── Untitled-1.htm │ ├── test.py │ ├── TODO.txt │ └── tools │ ├── cc.json │ ├── geckodriver.log │ ├── ghostdriver.log │ ├── phantomjs.exe │ ├── pycache │ │ ├── spider.cpython-36.pyc │ │ └── xpath.cpython-36.pyc │ ├── read_plan.py │ ├── spider.py │ └── xpath.py ├── docker-compose.override.yml ├── docker-compose.yml └── Dockerfile
请忽视yml文件。
打包的临时目录只有app目录,其中是完整的flask项目。
还有Dockerfile文件。
FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7 COPY ./app /app RUN pip install -r /app/app/requirements.txt
requirements.txt是项目虚拟环境生成的库依赖列表。
可以用pip install 来安装。
dockerfile定了了使用基础镜像tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7
并把app目录加入镜像目录/app
然后就是运行pip install 安装整个环境依赖库。
当我尝试使用QuickStart for bigger projects structured as a Python package当中第一个文件结构打包

.
├── app
│ ├── app
│ │ ├── __init__.py
│ │ ├── main.py
│ └── uwsgi.ini
└── Dockerfile
就不能正常工作了。
虽然可以更改源码来使它工作正常。但是很不方便。
作者:上官飞鸿
出处:https://www.cnblogs.com/jackadam/p/8447094.html
版权:本作品采用「知识共享-署名-非商业性-禁止演绎(CC-BY-NC-ND)」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现