【问题标题】:Problem building Docker image for python app using a forked Flask github repository使用分叉的 Flask github 存储库为 python 应用程序构建 Docker 映像的问题
【发布时间】:2021-07-01 06:13:47
【问题描述】:

我一直在尝试让 Flask 应用程序的这个 Ubuntu docker 映像正确构建一段时间,但它从未成功构建。我正在尝试创建一个 Python Flask 应用程序,该应用程序运行于 Flask github 存储库的分叉版本。对这个 github 存储库所做的唯一更改是在 setup.py 中,其中的依赖项被替换为其他分叉的依赖项。当我运行“docker build -t test”时。我收到以下错误:

Obtaining flask from git+git://github.com/ectoglasses/flask.git#egg=flask (from -r requirements.txt (line 6))
  Cloning git://github.com/ectoglasses/flask.git to ./src/flask
  Running command git clone -q git://github.com/ectoglasses/flask.git /app/src/flask
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/app/src/flask/setup.py'"'"'; __file__='"'"'/app/src/flask/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info
         cwd: /app/src/flask/
    Complete output (33 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/app/src/flask/setup.py", line 4, in <module>
        setup(
      File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 144, in setup
        return distutils.core.setup(**attrs)
      File "/usr/lib/python3.8/distutils/core.py", line 121, in setup
        dist.parse_config_files()
      File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 701, in parse_config_files
        parse_configuration(self, self.command_options,
      File "/usr/lib/python3/dist-packages/setuptools/config.py", line 121, in parse_configuration
        meta.parse()
      File "/usr/lib/python3/dist-packages/setuptools/config.py", line 426, in parse
        section_parser_method(section_options)
      File "/usr/lib/python3/dist-packages/setuptools/config.py", line 399, in parse_section
        self[name] = value
      File "/usr/lib/python3/dist-packages/setuptools/config.py", line 184, in __setitem__
        value = parser(value)
      File "/usr/lib/python3/dist-packages/setuptools/config.py", line 515, in _parse_version
        version = self._parse_attr(value, self.package_dir)
      File "/usr/lib/python3/dist-packages/setuptools/config.py", line 349, in _parse_attr
        module = import_module(module_name)
      File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
      File "<frozen importlib._bootstrap>", line 991, in _find_and_load
      File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 848, in exec_module
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "/app/src/flask/src/flask/__init__.py", line 1, in <module>
        from markupsafe import escape
    ModuleNotFoundError: No module named 'markupsafe'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
The command '/bin/sh -c apt-get install -y python3-pip && apt-get install -y git && pip3 install -r requirements.txt' returned a non-zero code: 1

我之前在这个项目中使用过 Alpine,但没有遇到同样的错误。我还尝试使用托管在 Pypi 上的 Flask 版本来查看问题是否出在包本身,但没有出现任何错误。这告诉我,考虑到产生这些结果的代码,这与 pip 在 Ubuntu 上的工作方式有关,我在下面发布了这些代码。

Dockerfile:

# Use Google Cloud SDK's container as the base image
FROM ubuntu:20.04

...

# Copy the contents of the current directory into the container directory /app
COPY . /app

# Set the working directory of the container to /app
WORKDIR /app

# Install the Python packages specified by requirements.txt into the container
RUN apt-get update -y
RUN apt-get install -y python3-pip && apt-get install -y git && pip3 install -r requirements.txt

CMD python3 app.py

requirements.txt:

# Python framework
-e git://github.com/ectoglasses/flask.git#egg=flask

是否有人对可能导致此错误的原因有任何想法?

【问题讨论】:

    标签: python docker ubuntu flask


    【解决方案1】:

    从错误来看,模块markupsafe 似乎丢失了。 Dockerfile 也可以使用一些缓存改进。

    例如

    # Use Google Cloud SDK's container as the base image
    FROM ubuntu:20.04
    
    ...
    
    COPY requirements.txt /app/
    
    WORKDIR /app
    
    RUN apt-get update -y \
        && apt-get install -y \ 
                python3-pip \
                python-markupsafe \
                git
    
    # Install the Python packages specified by requirements.txt into the container
    RUN pip3 install -r requirements.txt
    
    # Copy the contents of the current directory into the container directory /app
    COPY . /app
    
    CMD python3 app.py
    

    【讨论】:

      【解决方案2】:

      我找到了问题所在。问题出在setup.cfg 文件第 3 行version = attr: flask.__version__。在pip3 install -r requirements.txt 的 docker build 阶段,它会在安装任何依赖项之前加载setup.cfg 文件。当它加载setup.cfg 文件时,它会加载flask 模块(__init__.py 文件)来查找__version__ 属性。它会抛出错误,因为尚未安装 __init__.py 中列出的依赖项。

      这似乎是 Ubuntu 环境中的一个错误。我在 macOS 中运行没有问题。您可以通过更改 setup.cfg 文件中的 version = 1.0 来快速修复。

      【讨论】:

        猜你喜欢
        • 2017-06-04
        • 2018-01-05
        • 1970-01-01
        • 2021-03-26
        • 2016-06-24
        • 2015-01-01
        • 2018-03-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多