使用 flask 和 flask-restful 容器化微服务构建时 Docker 构建错误

标签 docker python-3.7 alpine

无法将 flask 微服务容器化。我已经通过 GitHub 页面描述了解决错误的方法,但似乎没有任何效果。我尝试了不同的 python3.X-alpine 运行时,但仍然是同一件事。我真的不明白为什么日志告诉我错误:命令“gcc”失败,退出状态为 1 当我添加了所需的 GCC 依赖项时。

Dockerfile

FROM python:3.7.4-alpine
RUN apk add --no-cache --virtual .build-deps gcc musl-dev \
 && pip install cython \
 && apk del .build-deps
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt --default-timeout=100 future
EXPOSE 5000
CMD ["python3", "app.py"]

错误
   command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ohfwxr4o/typed-ast/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ohfwxr4o/typed-ast/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-6vb5bz2_ --python-tag cp37
       cwd: /tmp/pip-install-ohfwxr4o/typed-ast/
  Complete output (23 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.7
  creating build/lib.linux-x86_64-3.7/typed_ast
  copying typed_ast/ast3.py -> build/lib.linux-x86_64-3.7/typed_ast
  copying typed_ast/conversions.py -> build/lib.linux-x86_64-3.7/typed_ast
  copying typed_ast/ast27.py -> build/lib.linux-x86_64-3.7/typed_ast
  copying typed_ast/__init__.py -> build/lib.linux-x86_64-3.7/typed_ast
  package init file 'ast3/tests/__init__.py' not found (or not a regular file)
  creating build/lib.linux-x86_64-3.7/typed_ast/tests
  copying ast3/tests/test_basics.py -> build/lib.linux-x86_64-3.7/typed_ast/tests
  running build_ext
  building '_ast27' extension
  creating build/temp.linux-x86_64-3.7
  creating build/temp.linux-x86_64-3.7/ast27
  creating build/temp.linux-x86_64-3.7/ast27/Parser
  creating build/temp.linux-x86_64-3.7/ast27/Python
  creating build/temp.linux-x86_64-3.7/ast27/Custom
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -Iast27/Include -I/usr/local/include/python3.7m -c ast27/Parser/acceler.c -o build/temp.linux-x86_64-3.7/ast27/Parser/acceler.o
  unable to execute 'gcc': No such file or directory
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for typed-ast
  Running setup.py clean for typed-ast
  Building wheel for wrapt (setup.py): started
  Building wheel for wrapt (setup.py): finished with status 'done'
  Created wheel for wrapt: filename=wrapt-1.11.2-cp37-none-any.whl size=19592 sha256=82243e530eafb10da7c6b0645a61b8808dcb9136c0e5787bc6c161172f7ca90d
  Stored in directory: /root/.cache/pip/wheels/d7/de/2e/efa132238792efb6459a96e85916ef8597fcb3d2ae51590dfd
Successfully built future MarkupSafe wrapt
Failed to build typed-ast
Installing collected packages: future, aniso8601, wrapt, lazy-object-proxy, six, typed-ast, astroid, Click, MarkupSafe, Jinja2, itsdangerous, Werkzeug, Flask, pytz, Flask-RESTful, isort, mccabe, pylint
  Running setup.py install for typed-ast: started
    Running setup.py install for typed-ast: finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ohfwxr4o/typed-ast/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ohfwxr4o/typed-ast/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-glxx7aqy/install-record.txt --single-version-externally-managed --compile
         cwd: /tmp/pip-install-ohfwxr4o/typed-ast/
    Complete output (23 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.7
    creating build/lib.linux-x86_64-3.7/typed_ast
    copying typed_ast/ast3.py -> build/lib.linux-x86_64-3.7/typed_ast
    copying typed_ast/conversions.py -> build/lib.linux-x86_64-3.7/typed_ast
    copying typed_ast/ast27.py -> build/lib.linux-x86_64-3.7/typed_ast
    copying typed_ast/__init__.py -> build/lib.linux-x86_64-3.7/typed_ast
    package init file 'ast3/tests/__init__.py' not found (or not a regular file)
    creating build/lib.linux-x86_64-3.7/typed_ast/tests
    copying ast3/tests/test_basics.py -> build/lib.linux-x86_64-3.7/typed_ast/tests
    running build_ext
    building '_ast27' extension
    creating build/temp.linux-x86_64-3.7
    creating build/temp.linux-x86_64-3.7/ast27
    creating build/temp.linux-x86_64-3.7/ast27/Parser
    creating build/temp.linux-x86_64-3.7/ast27/Python
    creating build/temp.linux-x86_64-3.7/ast27/Custom
    gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -Iast27/Include -I/usr/local/include/python3.7m -c ast27/Parser/acceler.c -o build/temp.linux-x86_64-3.7/ast27/Parser/acceler.o
    unable to execute 'gcc': No such file or directory
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ohfwxr4o/typed-ast/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ohfwxr4o/typed-ast/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-glxx7aqy/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.
The command '/bin/sh -c pip install -r requirements.txt --default-timeout=100 future' returned a non-zero code: 1```

最佳答案

在您的 Dockerfile 开始时,您卸载 gcc。当您稍后安装应用程序的依赖项时,如果其中一个具有 native 扩展,您将收到您看到的错误。

RUN ... apk del .build-deps
# Now gcc is gone
RUN pip install -r requirements.txt

要完成这项工作,您需要移动 pip install进入 apk add 之间的 block 和 apk del .为了使这项工作,requirements.txt文件需要已经在图像中。但是,您不需要整个应用程序源。如果您 COPY仅在 requirements.txt文件然后重复 docker build如果该文件已更改,将重新安装软件包,但否则 Docker 的层缓存将重用已经存在的内容。

这应该使您:

FROM python:3.7.4-alpine
WORKDIR /app # Docker creates this for you
COPY requirements.txt .
RUN apk add --no-cache --virtual .build-deps gcc musl-dev \
 && pip install cython \
 && pip install -r requirements.txt --default-timeout=100 future \
 && apk del .build-deps
COPY . .
EXPOSE 5000
CMD ["python3", "app.py"]

关于使用 flask 和 flask-restful 容器化微服务构建时 Docker 构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357173/

相关文章:

node.js - Docker 找不到模块/bin/bash

Docker 注册表垃圾收集

angular - API key 是否会通过 DockerHub 上的私有(private) Docker 容器镜像公开?

docker - Docker 镜像中不包含 wwwroot 中的静态文件

python - 将小部件导出为 PDF 文件

python - AttributeError : type object 'Callable' has no attribute '_abc_registry'

algorithm - Yen 的 K 最短路径给出错误结果 (Python)

docker - 如何在 alpine :latest image using rc-service command 中启动 Nginx 服务器

nginx - Docker 容器 : how do they work together?

azure - 部署到 Service Fabric 时,如何在 docker-compose 文件中设置有效的 ServiceDnsName?