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"]
Emma
  • 439
  • 5
  • 17
  • A. What's your host OS? B. From a host perspective, do you want this to be cross-platform (Windows/Linux)? – om-ha Apr 14 '21 at 18:43
  • It's Raspberry Pi OS. Just want a lightweight docker container with a python script, not cross-platform – Emma Apr 14 '21 at 19:05
  • 1
    Done with my changes, check the answer if it works for you. – om-ha Apr 14 '21 at 19:37
  • Thanks a lot, I am in bed now, but will give it a try as soon as I wake up – Emma Apr 14 '21 at 20:13

1 Answers1

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
om-ha
  • 3,102
  • 24
  • 37
  • 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? – yw173 Dec 08 '21 at 16:20
  • @WangYufan When building, specify the platform like this `docker build --platform linux/arm/v7` – om-ha Dec 08 '21 at 16:49
  • Also refer to [this issue](https://github.com/pi-hole/docker-pi-hole/issues/735#issuecomment-749709145) for some inspiration. – om-ha Dec 08 '21 at 16:51
  • You can specify the platform in the dockerfile, refer to `[--platform=]` in [`FROM` documentation](https://docs.docker.com/engine/reference/builder/#from) e.g. `FROM --platform=linux/arm/v7 python:3.7` – om-ha Dec 08 '21 at 16:55
  • Finally, this could prove useful too: [Multi-Platform Docker Builds](https://www.docker.com/blog/multi-platform-docker-builds/). – om-ha Dec 08 '21 at 16:59
  • Thank you, I will walk through and try the above approaches. – yw173 Dec 09 '21 at 07:53