2

I want to run a Docker container on my Raspberry PI 2 with a Python script that uses numpy. For this I have the following Dockerfile:

FROM python:3.7
COPY numpy_script.py /
RUN pip install numpy

CMD ["python", "numpy_script.py"]

But when I want to import numpy, I get the error message that libf77blas.so.3 was not found. I have also tried to install numpy with a wheel from www.piwheels.org, but the same error occurs.

The Google search revealed that I need to install the liblapack3. How do I need to modify my Dockerfile for this?


Inspired by the answer of om-ha, this worked for me:

FROM python:3.7
COPY numpy_script.py /
RUN apt-get update \
    && apt-get -y install libatlas-base-dev \
    && pip install numpy

CMD ["python", "numpy_script.py"]
4
  • A. What's your host OS? B. From a host perspective, do you want this to be cross-platform (Windows/Linux)? Commented Apr 14, 2021 at 18:43
  • It's Raspberry Pi OS. Just want a lightweight docker container with a python script, not cross-platform Commented Apr 14, 2021 at 19:05
  • 1
    Done with my changes, check the answer if it works for you. Commented Apr 14, 2021 at 19:37
  • Thanks a lot, I am in bed now, but will give it a try as soon as I wake up Commented Apr 14, 2021 at 20:13

1 Answer 1

2

Working Dockerfile

# Python image (debian-based)
FROM python:3.7

# Create working directory
WORKDIR /app

# Copy project files
COPY numpy_script.py numpy_script.py

# RUN command to update packages & install dependencies for the project
RUN apt-get update \
    && apt-get install -y \
    && pip install numpy

# Commands to run within the container
CMD ["python", "numpy_script.py"]

Explanation

  1. You have an extra trailing \ in your dockerfile, this is used for multi-line shell commands actually. You can see this used in-action here. I used this in my answer above. Beware the last shell command (in this case pip) does not need a trailing \, a mishap that was done in the code you showed.
  2. You should probably use a working directory via WORKDIR /app
  3. Run apt-get update just to be sure everything is up-to-date.
  4. It's recommended to group multiple shell commands within ONE RUN directive using &&. See best practices and this discussion.

Resources sorted by order of use in this dockerfile

  1. FROM
  2. WORKDIR
  3. COPY
  4. RUN
  5. CMD
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, om-ha, thank you for given the working Dockerfile. I tried to build with above working Dockerfile in windows 10, which is working. However when I copied over image, and use docker to run the image on the Raspberry Pi 4, it says "The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm/v7) and no specific platform was requested." I tried add build parameter --platform linux/armv7, which takes hour to build on windows 10. Is this behavior expected?
@WangYufan When building, specify the platform like this docker build --platform linux/arm/v7
Also refer to this issue for some inspiration.
You can specify the platform in the dockerfile, refer to [--platform=<platform>] in FROM documentation e.g. FROM --platform=linux/arm/v7 python:3.7
Finally, this could prove useful too: Multi-Platform Docker Builds.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.