首页 > 解决方案 > 使用 Docker 在 Flask 中部署 SpaCY 应用程序

问题描述

我有一个用 Python Flask 编写的 SpaCY 应用程序来检测命名实体,该实体将用作与另一个应用程序一起使用的服务。它是使用 Visual Studio 作为 Python 应用程序创建的,虚拟环境是 Python 版本 (3.6)。我将它作为 Windows VM 部署在 Azure 上,效果很好。

下一步是使用 Docker 进行部署。我创建了一个 Dockerfile,构建了一个镜像并运行了它。它确实运行并显示了 url 和端口。但是在访问任何已定义的路线时,我会收到“无法访问此站点”。

我能够直接运行 python 项目或通过 Visual Studio 并且没有任何问题,这让我相信问题出在我的 Dockerfile 或我对它的理解上。

作为指导,我开始遵循 Microsoft 的 Azure 教程(https://docs.microsoft.com/en-us/azure/container-instances/container-instances-tutorial-prepare-app)并使用示例项目:https:// github.com/Azure-Samples/aci-helloworld我能够构建一个 dockerfile 并访问它的路由。

现在它让我相信 SpaCY、我的 dockerfile 或 SpaCY 和 Docker 之间的某些不兼容导致了这种情况。对此的任何帮助将不胜感激。如果我遗漏任何信息,我很乐意补充。

我的项目结构:

运行服务器.py

from SpaCYParser import app

if __name__ == '__main__':
    app.run()

初始化.py

from flask import Flask
app = Flask(__name__)

解析器.py

from flask import request, jsonify, make_response
from SpaCYParser import app
import json, spacy
from SpaCYParser import parserOutput
from SpaCYParser import Entity
from SpaCYParser import NEREntities

@app.route('/')
@app.route('/test')
def ping():
    print("hello world")
    return "HELLO WORLD"
@app.route('/parse')
def parse():
    # NER Specific code.

我的 Docker 文件:

FROM tiangolo/uwsgi-nginx-flask:python3.6

LABEL Name=mparser Version=0.0.1
ENV LISTEN_PORT=5000
EXPOSE 5000

WORKDIR /mparser
COPY . /mparser
# Using pip:
RUN python -m pip install -r requirements.txt
CMD ["python", "runserver.py"]

码头工人日志:

PS C:\Users\STanwar\Documents\GitHub\mParser\SpaCYParser\SpaCYParser> docker build --rm -f ../Dockerfile -t mparser:latest .
Sending build context to Docker daemon  1.382GB
Step 1/8 : FROM tiangolo/uwsgi-nginx-flask:python3.6
 ---> 839b35a9f270
Step 2/8 : LABEL Name=mparser Version=0.0.1
 ---> Using cache
 ---> 1abd09524d8d
Step 3/8 : ENV LISTEN_PORT=5000
 ---> Using cache
 ---> 896d10f35e29
Step 4/8 : EXPOSE 5000
 ---> Using cache
 ---> aa6010d2ceea
Step 5/8 : WORKDIR /mparser
 ---> Using cache
 ---> 9f5860f4b559
Step 6/8 : COPY . /mparser
 ---> 800bc0c19ce7
Step 7/8 : RUN python -m pip install -r requirements.txt
 ---> Running in 3deceaf0c24e
Requirement already satisfied: click==6.7 in /usr/local/lib/python3.6/site-packages (from -r requirements.txt (line 1)) (6.7)
Collecting cymem==1.31.2 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/f8/9e/273fbea507de99166c11cd0cb3fde1ac01b5bc724d9a407a2f927ede91a1/cymem-1.31.2.tar.gz
Collecting cytoolz==0.8.2 (from -r requirements.txt (line 3))
  Downloading https://files.pythonhosted.org/packages/0f/e6/ccc124714dcc1bd511e64ddafb4d5d20ada2533b92e3173a4cf09e0d0831/cytoolz-0.8.2.tar.gz (386kB)
Collecting dill==0.2.8.2 (from -r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/6f/78/8b96476f4ae426db71c6e86a8e6a81407f015b34547e442291cd397b18f3/dill-0.2.8.2.tar.gz (150kB)
Collecting Flask<1 (from -r requirements.txt (line 5))
  Downloading https://files.pythonhosted.org/packages/2e/48/f1936dadac2326b3d73f2fe0a964a87d16be16eb9d7fc56f09c1bea3d17c/Flask-0.12.4-py2.py3-none-any.whl (81kB)
Requirement already satisfied: itsdangerous==0.24 in /usr/local/lib/python3.6/site-packages (from -r requirements.txt (line 6)) (0.24)
Requirement already satisfied: Jinja2==2.10 in /usr/local/lib/python3.6/site-packages (from -r requirements.txt (line 7)) (2.10)
Requirement already satisfied: MarkupSafe==1.0 in /usr/local/lib/python3.6/site-packages (from -r requirements.txt (line 8)) (1.0)
Collecting msgpack-numpy==0.4.1 (from -r requirements.txt (line 9))
  Downloading https://files.pythonhosted.org/packages/2e/43/393e30e2768b0357541ac95891f96b80ccc4d517e0dd2fa3042fc8926538/msgpack_numpy-0.4.1-py2.py3-none-any.whl
Collecting msgpack-python==0.5.6 (from -r requirements.txt (line 10))
  Downloading https://files.pythonhosted.org/packages/8a/20/6eca772d1a5830336f84aca1d8198e5a3f4715cd1c7fc36d3cc7f7185091/msgpack-python-0.5.6.tar.gz (138kB)
Collecting murmurhash==0.28.0 (from -r requirements.txt (line 11))
  Downloading https://files.pythonhosted.org/packages/5e/31/c8c1ecafa44db30579c8c457ac7a0f819e8b1dbc3e58308394fff5ff9ba7/murmurhash-0.28.0.tar.gz
Collecting numpy==1.14.5 (from -r requirements.txt (line 12))
  Downloading https://files.pythonhosted.org/packages/68/1e/116ad560de97694e2d0c1843a7a0075cc9f49e922454d32f49a80eb6f1f2/numpy-1.14.5-cp36-cp36m-manylinux1_x86_64.whl (12.2MB)
Collecting pathlib==1.0.1 (from -r requirements.txt (line 13))
  Downloading https://files.pythonhosted.org/packages/ac/aa/9b065a76b9af472437a0059f77e8f962fe350438b927cb80184c32f075eb/pathlib-1.0.1.tar.gz (49kB)
Collecting pip==9.0.3 (from -r requirements.txt (line 14))
  Downloading https://files.pythonhosted.org/packages/ac/95/a05b56bb975efa78d3557efa36acaf9cf5d2fd0ee0062060493687432e03/pip-9.0.3-py2.py3-none-any.whl (1.4MB)
Collecting plac==0.9.6 (from -r requirements.txt (line 15))
  Downloading https://files.pythonhosted.org/packages/9e/9b/62c60d2f5bc135d2aa1d8c8a86aaf84edb719a59c7f11a4316259e61a298/plac-0.9.6-py2.py3-none-any.whl
Collecting preshed==1.0.0 (from -r requirements.txt (line 16))
  Downloading https://files.pythonhosted.org/packages/1b/ac/7c17b1fd54b60972785b646d37da2826311cca70842c011c4ff84fbe95e0/preshed-1.0.0.tar.gz (89kB)
Collecting pyreadline==2.1 (from -r requirements.txt (line 17))
  Downloading https://files.pythonhosted.org/packages/bc/7c/d724ef1ec3ab2125f38a1d53285745445ec4a8f19b9bb0761b4064316679/pyreadline-2.1.zip (109kB)
Collecting regex==2017.4.5 (from -r requirements.txt (line 18))
  Downloading https://files.pythonhosted.org/packages/36/62/c0c0d762ffd4ffaf39f372eb8561b8d491a11ace5a7884610424a8b40f95/regex-2017.04.05.tar.gz (601kB)
Collecting setuptools==39.0.1 (from -r requirements.txt (line 19))
  Downloading https://files.pythonhosted.org/packages/20/d7/04a0b689d3035143e2ff288f4b9ee4bf6ed80585cc121c90bfd85a1a8c2e/setuptools-39.0.1-py2.py3-none-any.whl (569kB)
Collecting six==1.11.0 (from -r requirements.txt (line 20))
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting spacy==2.0.11 (from -r requirements.txt (line 21))
  Downloading https://files.pythonhosted.org/packages/3c/31/e60f88751e48851b002f78a35221d12300783d5a43d4ef12fbf10cca96c3/spacy-2.0.11.tar.gz (17.6MB)
Collecting termcolor==1.1.0 (from -r requirements.txt (line 22))
  Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz
Collecting thinc==6.10.2 (from -r requirements.txt (line 23))
  Downloading https://files.pythonhosted.org/packages/55/fd/e9f36081e6f53699943381858848f3b4d759e0dd03c43b98807dde34c252/thinc-6.10.2.tar.gz (1.2MB)
Collecting toolz==0.9.0 (from -r requirements.txt (line 24))
  Downloading https://files.pythonhosted.org/packages/14/d0/a73c15bbeda3d2e7b381a36afb0d9cd770a9f4adc5d1532691013ba881db/toolz-0.9.0.tar.gz (45kB)
Collecting tqdm==4.23.4 (from -r requirements.txt (line 25))
  Downloading https://files.pythonhosted.org/packages/93/24/6ab1df969db228aed36a648a8959d1027099ce45fad67532b9673d533318/tqdm-4.23.4-py2.py3-none-any.whl (42kB)
Collecting ujson==1.35 (from -r requirements.txt (line 26))
  Downloading https://files.pythonhosted.org/packages/16/c4/79f3409bc710559015464e5f49b9879430d8f87498ecdc335899732e5377/ujson-1.35.tar.gz (192kB)
Requirement already satisfied: Werkzeug==0.14.1 in /usr/local/lib/python3.6/site-packages (from -r requirements.txt (line 27)) (0.14.1)
Collecting wrapt==1.10.11 (from -r requirements.txt (line 28))
  Downloading https://files.pythonhosted.org/packages/a0/47/66897906448185fcb77fc3c2b1bc20ed0ecca81a0f2f88eda3fc5a34fc3d/wrapt-1.10.11.tar.gz
Building wheels for collected packages: cymem, cytoolz, dill, msgpack-python, murmurhash, pathlib, preshed, pyreadline, regex, spacy, termcolor, thinc, toolz, ujson, wrapt
  Running setup.py bdist_wheel for cymem: started
  Running setup.py bdist_wheel for cymem: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/55/8d/4a/f6328252aa2aaec0b1cb906fd96a1566d77f0f67701071ad13
  Running setup.py bdist_wheel for cytoolz: started
  Running setup.py bdist_wheel for cytoolz: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/f8/b1/86/c92e4d36b690208fff8471711b85eaa6bc6d19860a86199a09
  Running setup.py bdist_wheel for dill: started
  Running setup.py bdist_wheel for dill: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/e2/5d/17/f87cb7751896ac629b435a8696f83ee75b11029f5d6f6bda72
  Running setup.py bdist_wheel for msgpack-python: started
  Running setup.py bdist_wheel for msgpack-python: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/d5/de/86/7fa56fda12511be47ea0808f3502bc879df4e63ab168ec0406
  Running setup.py bdist_wheel for murmurhash: started
  Running setup.py bdist_wheel for murmurhash: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/b8/94/a4/f69f8664cdc1098603df44771b7fec5fd1b3d8364cdd83f512
  Running setup.py bdist_wheel for pathlib: started
  Running setup.py bdist_wheel for pathlib: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/f9/b2/4a/68efdfe5093638a9918bd1bb734af625526e849487200aa171
  Running setup.py bdist_wheel for preshed: started
  Running setup.py bdist_wheel for preshed: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/8f/85/06/2d132fb649a6bbcab22487e4147880a55b0dd0f4b18fdfd6b5
  Running setup.py bdist_wheel for pyreadline: started
  Running setup.py bdist_wheel for pyreadline: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/70/66/59/590265c96902c7616243300c8f0d8ffe7800253ad55dfa9aa1
  Running setup.py bdist_wheel for regex: started
  Running setup.py bdist_wheel for regex: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/75/07/38/3c16b529d50cb4e0cd3dbc7b75cece8a09c132692c74450b01
  Running setup.py bdist_wheel for spacy: started
  Running setup.py bdist_wheel for spacy: still running...
  Running setup.py bdist_wheel for spacy: still running...
  Running setup.py bdist_wheel for spacy: still running...
  Running setup.py bdist_wheel for spacy: still running...
  Running setup.py bdist_wheel for spacy: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/fb/00/28/75c85d5135e7d9a100639137d1847d41e914ed16c962d467e4
  Running setup.py bdist_wheel for termcolor: started
  Running setup.py bdist_wheel for termcolor: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/7c/06/54/bc84598ba1daf8f970247f550b175aaaee85f68b4b0c5ab2c6
  Running setup.py bdist_wheel for thinc: started
  Running setup.py bdist_wheel for thinc: still running...
  Running setup.py bdist_wheel for thinc: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/d8/5c/3e/9acf5d9974fb1c9e7b467563ea5429c9325f67306e93147961
  Running setup.py bdist_wheel for toolz: started
  Running setup.py bdist_wheel for toolz: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/f4/0c/f6/ce6b2d1aa459ee97cc3c0f82236302bd62d89c86c700219463
  Running setup.py bdist_wheel for ujson: started
  Running setup.py bdist_wheel for ujson: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/28/77/e4/0311145b9c2e2f01470e744855131f9e34d6919687550f87d1
  Running setup.py bdist_wheel for wrapt: started
  Running setup.py bdist_wheel for wrapt: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/48/5d/04/22361a593e70d23b1f7746d932802efe1f0e523376a74f321e
Successfully built cymem cytoolz dill msgpack-python murmurhash pathlib preshed pyreadline regex spacy termcolor thinc toolz ujson wrapt
Installing collected packages: cymem, toolz, cytoolz, dill, Flask, numpy, msgpack-python, msgpack-numpy, murmurhash, pathlib, pip, plac, preshed, pyreadline, regex, setuptools, six, wrapt, tqdm, termcolor, thinc, ujson, spacy
  Found existing installation: Flask 1.0.2
    Uninstalling Flask-1.0.2:
      Successfully uninstalled Flask-1.0.2
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
  Found existing installation: setuptools 39.2.0
    Uninstalling setuptools-39.2.0:
      Successfully uninstalled setuptools-39.2.0
Successfully installed Flask-0.12.4 cymem-1.31.2 cytoolz-0.8.2 dill-0.2.8.2 msgpack-numpy-0.4.1 msgpack-python-0.5.6 murmurhash-0.28.0 numpy-1.14.5 pathlib-1.0.1 pip-9.0.3 plac-0.9.6 preshed-1.0.0 pyreadline-2.1 regex-2017.4.5 setuptools-39.0.1 six-1.11.0 spacy-2.0.11 termcolor-1.1.0 thinc-6.10.2 toolz-0.9.0 tqdm-4.23.4 ujson-1.35 wrapt-1.10.11
You are using pip version 9.0.3, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 3deceaf0c24e
 ---> cdaa3e79b5d1
Step 8/8 : CMD ["python", "runserver.py"]
 ---> Running in 38bd3ddd0b2f
Removing intermediate container 38bd3ddd0b2f
 ---> 142755fee0a2
Successfully built 142755fee0a2
Successfully tagged mparser:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
PS C:\Users\STanwar\Documents\GitHub\mParser\SpaCYParser\SpaCYParser> docker run mparser:latest
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

提前致谢。沙拉德

附加背景:我有一些学校项目的 Python 经验。这是我第一个使用和部署 Docker 的实例。当我们决定为不同的域(美国和加拿大,由于数据的敏感性)部署我们的项目并构建可用于轻松生成实例(容器?)的 docker 映像时,对 Docker 的需求就出现了。

标签: pythonvisual-studiodockerflaskspacy

解决方案


推荐阅读