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"]
Related
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
My Dockerfile:
FROM python:3.9
WORKDIR /usr/src/app
RUN pip install python-dotenv
RUN pip install other_libraries...
During building container, message is displayed:
...
Step 3/4 : RUN pip install python-dotenv
---> Running in 5fffd3fe4042
Collecting python-dotenv
Downloading python_dotenv-0.15.0-py2.py3-none-any.whl (18 kB)
Installing collected packages: python-dotenv
Successfully installed python-dotenv-0.15.0
Removing intermediate container 5fffd3fe4042
---> 2cd0942f520c
...
But when I run docker-compose exec container_name pip list there is no on list python-dotenv library.
I tried on python:3.9, python:3.8 with python-dotenv in 0.14 or 0.15 version.
Of course, when I run docker-compose exec container_name pip install python-dotenv everything is okey.
Why RUN command in Dockerfile not installed correctly?
Step by step.
Build image that including python libraries (using your Dockerfile)
docker build -t dotenv_image:1.0 .
List images
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dotenv_image 1.0 507f2d0505a0 4 minutes ago 891MB
↑Okay it is here
Run container with sh shell to be able run commands and check what inside container
docker run --rm -it --entrypoint sh dotenv_image:1.0
Check libraries
pip freeze
python-dotenv==0.15.0
Library is here
Try to use library
python -c 'from dotenv import load_dotenv; print("ALL OK" if load_dotenv() else "CAN NOT LOAD")';
ALL OK
↑Positive output
I don't use docker-compose, but since your title says that your Dockerfile isn't working right, I can tell you that that's apparently not the problem. Your Dockerfile seems to work just fine:
>>> cat Dockerfile
FROM python:3.9
WORKDIR /usr/src/app
RUN pip install python-dotenv
>>> docker build -t so2 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM python:3.9
3.9: Pulling from library/python
e4c3d3e4f7b0: Pull complete
101c41d0463b: Pull complete
8275efcd805f: Pull complete
751620502a7a: Pull complete
0a5e725150a2: Pull complete
397dba5694db: Pull complete
b1d09d0eabcb: Pull complete
475299e7c7f3: Pull complete
d2fe14d8e6bc: Pull complete
Digest: sha256:429b2fd1f6657e4176d81815dc9e66477d74f8cbf986883c024c9b97f7d4d5a6
Status: Downloaded newer image for python:3.9
---> 5336a27a9b1f
Step 2/3 : WORKDIR /usr/src/app
---> Running in 37b03142a9b6
Removing intermediate container 37b03142a9b6
---> 4677ab34ce84
Step 3/3 : RUN pip install python-dotenv
---> Running in e89d17be1a32
Collecting python-dotenv
Downloading python_dotenv-0.15.0-py2.py3-none-any.whl (18 kB)
Installing collected packages: python-dotenv
Successfully installed python-dotenv-0.15.0
Removing intermediate container e89d17be1a32
---> 55d00eeae4b4
Successfully built 55d00eeae4b4
Successfully tagged so2:latest
>>> docker run -it so2 bash
root#d211989c4bd7:/usr/src/app# pip list
Package Version
------------- -------
pip 20.2.4
python-dotenv 0.15.0
setuptools 50.3.2
wheel 0.35.1
root#d211989c4bd7:/usr/src/app# exit
>>> docker run -it so2
Python 3.9.0 (default, Oct 13 2020, 20:14:06)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from dotenv import load_dotenv; load_dotenv()
True
>>>
>>> represent my prompt in my MacBook pro Terminal window.
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
# ...
This question already has answers here:
Activate python virtualenv in Dockerfile
(6 answers)
Closed 2 years ago.
I am trying to setup a python virtual environment on a docker image running a docker build
The terminal output is ok when I run docker build .. but when I login into my container, no packages are installer in my virtual environment.
#Dockerfile
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
RUN python3.8 get-pip.py
RUN pip install virtualenv
RUN virtualenv venv
RUN /home/ubuntu/venv/bin/pip install -r auto/requirements.txt
...
# Docker build command
docker build --no-cache -t auto-server:1.0 .
# Terminal output
Step 27/27 : RUN /home/ubuntu/venv/bin/pip install -r auto/requirements.txt
---> Running in d27dbb9a4c97
Collecting asgiref==3.2.10
Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting beautifulsoup4==4.9.1
Downloading beautifulsoup4-4.9.1-py3-none-any.whl (115 kB)
Collecting Django==3.1.1
Downloading Django-3.1.1-py3-none-any.whl (7.8 MB)
...
Successfully installed Django-3.1.1 asgiref-3.2.10 beautifulsoup4-4.9.1 fake-useragent-0.1.11 joblib-0.16.0 numpy-1.19.2 pandas-1.1.2 python-dateutil-2.8.1 pytz-2020.1 scikit-learn-0.23.2 scipy-1.5.2 six-1.15.0 sklearn-0.0 soupsieve-2.0.1 sqlparse-0.3.1 threadpoolctl-2.1.0
Here is what I get when I list pakages in my virtual environment:
$ docker exec -ti auto-server bash
root#9c1f914d1b7b:/home/ubuntu# source venv/bin/activate
(venv) root#9c1f914d1b7b:/home/ubuntu# pip list
Package Version
---------- -------
pip 20.2.2
setuptools 49.6.0
wheel 0.35.1
WARNING: You are using pip version 20.2.2; however, version 20.2.3 is available.
You should consider upgrading via the '/home/ubuntu/venv/bin/python -m pip install --upgrade pip' command.
Is it the right way to do it? How to make sure packages will be installed?
Finally having a virtual environment is not mandatory in a container development environment, I just setup image python environment as desired.
# Dockerfile
...
RUN python3.8 get-pip.py
RUN pip install -r auto/requirements.txt
This is not exactly what I was looking for but it does the job.
I use simple Dockerfile for dev mode
# pull official base image
FROM python:3.6-alpine
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
I am trying to create an docker image with ubutu 16.04 as base. I want to install few python packages like pandas, flask etc. I have kept all packages in "requirements.txt". But when I am trying to build image, I am getting
Could not find a version that satisfies the requirement requests (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for requests (from -r requirements.txt (line 1))
Basically, I have not mentioned any version in "requirements.txt". I guess it should take the latest available and compatible version of that package. But for every package same issue I am getting.
My DockerFile is as follows.
FROM ubuntu:16.04
RUN apt-get update -y && \
apt-get install -y python3-pip python3-dev build-essential cmake pkg-config libx11-dev libatlas-base-dev
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /testing/requirements.txt
WORKDIR /testing
RUN pip3 install -r requirements.txt
and requirements.txt is.
pandas
requests
PyMySQL
Flask
Flask-Cors
Pillow
face-recognition
Flask-SocketIO
Where I am doing wrong ? Can anybody help ?
I too ran into the same situation. I observed that, python packages is looking for the network within docker. It is thinking that, it is running in a standalone without network so its not able to locate the package. In these type of situations either
No matching distribution found
or sometimes
Retrying ...
error may occur.
I used a --network option in the docker build command like below to overcome this error where the command insists python to use the host network to download the required packages.
docker build --network=host -t tracker:latest .
Try using this:
RUN python3.6 -m pip install --upgrade pip \
&& python3.6 -m pip install -r requirements.txt
by using it in this way, you are specifying the version of python in which you want to search for those packages.
Change it to python3.7 if you wish to use 3.7 version.
I suggest using the official python image instead. As a result, your Dockerfile will now become:
FROM python:3
WORKDIR /testing
COPY ./requirements.txt /testing/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
... etc ...
Now re: Angular/Node. You have two options from here: 1) Install Angular/Node on the Python image; or 2) Use Docker's multi-stage build feature so you build the Angular and Python-specific images before merging them together. Option 2 is recommended but it would take some work. It would probably look like this:
FROM node:8 as node
# Angular-specific build
FROM python:3 as python
# Python-specific build
# Then copy your data from the Angular image to the Python one:
COPY --from=node /usr/src/app/dist/angular-docker /usr/src/app