简体   繁体   中英

Docker: cant install Tensorflow and numpy

How can I install Numpy and tensorflow inside a Docker image?

I'm trying to create a image of this simple Flask app:

import numpy as np
import tensorflow as tf

from flask import Flask, jsonify

print(tf.__version__)

with open('../assets/model/v1/model_architecture_V1.json', 'r') as f:
    model_json = f.read()

model = tf.keras.models.model_from_json(model_json)

model.load_weights("../assets/model/v1/model_weight_V1.h5")

app = Flask(__name__)

@app.route("/api/v1", methods=["GET"])
def getPrediction():

    prediction = model.predict()

    return jsonify({"Fe": 3123 })

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=4000, debug=True)

This is my DockerFile

FROM alpine:3.10

RUN apk add --no-cache python3-dev \
    && pip3 install --upgrade pip

WORKDIR /app

COPY . /app

RUN pip3 --no-cache-dir install -r requirements.txt

CMD ["python3","src/app.py"]

And this is my requirements.txt:

Flask==1.1.2
numpy==1.18.1
tensorflow==2.0.0

When I build the image, Docker throws an error that says tensorflow and numpy cant be found.

Error:

在此处输入图像描述

在此处输入图像描述

The issue here seems to be missing libraries to build the packages from .whl files. When creating Docker images for python which includes heavy libraries like tensorflow , I would suggest you to use the official Debian images.

Please see below Dockerfile using Debian-Buster :

FROM python:3.7.5-buster
RUN echo \
   && apt-get update \
   && apt-get --yes install apt-file \
   && apt-file update
RUN echo \
   && apt-get --yes install build-essential
ARG USER=nobody
RUN usermod -aG sudo $USER
RUN pip3 install --upgrade pip
COPY . /app
WORKDIR /app
RUN pip3 --no-cache-dir install -r requirements.txt
USER $USER
# Using 4000 here as you used 4000 in the code. Flask has a default of 5000
EXPOSE 4000
ENTRYPOINT ["python"]
CMD ["app/app.py"]

I used below commands to build and run the docker image, and got the result at http://0.0.0.0:4000/api/v1

docker build -t tfdocker:v1 . 
docker run -p 4000:4000 -t tfdocker:v1

For reference: This was my directory structure:

├── Dockerfile
├── app
│   └── app.py
└── requirements.txt

1 directory, 3 files

Content of requirements.txt were:

Flask==1.1.2
numpy==1.18.4
tensorflow==2.2.0

Hope this helps!

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