简体   繁体   中英

Install Numpy Requirement in a Dockerfile. Results in error

I am attempting to install a numpy dependancy inside a docker container. (My code heavily uses it). On building the container the numpy library simply does not install and the build fails. This is on OS raspbian-buster\/stretch. This does however work when building the container on MAC OS.

FROM python:3.6
ENV PYTHONUNBUFFERED 1
ENV APP /app
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir $APP
WORKDIR $APP
ADD requirements.txt .
RUN pip install -r requirements.txt
COPY . .

To use Numpy on python3 here, we first head over to the official documentation to find what dependencies are required to build Numpy.

Mainly these 5 packages + their dependencies must be installed:

  1. Python3 - 70 mb
  2. Python3-dev - 25 mb
  3. gfortran - 20 mb
  4. gcc - 70 mb
  5. musl-dev -10 mb (used for tracking unexpected behaviour/debugging)

An POC setup would look something like this -

Dockerfile:

FROM gliderlabs/alpine
ADD repositories.txt /etc/apk/repositories

RUN apk add --no-cache --update \
    python3 python3-dev gcc \
    gfortran musl-dev

ADD requirements-pip.txt .
RUN pip3 install --upgrade pip setuptools && \
    pip3 install -r requirements-pip.txt

ADD . /app
WORKDIR /app
ENV PYTHONPATH=/app/
ENTRYPOINT python3 testscript.py

repositories.txt

http://dl-5.alpinelinux.org/alpine/v3.4/main

requirements-pip.txt

numpy

testscript.py

import numpy as np

def random_array(a, b):
    return np.random.random((a, b))

a = random_array(2,2)
b = random_array(2,2)
print(np.dot(a,b))

To run this - clone alpine , build it using "docker build -t gliderlabs/alpine ."

Build and Run your Dockerfile

docker build -t minidocker .
docker run minidocker

Output should be something like this-

[[ 0.03573961 0.45351115]
[ 0.28302967 0.62914049]]

Here's the git link , if you want to test it out

From the error logs, it does not seems that it from numpy. but you can install numpy before the requirment.txt and verify if it's working.

FROM python:3.6
RUN pip install numpy==1.14.3

Build

docker build -t numpy .

Run and Test

docker run numpy bash -c "echo import numpy as np > test.py ; python test.py"

So you will see no error on import.

or You can try numpy as an alpine package

FROM python:3-alpine3.9
RUN apk add --no-cache py3-numpy

Or better to post the requirement.txt.

I had lot of trouble with this issue using FROM python:3.9-buster and pandas.

My requirements.txt had the python-dev-tools, numpy and pandas, along with other packages.

I always got something like this when attempting to build :

在此处输入图片说明

preluded by:

在此处输入图片说明

and by:

在此处输入图片说明

Following hints by Adiii in this thread, I did some debug and found out that this actually works and builds a perfectly running container:

RUN pip3 install NumPy==1.18.0

RUN pip3 install python-dev-tools

RUN pip3 install pandas

RUN pip3 install -r requirements.txt

So, giving a specific RUN layer to the pip3 installing pandas solved the problem!

FROM python:slim
CMD pip install numpy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM