Pip3 is unable to install requirements.txt during docker build

Viewed 1334

I am using docker tutorial (https://docs.docker.com/language/python/build-images/) to build a simple python app. Using freeze command I made requirements.txt file which consists a lot of packages.

When I want to build the docker image, I am getting this error:

Step 4/6 : RUN pip3 install -r requirements.txt ---> Running in f92acd21d271

ERROR: Could not find a version that satisfies the requirement apt-clone==0.2.1 (from versions: none)

ERROR: No matching distribution found for apt-clone==0.2.1

The command '/bin/sh -c pip3 install -r requirements.txt' returned a non-zero code: 1

This is my dockerfile contents (same as what is mentioned in the tutorial):

# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "-m", "flask","run","--host=0.0.0.0" ]

It's not related to apt-clone==0.2.1 package. Whatever I try to install in the docker image, it fails. I tried apt update and installing pip3 in the dockerfile too but didn't work.

What did I miss?

2 Answers

There is no package named apt-clone in the public PyPI repository, so pip3 obviously cannot find it.

If you actually have a Python package named like this, where did it come from?

  • If you created a package with this name locally, you need to install it too in your Docker image somehow.

  • If you are inside an organization, maybe you have a local PyPI with different packages than the public one, and then you need to configure pip3 to use it (try pip3 -i http://your.local.pypi/simple -r requirements.txt where obviously the URL needs to be something else, but we can't guess what).

If pip3 freeze says you have this package, it must have come from somewhere, but we don't know where. You have to figure that out, or supply more details to help us help you.

There is a Debian package named apt-clone but that obviously isn't something pip can install. Did you actually mean apt-get install -y apt-clone==0.2.1? (I can only find version 0.4.1 in Debian Buster, though.)

... @atline's answer explains what happened (you should probably accept that) but the simple fix is to manually populate requirements.txt with your actual requirements. pip3 freeze is a nice shorthand for that if you are in a virtual environment, but it's not a proper replacement for a manually curated requirements.txt or setup.py (or its modern replacements, currently moving from setup.cfg to pyproject.toml). If flask is the only package your app needs, simply put pip3 install flask and don't create a requirements.txt.

pip3 freeze outputs the package and its version installed in the current environment, no matter the package installed by pip or with other methods.

In fact, apt-clone==0.2.1 comes from debian package repo, not from pypi.org, see next:

$ pip3 freeze | grep apt-clone
$ apt-get install -y apt-clone
$ dpkg -l apt-clone
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
ii  apt-clone      0.4.1        all          Script to create state bundles
$  dpkg -L apt-clone
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/apt-clone
/usr/share/doc/apt-clone/copyright
/usr/share/doc/apt-clone/changelog.gz
/usr/share/man
/usr/share/man/man8
/usr/share/man/man8/apt-clone.8.gz
/usr/lib
/usr/lib/python3
/usr/lib/python3/dist-packages
/usr/lib/python3/dist-packages/apt_clone.py
/usr/lib/python3/dist-packages/apt_clone-0.2.1.egg-info
/usr/bin
/usr/bin/apt-clone
$ pip3 freeze | grep apt-clone
apt-clone==0.2.1

You could see from above, the apt_clone.py & apt_clone-0.2.1.egg-info are installed by debian package apt-clone, the 0.4.1 is just the debian package version, while 0.2.1 is the python package version.

So, for apt-clone similar, you need to install them with apt although they are seen in pip3 freeze.

Related