【问题标题】:Problem with downloading NLTK packages with Google Cloud Build and Dockerfile使用 Google Cloud Build 和 Dockerfile 下载 NLTK 包时出现问题
【发布时间】:2021-05-18 18:18:48
【问题描述】:

我正在使用带有 Cloud Build 的 Google Cloud Run,我希望从 Git 进行持续部署。我正在使用 Docker 文件通过 Google Cloud Buildpack 构建映像,因为我必须下载 Textblob Corpora (see topic)

我不使用 Textblob 的应用程序的所有其他部分都有效。当我使用 Docker 制作一个容器以在本地(在我的电脑上)进行测试并运行时,一切正常。

我的 Docker 文件是:

# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.8-slim

# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Build dependencies that you require for your pip installs
# Install production dependencies
# Remove packages
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc python3-dev python3-pip \
    && rm -rf /var/lib/apt/lists/* \
    && pip install --trusted-host pypi.python.org -r requirements.txt \
    && python -m textblob.download_corpora \
    && apt-get purge -y --auto-remove gcc python3-dev python3-pip 

# Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling.
CMD exec gunicorn --bind :$PORT --workers 2 --threads 8 --timeout 0 index:server

我尝试了以下方法但没有成功:

RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc pytho3n-dev python3-pip \
    && rm -rf /var/lib/apt/lists/* \
    && pip install --trusted-host pypi.python.org -r requirements.txt \
    && python -m textblob.download_corpora -d /usr/share/nltk_data \
    && apt-get purge -y --auto-remove gcc python-dev python3-pip

RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc python3-dev python3-pip \
    && rm -rf /var/lib/apt/lists/* \
    && pip install --trusted-host pypi.python.org -r requirements.txt \
    && python -m nltk.downloader -d /usr/share/nltk_data movie_reviews \
    && apt-get purge -y --auto-remove gcc python-dev python3-pip 

我该如何解决这个问题?

最好的,

维杰

编辑: 好像是Cloud Build下载到了根目录

[nltk_data] Downloading package movie_reviews to /root/nltk_data...
[nltk_data]   Unzipping corpora/movie_reviews.zip.

但 Cloud Run 中的应用正在主目录中寻找它:

Default
2021-05-19T10:06:02.040268Z Searched in:
Default
2021-05-19T10:06:02.040277Z - '/home/nltk_data'
Default
2021-05-19T10:06:02.040287Z - '/usr/local/nltk_data'
Default
2021-05-19T10:06:02.040297Z - '/usr/local/share/nltk_data'
Default
2021-05-19T10:06:02.040308Z - '/usr/local/lib/nltk_data'
Default
2021-05-19T10:06:02.040317Z - '/usr/share/nltk_data'
Default
2021-05-19T10:06:02.040326Z - '/usr/local/share/nltk_data'
Default
2021-05-19T10:06:02.040334Z - '/usr/lib/nltk_data'
Default
2021-05-19T10:06:02.040343Z - '/usr/local/lib/nltk_data'

使用python -m nltk.downloader -d /usr/share/nltk_data movie_reviews 也不起作用。

【问题讨论】:

    标签: python docker nltk google-cloud-run google-cloud-build


    【解决方案1】:

    您不需要apt-get install python。此图像中已经有一个 Python。因为您正在安装 Python,所以您需要安装两个 Python,这会发生冲突并混淆正在发生的事情。

    所以首先删除apt-get install python-dev python3-pip,看看是否能解决问题。

    【讨论】:

    • 感谢 Itamar,但删除它只会给出退出错误代码。然后它根本不安装。似乎问题在于目录存在差异。在构建它时下载到 root 并在应用程序中查找期间它看起来在家里。
    • 可能会有额外的错误,但你真的不想安装第二个 Python。
    • 我找到了我的解决方案,我必须添加 ENV NLTK_DATA /nltk_data/ ADD 。 $NLTK_DATA 到 Dockerfile,现在它正在工作。
    【解决方案2】:

    我尝试remove apt-get install python3-dev python3-pip,但收到此错误消息:

    ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-orra053u/pyahocorasick_d29a5a8c2da74f559d960bfd985b17f7/setup.py'"'"'; __file__='"'"'/tmp/pip-install-orra053u/pyahocorasick_d29a5a8c2da74f559d960bfd985b17f7/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-pourpyoq/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/pyahocorasick Check the logs for full command output.
    The command '/bin/sh -c apt-get update     && apt-get install -y --no-install-recommends gcc     && rm -rf /var/lib/apt/lists/*     && pip install --trusted-host pypi.python.org -r requirements.txt     && sudo python -m nltk.downloader movie_reviews     && apt-get purge -y --auto-remove gcc' returned a non-zero code: 1
    ERROR
    ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1
    

    【讨论】:

      猜你喜欢
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 2017-02-18
      • 2020-04-18
      • 1970-01-01
      • 2015-09-05
      • 2018-07-23
      相关资源
      最近更新 更多