0

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:

enter image description here

enter image description here

Braven
  • 484
  • 1
  • 6
  • 27

1 Answers1

3

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!

Jinto Lonappan
  • 312
  • 2
  • 8
  • Thanks, I end up fallowing the official doc: https://github.com/docker-library/docs/tree/master/python#how-to-use-this-image But your code seems interesting. Why you suggest to use Debian-buster instead of Python only? – Braven May 29 '20 at 19:25
  • I have a problem though if you can help me. This is mi dockerFile now: *************** FROM python:3 WORKDIR /app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["python3","src/app.py"] ************* The issue is now that assets isnt copied. The app throws me an error that says model is not founded – Braven May 29 '20 at 19:31
  • 1
    It looks like your `models` path is outside (you are referencing `..`) of your `Docker` context which is the **current** directory. If you want to copy the model directory, you would need to copy it to your working directory (Directory you use to build the image from - Typically the current directory indicates with `.`/ Where you have your Dockerfile). – Jinto Lonappan May 29 '20 at 22:03