Installing python numpy module inside python alpine docker - python

I am trying to dockerize my python application. Errors are showing inside building Dockerfile and installing dependencies of scikit-learn ie. numpy.
Dockerfile
FROM python:alpine3.8
RUN apk update
RUN apk --no-cache add linux-headers gcc g++
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5001
ENTRYPOINT [ "python" ]
CMD [ "main.py" ]
requirements.txt
scikit-learn==0.23.2
pandas==1.1.3
Flask==1.1.2
ERROR: Could not find a version that satisfies the requirement setuptools (from versions: none)
ERROR: No matching distribution found for setuptools
Full Error

Agree with #senderle comment, Alpine is not the best choice here especially if you plan to use scientific Python packages that relies on numpy. If you absolutely need to use Alpine, you should have a look to other questions like Installing numpy on Docker Alpine.
Here is a suggestion, I've also replaced the ENTRYPOINT by CMD in order to be able to overwrite to ease debugging (for example to run a shell). If the ENTRYPOINT is python it will be not be possible to overwrite it and you will not be able to run anything other than python commands.
FROM python:3.8-slim
COPY . /app
WORKDIR /app
RUN pip install --quiet --no-cache-dir -r requirements.txt
EXPOSE 5001
CMD ["python", "main.py"]
Build, run, debug.
# build
$ docker build --rm -t my-app .
# run
docker run -it --rm my-app
# This is a test
# debug
$ docker run -it --rm my-app pip list
# Package Version
# --------------- -------
# click 7.1.2
# Flask 1.1.2
# itsdangerous 1.1.0
# Jinja2 2.11.2
# joblib 0.17.0
# MarkupSafe 1.1.1
# numpy 1.19.2
# pandas 1.1.3
# ...

Related

Pip error with requirements while deployng Django to Digital Ocean [duplicate]

I am baffled by a strange issue that I am facing with docker-compose. Pip install is failing for certain packages within requirements.txt file.
docker version
Client:
Version: 18.09.9
API version: 1.39
Go version: go1.13.4
Git commit: 1752eb3
Built: Sat Nov 16 01:05:26 2019
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.09.9
API version: 1.39 (minimum version 1.12)
Go version: go1.13.4
Git commit: 9552f2b
Built: Sat Nov 16 01:07:48 2019
OS/Arch: linux/amd64
Experimental: false
My docker-compose.yml file is:
version: "3.7"
services:
flask:
build: ./flask
container_name: flask
restart: always
environment:
- APP_NAME=MyFlaskApp
expose:
- 8080
nginx:
build: ./nginx
container_name: nginx
restart: always
ports:
- "80:80"
The contents of Dockerfile inside ./flask directory is:
# Use the Python3.7.5 image
FROM python:3.7.5
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install the dependencies
RUN pip3 install -r requirements.txt
# run the command to start uWSGI
CMD ["uwsgi", "app.ini"]
My requirements.txt file is (first few lines):
appdirs==1.4.3
apturl==0.5.2
asn1crypto==0.24.0
bcrypt==3.1.6
blinker==1.4
However, when I run docker-compose up command, it fails to install the second package in requirements.txt file.
Building flask
Step 1/6 : FROM python:3.7.5
---> fbf9f709ca9f
Step 2/6 : WORKDIR /app
---> Using cache
---> 39ab3ee34991
Step 3/6 : ADD . /app
---> Using cache
---> 8968809ff844
Step 4/6 : RUN python3 -m pip install --upgrade pip
---> Using cache
---> 15f717de5181
Step 5/6 : RUN pip3 install -r requirements.txt
---> Running in 7068f09498dc
Collecting appdirs==1.4.3
Downloading appdirs-1.4.3-py2.py3-none-any.whl (12 kB)
ERROR: Could not find a version that satisfies the requirement apturl==0.5.2 (from -r requirements.txt (line 2)) (from versions: none)
ERROR: No matching distribution found for apturl==0.5.2 (from -r requirements.txt (line 2))
ERROR: Service 'flask' failed to build: The command '/bin/sh -c pip3 install -r requirements.txt' returned a non-zero code: 1
I have tried a lot of possible options, but of no use. Some examples being:
Running pip upgrade inside the container.
Updating the DNS in docker as per the thread here.
as #WilliamD.Irons already pointed here apturl is a package, you can use :
RUN apt install apturl
RUN pip3 install -r requirements.txt
and remove apturl from your requirements.txt
apturl seems to be a client side Ubuntu program for adding support for links like click within a webpage:
The apturl is a graphical mini-program for installing packages from the repository that a user has. It is pre-installed on Ubuntu since version 7.10, and the Firefox and Pidgin programs come with support for it.
Additionally, there are other suggestions that support for this doesn't exisit outside Ubuntu. As the official python:3.7.5 image is based on Debian GNU/Linux this wouldn't be available anyway.
I'd question why this needs to be in the requirements for your Flask app, as you should just be able to write your own python function to generate compatible links within the app. Anyone hitting these links from a supported client (Any Ubuntu box with Firefox according to the above) should be able to successfully process those links.
If using a base container isn't an issue, try the Ubuntu environment and install python.
apturl is provided by ubuntu, specifically as it uses apt
# Use the ubuntu
FROM ubuntu:lts
# Set the working directory to /app
WORKDIR /app
# install python and pip, and delete cache
RUN apt update && apt install -y python3 python3-pip && rm -rf /var/lib/apt/lists/*
# Copy the current directory contents into the container at /app
ADD . /app
# Install the dependencies
RUN pip3 install -r requirements.txt
# run the command to start uWSGI
CMD ["uwsgi", "app.ini"]

How to install python libraries in docker file on ubuntu?

I want to create a docker image (docker version: 20.10.20)that contains python libraires from a requirement.txt file that contains 50 libraries. Without facing root user permissions how can proceed. Here is the file:
From ubuntu:latest
RUN apt update
RUN apt install python3 -y
WORKDIR /Destop/DS
# COPY requirement.txt ./
# RUN pip install -r requirement.txt
# it contains only pandas==1.5.1
COPY script2.py ./
CMD ["python3", "./script2.py"]
It failed at requiremnt.txt command
*error it takes lot of time while creating image.
because it ask for root permission.
For me the only problem in your Dockerfile is in the line RUN apt install python -y. This is erroring with Package 'python' has no installation candidate.
It is expected since python refers to version 2.x of Python wich is deprecated and no longer present in the default Ubuntu repositories.
Changing your Dockerfile to use Python version 3.x worked fine for me.
FROM ubuntu:latest
RUN apt update
RUN apt install python3 python3-pip -y
WORKDIR /Destop/DS
COPY requirement.txt ./
RUN pip3 install -r requirement.txt
COPY script2.py ./
CMD ["python3", "./script2.py"]
To test I used requirement.txt
pandas==1.5.1
and script2.py
import pandas as pd
print(pd.__version__)
With this building the docker image and running a container from it executed succesfully.
docker build -t myimage .
docker run --rm myimage

Installing private pip package inside docker container

I am trying to create docker container for a fastapi application.
This application is going to use a private pip package hosted on github.
During local development, I used the following command to install the dependency:
pip install git+https://<ACCESS_TOKEN>:x-oauth-basic#github.com/username/projectname
I tried the same approach inside dockerfile, however without success
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
ARG ACCESS_TOKEN=default_value
RUN /usr/local/bin/python -m pip install --upgrade pip
RUN echo "pip install git+https://${ACCESS_TOKEN}:x-oauth-basic#github.com/username/projectname"
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY . /code
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
docker build --build-arg ACCESS_TOKEN=access_token_value .
The container builds without errors and during the build process I can see that the token is passed correctly.
However, after running the container with docker run <containerid> I get the following error:
ModuleNotFoundError: No module named 'projectname'
Have anyone tried such thing before?
Is it the correct approach?
if I am not mistaken, you could run your pip command without echo:
RUN pip install git+https://${ACCESS_TOKEN}:x-oauth-basic#github.com/username/projectname

How to correctly install NumPy in Docker container running on a Raspberry Pi?

I am trying to host a simple Dash app on a Raspberry Pi in a Docker environment. The app runs as expected locally, but when building for ARMv6 and pulling the image, I get the following error (shortened for brevity):
ImportError: Unable to import required dependencies:
numpy:
[...]
Original error was: No module named 'numpy.core._multiarray_umath'
I build the app for ARMv6 and push it to my DockerHub repository using the following command:
docker buildx build --platform linux/arm/v6 -t <username>/<repo-name>:<tag> --push .
The image is then downloaded using docker-compose pull into the Docker environment on the Raspbery Pi and started using docker-compose up
My Dockerfile contains the following:
FROM python:3.9
WORKDIR /app
COPY requirements.txt requirements.txt
# Problems with brotli from piwheels means it needs to be downloaded from another repo.
RUN pip3 install --extra-index-url https://www.piwheels.org/simple --no-cache-dir -r requirements.txt \
&& pip3 uninstall -y brotli \
&& pip3 install --no-cache-dir brotli
COPY . .
CMD [ "python", "app.py" ]
Requirements.txt:
numpy==1.21.4
pandas==1.3.5
dash==2.0.0
dash-core-components==2.0.0
dash-html-components==2.0.0
dash-bootstrap-components==1.0.2
dash-table==5.0.0
plotly==5.3.1
mysql-connector-python==8.0.27
dash-bootstrap-templates==1.0.4

docker image build fails at RUN pip install -r requirements.txt in a Flask app

I have a Flask application with the following requirements.txt
chalice
matplotlib
sklearn
numpy
scipy
pandas
flask
flask_restful
and the following Dockerfile:
FROM python:3.6.1-alpine
WORKDIR /project
ADD . /project
RUN pip install -r requirements.txt
CMD ["python","app.py"]
Running the command docker image build -t clf_test .
generates the following error:
You are using pip version 9.0.1, however version 20.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1
It seems that the matplotlib can't get installed for some reason.
Running the pip install -r requirements.txt locally doesn't produce any errors
matplotlib must be built from source, and compiling it requires a number of supporting libraries as well as a functioning C compiler. You can figure out what these are and install them so that it builds properly...
...or you can just base your Dockerfile on the non-alpine python:3.6.1 image and then apt-get install python3-matplotlib before installing your other requirements. E.g., this builds without errors:
FROM python:3.6.1
WORKDIR /project
ADD . /project
RUN apt update; apt-get -y install python3-matplotlib
RUN pip install -r requirements.txt
CMD ["python","app.py"]

Categories

Resources