简介:

上一篇,我写了如何使用别人的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..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
│ │ └── .xls
│ ├── ghostdriver.log
│ ├── main.py
│ ├── migrate
│ │ ├── env.py
│ │ ├── pycache
│ │ │ └── env.cpython-.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-.htm
│ ├── test.py
│ ├── TODO.txt
│ └── tools
│ ├── cc.json
│ ├── geckodriver.log
│ ├── ghostdriver.log
│ ├── phantomjs.exe
│ ├── pycache
│ │ ├── spider.cpython-.pyc
│ │ └── xpath.cpython-.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.-alpine3.

COPY ./app /app
RUN pip install -r /app/app/requirements.txt

requirements.txt是项目虚拟环境生成的库依赖列表。

可以用pip install 来安装。

dockerfile定了了使用基础镜像tiangolo/uwsgi-nginx-flask:python3.6-alpine3.

并把app目录加入镜像目录/app

然后就是运行pip install 安装整个环境依赖库。

当我尝试使用QuickStart for bigger projects structured as a Python package当中第一个文件结构打包

.
├── app
│ ├── app
│ │ ├── __init__.py
│ │ ├── main.py
│ └── uwsgi.ini
└── Dockerfile

就不能正常工作了。

虽然可以更改源码来使它工作正常。但是很不方便。

docker 部署 flask(三)高级编写及生成镜像,安装requirements.txt的更多相关文章

  1. [python] [转]如何自动生成和安装requirements.txt依赖

    [转]如何自动生成和安装requirements.txt依赖 在查看别人的Python项目时,经常会看到一个requirements.txt文件,里面记录了当前程序的所有依赖包及其精确版本号.这个文件 ...

  2. pip自动生成和安装requirements.txt

    生成requirements.txt文件 pip freeze > requirements.txt 安装requirements.txt依赖 pip install -r requiremen ...

  3. 如何利用pip自动生成和安装requirements.txt依赖

    在查看别人的Python项目时,经常会看到一个requirements.txt文件,里面记录了当前程序的所有依赖包及其精确版本号.这个文件有点类似与Rails的Gemfile.其作用是用来在另一台PC ...

  4. 如何自动生成和安装requirements.txt依赖

    在查看别人的Python项目时,经常会看到一个requirements.txt文件,里面记录了当前程序的所有依赖包及其精确版本号.这个文件有点类似与Rails的Gemfile.其作用是用来在另一台PC ...

  5. 自动生成和安装requirements.txt依赖

    在查看别人的Python项目时,经常会看到一个requirements.txt文件,里面记录了当前程序的所有依赖包及其精确版本号.这个文件有点类似与Rails的Gemfile.其作用是用来在另一台PC ...

  6. pycharm中使用配置好的virtualenv环境,自动生成和安装requirements.txt依赖

    1.手动建立: 第一步 建立虚拟环境 Windows cmd: pip install virtualenv 创建虚拟环境目录 env 激活虚拟环境 C:\Python27\Scripts\env\S ...

  7. 生成和安装requirements.txt依赖

    pip freeze > requirements.txt pip install -r < requirements.txt

  8. Python 生成requirement 使用requirements.txt

    第一步:python项目中必须包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号.以便新环境部署. requirements.txt可以通过pip命令自动生成和安装 ...

  9. docker 部署 flask(二)编写及生成镜像。

    简介: 上一篇文章,我们简单的测试了一下服务器环境和docker基础镜像.并没有涉及我们自己编写的flask python程序. 现在,我们就要把我们自己的flask程序,放进docker镜像. 但是 ...

随机推荐

  1. 牛客国庆集训派对Day3 A Knight

    Knight 思路: bfs打表找规律 如下图 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) ...

  2. JAVA基础知识总结:十一

    一.内部类 一个类中包含着另外一个类,里面的类被称为内部类,外面的称为外部类 1.成员内部类 和成员变量或者成员方法平级的内部类 语法: 访问权限修饰符 class 外部类类名{ //成员变量 //成 ...

  3. android -------- Data Binding的使用 ( 四 )ListView

    今天来说说DataBinding在列表ListView中的使用 主要分为两种,1: 基本的实体类  2:Observable 定义字段 listView布局文件 <?xml version=&q ...

  4. Spring Batch 批量处理策略

    为了帮助设计和实现批量处理系统,基本的批量应用是通过块和模式来构建的,同时也应该能够为程序开发人员和设计人员提供结构的样例和基础的批量处理程序. 当你开始设计一个批量作业任务的时候,商业逻辑应该被拆分 ...

  5. Confluence 6 创建小组的公众空间

    现在是我们可以开始创建公众空间的时候了,全世界都希望知道这个项目和勇敢的探险活动. 在这个步骤中,我们将会创建一个项目小组的空间,并且将这个空间公布给全世界.这个表示的是你将会让你的 Confluen ...

  6. 第一章:IPsecVPN

    第一章 一.VPN(virtual private Network,虚拟专用网)的基本概念 VPN连接模式分为两种,分别是传输模式和隧道模式 传输模式:在整个VPN传输中,ip包头并没有被封装进去 隧 ...

  7. Building Fire Stations ZOJ - 3820 (二分,树的直径)

    大意: 给定树, 求两个点, 使得所有其他的点到两点的最短距离的最大值尽量小. 二分答案转为判定选两个点, 向外遍历$x$的距离是否能遍历完整棵树. 取直径两段距离$x$的位置bfs即可. #incl ...

  8. loj#2020. 「AHOI / HNOI2017」礼物

    题意:给定xy数组求 \(\sum_{i=0}^{n-1}(x_i+y_{(i+k)\modn}+c)^2\) 题解:先化简可得 \(n*c^2+2*\sum_{i=0}^{n-1}x_i-y_i+\ ...

  9. Redis开发规范

    1.冷热数据分离,不要将所有数据全部都放到Redis中 虽然Redis支持持久化,但是Redis的数据存储全部都是在内存中的,成本昂贵.建议根据业务只将高频热数据存储到Redis中[QPS大于5000 ...

  10. python中RabbitMQ的使用(路由键模糊匹配)

    路由键模糊匹配 使用正则表达式进行匹配.其中“#”表示所有.全部的意思:“*”只匹配到一个词. 匹配规则: 路由键:routings = [ 'happy.work',  'happy.life' , ...