Search code examples
pythondockerpipubuntu-16.04docker-build

Installation of python module using `pip` during `docker build` fails


I'm trying to build a Docker container in which I want to install some Python modules using pip (these modules will eventually be needed to build an app). I have written the following Dockerfile:

FROM ubuntu:16.04

RUN echo "===> Adding prerequisites..."   && \
    apt update -y            && \
    DEBIAN_FRONTEND=noninteractive  \
        apt install --no-install-recommends -y -q  \
                build-essential                        \
                python python-pip python-dev           && \
    pip install --upgrade setuptools pip wheel      && \
    pip install --upgrade pyyaml

# Default command
CMD [ "echo", "Hello!" ]

I get the following output when building (docker build -t app:0 .):

Sending build context to Docker daemon  10.26MB
Step 1/3 : FROM ubuntu:16.04
 ---> 4a689991aa24
Step 2/3 : RUN echo "===> Adding prerequisites..."   &&     apt update -y            &&     DEBIAN_FRONTEND=noninteractive          apt install --no-install-recommends -y -q                  build-essential                                        python python-pip python-dev           &&     pip install --upgrade setuptools pip wheel      &&     pip install --upgrade pyyaml
 ---> Running in 9d813b5bc68d
===> Adding prerequisites...

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
[More apt output, no errors here]
Get:18 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B]
Fetched 15.5 MB in 1s (11.3 MB/s)
Reading package lists...
Building dependency tree...
Reading state information...
All packages are up to date.

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  binutils bzip2 ca-certificates cpp cpp-5 dpkg-dev g++ g++-5 gcc gcc-5
  libasan2 libatomic1 libc-dev-bin libc6-dev libcc1-0 libcilkrts5 libdpkg-perl
  libexpat1 libexpat1-dev libffi6 libgcc-5-dev libgdbm3 libgmp10 libgomp1
  libisl15 libitm1 liblsan0 libmpc3 libmpfr4 libmpx0 libperl5.22 libpython-dev
  libpython-stdlib libpython2.7 libpython2.7-dev libpython2.7-minimal
  libpython2.7-stdlib libquadmath0 libsqlite3-0 libssl1.0.0 libstdc++-5-dev
  libtsan0 libubsan0 linux-libc-dev make mime-support openssl patch perl
  perl-modules-5.22 python-minimal python-pip-whl python2.7 python2.7-dev
  python2.7-minimal xz-utils
Suggested packages:
  binutils-doc bzip2-doc cpp-doc gcc-5-locales debian-keyring g++-multilib
  g++-5-multilib gcc-5-doc libstdc++6-5-dbg gcc-multilib manpages-dev autoconf
  automake libtool flex bison gdb gcc-doc gcc-5-multilib libgcc1-dbg
  libgomp1-dbg libitm1-dbg libatomic1-dbg libasan2-dbg liblsan0-dbg
  libtsan0-dbg libubsan0-dbg libcilkrts5-dbg libmpx0-dbg libquadmath0-dbg
  glibc-doc libstdc++-5-doc make-doc ed diffutils-doc perl-doc
  libterm-readline-gnu-perl | libterm-readline-perl-perl python-doc python-tk
  python2.7-doc binfmt-support
Recommended packages:
  fakeroot libalgorithm-merge-perl manpages manpages-dev
  libfile-fcntllock-perl file netbase rename python-all-dev python-setuptools
  python-wheel
The following NEW packages will be installed:
  binutils build-essential bzip2 ca-certificates cpp cpp-5 dpkg-dev g++ g++-5
  gcc gcc-5 libasan2 libatomic1 libc-dev-bin libc6-dev libcc1-0 libcilkrts5
  libdpkg-perl libexpat1 libexpat1-dev libffi6 libgcc-5-dev libgdbm3 libgmp10
  libgomp1 libisl15 libitm1 liblsan0 libmpc3 libmpfr4 libmpx0 libperl5.22
  libpython-dev libpython-stdlib libpython2.7 libpython2.7-dev
  libpython2.7-minimal libpython2.7-stdlib libquadmath0 libsqlite3-0
  libssl1.0.0 libstdc++-5-dev libtsan0 libubsan0 linux-libc-dev make
  mime-support openssl patch perl perl-modules-5.22 python python-dev
  python-minimal python-pip python-pip-whl python2.7 python2.7-dev
  python2.7-minimal xz-utils
0 upgraded, 60 newly installed, 0 to remove and 0 not upgraded.
Need to get 79.5 MB of archives.
After this operation, 251 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial/main amd64 libgdbm3 amd64 1.8.3-13.1 [16.9 kB]
[More apt output, no errors here]
Get:60 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 python-pip all 8.1.1-2ubuntu0.4 [144 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 79.5 MB in 1s (66.0 MB/s)
Selecting previously unselected package libgdbm3:amd64.
(Reading database ... 4768 files and directories currently installed.)
Preparing to unpack .../libgdbm3_1.8.3-13.1_amd64.deb ...
Unpacking libgdbm3:amd64 (1.8.3-13.1) ...
[Even more apt output]
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for ca-certificates (20170717~16.04.1) ...
Updating certificates in /etc/ssl/certs...
148 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/96/06/c8ee69628191285ddddffb277bd5abdf769166e7a14b867c2a172f0175b1/setuptools-40.4.3-py2.py3-none-any.whl (569kB)
Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/5a/9b/6aebe9e2636d35d1a93772fa644c828303e1d5d124e8a88f156f42ac4b87/wheel-0.32.2-py2.py3-none-any.whl
Installing collected packages: setuptools, pip, wheel
  Found existing installation: pip 8.1.1
    Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr
Successfully installed pip-18.1 setuptools-40.4.3 wheel-0.32.2
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main
The command '/bin/sh -c echo "===> Adding prerequisites..."   &&     apt update -y            &&     DEBIAN_FRONTEND=noninteractive          apt install --no-install-recommends -y -q                  build-essential                                        python python-pip python-dev           &&     pip install --upgrade setuptools pip wheel      &&     pip install --upgrade pyyaml' returned a non-zero code: 1

However, if I remove the second call to pip from the Dockerfile, it appears the container is built successfully.

Can anyone please help me understand what is triggering the build error? Many thanks in advance!


Solution

  • This appears to be an issue when upgrading pip as you can see here:
    https://github.com/pypa/pip/issues/5240

    Instead of running

    pip install --upgrade setuptools pip wheel
    

    try altering to

    python -m pip install --upgrade setuptools pip wheel
    

    as suggested:

    The docker image will then be built without errors.