【问题标题】:Collecting numpy causes docker build to crash收集 numpy 导致 docker build 崩溃
【发布时间】:2020-11-19 14:26:44
【问题描述】:

我正在尝试使用 Selenium 在我的 GCP 虚拟机上部署一个小的 python 脚本,遵循this tutorial。不幸的是,我在构建容器映像时无法通过requirements.txt。确实如人们所读:

mikempc3@instance-1:~$ sudo docker build --tag my-python-app .
Sending build context to Docker daemon  387.1MB
Step 1/6 : FROM python:alpine3.7
alpine3.7: Pulling from library/python
48ecbb6b270e: Pull complete 
692f29ee68fa: Pull complete 
6439819450d1: Pull complete 
3c7be240f7bf: Pull complete 
ca4b349df8ed: Pull complete 
Digest: sha256:35f6f83ab08f98c727dbefd53738e3b3174a48b4571ccb1910bae480dcdba847
Status: Downloaded newer image for python:alpine3.7
 ---> 00be2573e9f7
Step 2/6 : COPY . /app
 ---> d8ae78db92f8
Step 3/6 : WORKDIR /app
 ---> Running in 3f6269c90e90
Removing intermediate container 3f6269c90e90
 ---> 717897752d1d
Step 4/6 : RUN pip install -r requirements.txt
 ---> Running in 061da28ee5cb
Collecting selenium (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
Collecting pandas (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/6f/29/32ff85413724ffa7cc8d52373f93c2ef1cb197ffd0c7b1b10d36452dd0ca/pandas-1.1.0.tar.gz (5.2MB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'error'
  Complete output from command /usr/local/bin/python /usr/local/lib/python3.7/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-ou2zb3ns/overlay --no-warn-script-l
ocation --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel Cython>=0.29.16,<3 "numpy==1.15.4; python_version=='3.6' and platform_system!='AIX'" "numpy==1.15.4; 
python_version=='3.7' and platform_system!='AIX'" "numpy==1.17.3; python_version>='3.8' and platform_system!='AIX'" "numpy==1.16.0; python_version=='3.6' and platform_system=='AIX'" "numpy==1.16.0
; python_version=='3.7' and platform_system=='AIX'" "numpy==1.17.3; python_version>='3.8' and platform_system=='AIX'":
  Ignoring numpy: markers 'python_version == "3.6" and platform_system != "AIX"' don't match your environment
  Ignoring numpy: markers 'python_version >= "3.8" and platform_system != "AIX"' don't match your environment
  Ignoring numpy: markers 'python_version == "3.6" and platform_system == "AIX"' don't match your environment
  Ignoring numpy: markers 'python_version == "3.7" and platform_system == "AIX"' don't match your environment
  Ignoring numpy: markers 'python_version >= "3.8" and platform_system == "AIX"' don't match your environment
  Collecting setuptools
    Downloading https://files.pythonhosted.org/packages/8e/11/9e10f1cad4518cb307b484c255cae61e97f05b82f6d536932b1714e01b47/setuptools-49.2.0-py3-none-any.whl (789kB)
  Collecting wheel
    Downloading https://files.pythonhosted.org/packages/8c/23/848298cccf8e40f5bbb59009b32848a4c38f4e7f3364297ab3c3e2e2cd14/wheel-0.34.2-py2.py3-none-any.whl
  Collecting Cython<3,>=0.29.16
    Downloading https://files.pythonhosted.org/packages/ad/4b/9e53bcce3c959fd0db143626e573210bba07be810fe8d7296373948c4183/Cython-0.29.21-py2.py3-none-any.whl (974kB)
  Collecting numpy==1.15.4
    Downloading https://files.pythonhosted.org/packages/2d/80/1809de155bad674b494248bcfca0e49eb4c5d8bee58f26fe7a0dd45029e2/numpy-1.15.4.zip (4.5MB)
  Building wheels for collected packages: numpy
    Building wheel for numpy (setup.py): started
    Building wheel for numpy (setup.py): finished with status 'error'
    Complete output from command /usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-800mjafl/numpy/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.rea
d().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-q_b64mdo --python-tag cp37:
    Running from numpy source directory.
    blas_opt_info:
    blas_mkl_info:
    customize UnixCCompiler
      libraries mkl_rt not found in ['/usr/local/lib', '/usr/lib']
      NOT AVAILABLE
  
    blis_info:
    customize UnixCCompiler
      libraries blis not found in ['/usr/local/lib', '/usr/lib']
      NOT AVAILABLE
  
    openblas_info:
    customize UnixCCompiler
    customize UnixCCompiler
      libraries openblas not found in ['/usr/local/lib', '/usr/lib']
      NOT AVAILABLE
  
    atlas_3_10_blas_threads_info:
    Setting PTATLAS=ATLAS
    customize UnixCCompiler
      libraries tatlas not found in ['/usr/local/lib', '/usr/lib']
      NOT AVAILABLE
  
    atlas_3_10_blas_info:
    customize UnixCCompiler
      libraries satlas not found in ['/usr/local/lib', '/usr/lib']
      NOT AVAILABLE
  
    atlas_blas_threads_info:
    Setting PTATLAS=ATLAS
    customize UnixCCompiler
      libraries ptf77blas,ptcblas,atlas not found in ['/usr/local/lib', '/usr/lib']
      NOT AVAILABLE
  
    atlas_blas_info:
    customize UnixCCompiler
      libraries f77blas,cblas,atlas not found in ['/usr/local/lib', '/usr/lib']
      NOT AVAILABLE
  
    accelerate_info:
      NOT AVAILABLE
  
    /tmp/pip-install-800mjafl/numpy/numpy/distutils/system_info.py:625: UserWarning:
        Atlas (http://math-atlas.sourceforge.net/) libraries not found.
        Directories to search for the libraries can be specified in the
        numpy/distutils/site.cfg file (section [atlas]) or by setting
        the ATLAS environment variable.
      self.calc_info()
    blas_info:
    customize UnixCCompiler
      libraries blas not found in ['/usr/local/lib', '/usr/lib']
      NOT AVAILABLE
    ...
        File "/usr/local/lib/python3.7/distutils/core.py", line 148, in setup
          dist.run_commands()
        File "/usr/local/lib/python3.7/distutils/dist.py", line 966, in run_commands
          self.run_command(cmd)
        File "/usr/local/lib/python3.7/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/tmp/pip-install-800mjafl/numpy/numpy/distutils/command/install.py", line 62, in run
          r = self.setuptools_run()
        File "/tmp/pip-install-800mjafl/numpy/numpy/distutils/command/install.py", line 36, in setuptools_run
          return distutils_install.run(self)
        File "/usr/local/lib/python3.7/distutils/command/install.py", line 545, in run
          self.run_command('build')
        File "/usr/local/lib/python3.7/distutils/cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "/usr/local/lib/python3.7/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/tmp/pip-install-800mjafl/numpy/numpy/distutils/command/build.py", line 47, in run
          old_build.run(self)
        File "/usr/local/lib/python3.7/distutils/command/build.py", line 135, in run
          self.run_command(cmd_name)
        File "/usr/local/lib/python3.7/distutils/cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "/usr/local/lib/python3.7/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/tmp/pip-install-800mjafl/numpy/numpy/distutils/command/build_src.py", line 148, in run
          self.build_sources()
        File "/tmp/pip-install-800mjafl/numpy/numpy/distutils/command/build_src.py", line 159, in build_sources
          self.build_library_sources(*libname_info)
        File "/tmp/pip-install-800mjafl/numpy/numpy/distutils/command/build_src.py", line 294, in build_library_sources
          sources = self.generate_sources(sources, (lib_name, build_info))
        File "/tmp/pip-install-800mjafl/numpy/numpy/distutils/command/build_src.py", line 377, in generate_sources
          source = func(extension, build_dir)
        File "numpy/core/setup.py", line 666, in get_mathlib_info
          raise RuntimeError("Broken toolchain: cannot link a simple C program")
      RuntimeError: Broken toolchain: cannot link a simple C program
  
      ----------------------------------------
  Command "/usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-800mjafl/numpy/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-sgbls4z_/install-record.txt --single-version-externally-managed --prefix /tmp/pip-build-env-ou2zb3ns/overlay -
-compile" failed with error code 1 in /tmp/pip-install-800mjafl/numpy/
  You are using pip version 19.0.1, however version 20.2 is available.
  You should consider upgrading via the 'pip install --upgrade pip' command.
  
  ----------------------------------------
Command "/usr/local/bin/python /usr/local/lib/python3.7/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-ou2zb3ns/overlay --no-warn-script-location --no-binary :n
one: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel Cython>=0.29.16,<3 "numpy==1.15.4; python_version=='3.6' and platform_system!='AIX'" "numpy==1.15.4; python_version=='3.7' 
and platform_system!='AIX'" "numpy==1.17.3; python_version>='3.8' and platform_system!='AIX'" "numpy==1.16.0; python_version=='3.6' and platform_system=='AIX'" "numpy==1.16.0; python_version=='3.7
' and platform_system=='AIX'" "numpy==1.17.3; python_version>='3.8' and platform_system=='AIX'"" failed with error code 1 in None
You are using pip version 19.0.1, however version 20.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

这是我的 requirements.txt 文件:

selenium
pandas
numpy
collections
json
time
requests

这是我要竞争的文件:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options



import pandas as pd
import numpy as np

from collections import defaultdict
import json

import time

import requests
from requests.exceptions import ConnectionError

# Define Browser Options
chrome_options = Options()
chrome_options.add_argument("--headless") # Hides the browser window

# Reference the local Chromedriver instance
chrome_path = r"C:\Programs\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)

df = pd.read_csv('path/to/file')    

tradable = []
print(len(df['Ticker']))
for ticker in df['Ticker']:
    print("ticker: ", ticker)
    location = "https://www.etoro.com/markets/" + ticker.lower()
    try:
        request = requests.get(location)
        driver.get(location)
        time.sleep(2)
        current_url = driver.current_url
        if current_url == location:
            tradable.append(ticker)
        else:
            print("no page but request= ", request)
    except ConnectionError:
        print('Ticker isn\'t tradable')
    else:
        tradable.append(ticker)

这是我的 Dockerfile:

FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD python ./find_tradable.py

这是我的操作系统名称和版本:

mikempc3@instance-1:~$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
VERSION_CODENAME=stretch
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

这是我的 Linux 内核版本:

mikempc3@instance-1:~$ uname -r
4.9.0-12-amd64

更新

我正在尝试 Serhii Rohoza 的答案。

我已经更新了我的 requirements.txt 文件,我没有遇到同样的问题,但现在尝试从 Dockerfile 构建映像的命令时出现错误:

mikempc3@instance-1:~$ sudo docker build --tag my-python-app:1 .
Sending build context to Docker daemon  387.1MB
Step 1/6 : FROM python:python3.7-slim
manifest for python:python3.7-slim not found: manifest unknown: manifest unknown

所以我尝试了:

mikempc3@instance-1:~$ sudo docker pull python:3.7-slim
3.7-slim: Pulling from library/python
6ec8c9369e08: Already exists 
401b5acb42e6: Already exists 
2e487de6656a: Pull complete 
519de614852e: Pull complete 
a3d1a61e090c: Pull complete 
Digest: sha256:47081c7bca01b314e26c64d777970d46b2ad7049601a6f702d424881af9f2738
Status: Downloaded newer image for python:3.7-slim
docker.io/library/python:3.7-slim
mikempc3@instance-1:~$ sudo docker build --tag my-python-app:1 .
Sending build context to Docker daemon  387.1MB
Step 1/6 : FROM python:python3.7-slim
manifest for python:python3.7-slim not found: manifest unknown: manifest unknown

【问题讨论】:

  • 不要使用基于 alpine 的 docker 镜像,因为 numpy 不提供与 alpine 兼容的轮子。使用基于 debian 的映像,例如 python:3.7-slim。
  • 您的发行版缺少Development Tools,无法编译python包的C程序部分。
  • @jakub 感谢您的评论。好的,我这样做是因为这是他们在the tutorial 中写的内容,我共享的链接正是基于这些内容。你推荐使用什么?
  • @JohnHanley 感谢您的评论。那好吧。但我在教程中没有找到。它包括什么?

标签: python-3.x docker google-cloud-platform requirements


【解决方案1】:

要解决您的问题,您应该使用基于 Debian 的映像:

为了构建 Docker 映像,我从 requirements.txt 中删除了 collections(即 deprecated)、json(内置)和 time(内置)并更改了 Dockerfile

$ cat Dockerfile 
FROM python:3.7-slim
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD python ./find_tradable.py

$ cat requirements.txt
selenium
pandas
numpy
requests

$ sudo docker build --tag my-python-app:1 .
Sending build context to Docker daemon   5.12kB
Step 1/6 : FROM python:3.7-slim
 ---> c042d3af0838
Step 2/6 : COPY . /app
 ---> ddb4662cd16f
Step 3/6 : WORKDIR /app
 ---> Running in 24e1a60ce53c
Removing intermediate container 24e1a60ce53c
 ---> 5db1f089513b
Step 4/6 : RUN pip install -r requirements.txt
 ---> Running in 5cdde4c11b10
Collecting selenium
  Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
Collecting pandas
  Downloading pandas-1.1.0-cp37-cp37m-manylinux1_x86_64.whl (10.5 MB)
Collecting numpy
  Downloading numpy-1.19.1-cp37-cp37m-manylinux2010_x86_64.whl (14.5 MB)
Collecting requests
  Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB)
Collecting urllib3
  Downloading urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
Collecting python-dateutil>=2.7.3
  Downloading python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
Collecting pytz>=2017.2
  Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
Collecting idna<3,>=2.5
  Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting chardet<4,>=3.0.2
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting certifi>=2017.4.17
  Downloading certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
Collecting six>=1.5
  Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: urllib3, selenium, six, python-dateutil, numpy, pytz, pandas, idna, chardet, certifi, requests
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 numpy-1.19.1 pandas-1.1.0 python-dateutil-2.8.1 pytz-2020.1 requests-2.24.0 selenium-3.141.0 six-1.15.0 urllib3-1.25.10
WARNING: You are using pip version 20.1.1; however, version 20.2 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 5cdde4c11b10
 ---> 94dd7203a37c
Step 5/6 : EXPOSE 5000
 ---> Running in 3f24bb8b62f5
Removing intermediate container 3f24bb8b62f5
 ---> 3bed65d447d6
Step 6/6 : CMD python ./find_tradable.py
 ---> Running in 35c0601f2c70
Removing intermediate container 35c0601f2c70
 ---> af17d0675fee
Successfully built af17d0675fee
Successfully tagged my-python-app:1

作为可能的workaround,您可以尝试从Alpine repository 提供的软件包中安装pandasnumpy

$ cat Dockerfile 
FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk add --update --no-cache libgfortran py3-pandas py3-numpy
ENV PYTHONPATH=/usr/lib/python3.7/site-packages
RUN pip install -r requirements.txt
EXPOSE 5000
CMD python ./find_tradable.py

$ cat requirements.txt
selenium
requests

$ sudo docker build --tag my-python-app:2 .
Sending build context to Docker daemon   5.12kB
Step 1/9 : FROM python:alpine3.7
 ---> 00be2573e9f7
Step 2/9 : COPY . /app
 ---> 2d9fe2cb3266
Step 3/9 : WORKDIR /app
 ---> Running in c106bf69643a
Removing intermediate container c106bf69643a
 ---> 807d1d546864
Step 4/9 : RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
 ---> Running in 77a65a19f776
Removing intermediate container 77a65a19f776
 ---> 940ce2de1711
Step 5/9 : RUN apk add --update --no-cache libgfortran py3-numpy py3-pandas
 ---> Running in 5084ac4055a2
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
fetch http://dl-8.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
(1/11) Installing libgcc (6.4.0-r5)
(2/11) Installing libquadmath (6.4.0-r5)
(3/11) Installing libgfortran (6.4.0-r5)
(4/11) Installing openblas (0.2.19-r3)
(5/11) Installing py3-numpy (1.19.1-r0)
(6/11) Installing python3 (3.6.9-r1)
(7/11) Installing py3-six (1.11.0-r0)
(8/11) Installing py3-dateutil (2.8.1-r0)
(9/11) Installing py3-tz (2017.3-r0)
(10/11) Installing libstdc++ (6.4.0-r5)
(11/11) Installing py3-pandas (1.0.5-r0)
Executing busybox-1.27.2-r11.trigger
OK: 172 MiB in 47 packages
Removing intermediate container 5084ac4055a2
 ---> 3af579bb35da
Step 6/9 : ENV PYTHONPATH=/usr/lib/python3.7/site-packages
 ---> Running in 89513c5c2f02
Removing intermediate container 89513c5c2f02
 ---> 3edc5aa5ef96
Step 7/9 : RUN pip install -r requirements.txt
 ---> Running in a694e49a4fbf
Collecting selenium (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
Collecting requests (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/45/1e/0c169c6a5381e241ba7404532c16a21d86ab872c9bed8bdcd4c423954103/requests-2.24.0-py2.py3-none-any.whl (61kB)
Collecting urllib3 (from selenium->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/9f/f0/a391d1463ebb1b233795cabfc0ef38d3db4442339de68f847026199e69d7/urllib3-1.25.10-py2.py3-none-any.whl (127kB)
Collecting chardet<4,>=3.0.2 (from requests->-r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting idna<3,>=2.5 (from requests->-r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58kB)
Collecting certifi>=2017.4.17 (from requests->-r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/5e/c4/6c4fe722df5343c33226f0b4e0bb042e4dc13483228b4718baf286f86d87/certifi-2020.6.20-py2.py3-none-any.whl (156kB)
Installing collected packages: urllib3, selenium, chardet, idna, certifi, requests
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 selenium-3.141.0 urllib3-1.25.10
You are using pip version 19.0.1, however version 20.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container a694e49a4fbf
 ---> c6cee0e65a83
Step 8/9 : EXPOSE 5000
 ---> Running in 0a8738c81f82
Removing intermediate container 0a8738c81f82
 ---> 66b223aa513c
Step 9/9 : CMD python ./find_tradable.py
 ---> Running in 3ca56154d699
Removing intermediate container 3ca56154d699
 ---> 3b5e507c7b4b
Successfully built 3b5e507c7b4b
Successfully tagged my-python-app:2

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 2020-08-10
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2017-05-28
    • 2018-01-23
    相关资源
    最近更新 更多