How to avoid reinstalling packages when building Docker image for Python projects?

satoru

My Dockerfile is something like

FROM my/base

ADD . /srv
RUN pip install -r requirements.txt
RUN python setup.py install

ENTRYPOINT ["run_server"]

Every time I build a new image, dependencies have to be reinstalled, which could be very slow in my region.

One way I think of to cache packages that have been installed is to override the my/base image with newer images like this:

docker build -t new_image_1 .
docker tag new_image_1 my/base

So next time I build with this Dockerfile, my/base already has some packages installed.

But this solution has two problems:

  1. It is not always possible to override a base image
  2. The base image grow bigger and bigger as newer images are layered on it

So what better solution could I use to solve this problem?

EDIT##:

Some information about the docker on my machine:

☁  test  docker version
Client version: 1.1.2
Client API version: 1.13
Go version (client): go1.2.1
Git commit (client): d84a070
Server version: 1.1.2
Server API version: 1.13
Go version (server): go1.2.1
Git commit (server): d84a070
☁  test  docker info
Containers: 0
Images: 56
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Dirs: 56
Execution Driver: native-0.2
Kernel Version: 3.13.0-29-generic
WARNING: No swap limit support
nacyot

Try to build a Dockerfile which looks something like this:

FROM my/base

WORKDIR /srv
ADD ./requirements.txt /srv/requirements.txt
RUN pip install -r requirements.txt
ADD . /srv
RUN python setup.py install

ENTRYPOINT ["run_server"]

Docker will use cache during pip install as long as you do not make any changes to the requirements.txt, irrespective of the fact whether other code files at . were changed or not. Here's an example.


Here's a simple Hello, World! program:

$ tree
.
├── Dockerfile
├── requirements.txt
└── run.py   

0 directories, 3 file

# Dockerfile

FROM dockerfile/python
WORKDIR /srv
ADD ./requirements.txt /srv/requirements.txt
RUN pip install -r requirements.txt
ADD . /srv
CMD python /srv/run.py

# requirements.txt
pytest==2.3.4

# run.py
print("Hello, World")

The output of docker build:

Step 1 : WORKDIR /srv
---> Running in 22d725d22e10
---> 55768a00fd94
Removing intermediate container 22d725d22e10
Step 2 : ADD ./requirements.txt /srv/requirements.txt
---> 968a7c3a4483
Removing intermediate container 5f4e01f290fd
Step 3 : RUN pip install -r requirements.txt
---> Running in 08188205e92b
Downloading/unpacking pytest==2.3.4 (from -r requirements.txt (line 1))
  Running setup.py (path:/tmp/pip_build_root/pytest/setup.py) egg_info for package pytest
....
Cleaning up...
---> bf5c154b87c9
Removing intermediate container 08188205e92b
Step 4 : ADD . /srv
---> 3002a3a67e72
Removing intermediate container 83defd1851d0
Step 5 : CMD python /srv/run.py
---> Running in 11e69b887341
---> 5c0e7e3726d6
Removing intermediate container 11e69b887341
Successfully built 5c0e7e3726d6

Let's modify run.py:

# run.py
print("Hello, Python")

Try to build again, below is the output:

Sending build context to Docker daemon  5.12 kB
Sending build context to Docker daemon 
Step 0 : FROM dockerfile/python
---> f86d6993fc7b
Step 1 : WORKDIR /srv
---> Using cache
---> 55768a00fd94
Step 2 : ADD ./requirements.txt /srv/requirements.txt
---> Using cache
---> 968a7c3a4483
Step 3 : RUN pip install -r requirements.txt
---> Using cache
---> bf5c154b87c9
Step 4 : ADD . /srv
---> 9cc7508034d6
Removing intermediate container 0d7cf71eb05e
Step 5 : CMD python /srv/run.py
---> Running in f25c21135010
---> 4ffab7bc66c7
Removing intermediate container f25c21135010
Successfully built 4ffab7bc66c7

As you can see above, this time docker uses cache during the build. Now, let's update requirements.txt:

# requirements.txt

pytest==2.3.4
ipython

Below is the output of docker build:

Sending build context to Docker daemon  5.12 kB
Sending build context to Docker daemon 
Step 0 : FROM dockerfile/python
---> f86d6993fc7b
Step 1 : WORKDIR /srv
---> Using cache
---> 55768a00fd94
Step 2 : ADD ./requirements.txt /srv/requirements.txt
---> b6c19f0643b5
Removing intermediate container a4d9cb37dff0
Step 3 : RUN pip install -r requirements.txt
---> Running in 4b7a85a64c33
Downloading/unpacking pytest==2.3.4 (from -r requirements.txt (line 1))
  Running setup.py (path:/tmp/pip_build_root/pytest/setup.py) egg_info for package pytest

Downloading/unpacking ipython (from -r requirements.txt (line 2))
Downloading/unpacking py>=1.4.12 (from pytest==2.3.4->-r requirements.txt (line 1))
  Running setup.py (path:/tmp/pip_build_root/py/setup.py) egg_info for package py

Installing collected packages: pytest, ipython, py
  Running setup.py install for pytest

Installing py.test script to /usr/local/bin
Installing py.test-2.7 script to /usr/local/bin
  Running setup.py install for py

Successfully installed pytest ipython py
Cleaning up...
---> 23a1af3df8ed
Removing intermediate container 4b7a85a64c33
Step 4 : ADD . /srv
---> d8ae270eca35
Removing intermediate container 7f003ebc3179
Step 5 : CMD python /srv/run.py
---> Running in 510359cf9e12
---> e42fc9121a77
Removing intermediate container 510359cf9e12
Successfully built e42fc9121a77

Notice how docker didn't use cache during pip install. If it doesn't work, check your docker version.

Client version: 1.1.2
Client API version: 1.13
Go version (client): go1.2.1
Git commit (client): d84a070
Server version: 1.1.2
Server API version: 1.13
Go version (server): go1.2.1
Git commit (server): d84a070

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

InsecurePlatformWarning when building Docker image

From Dev

Assigning port when building flask docker image

From Dev

"virtual memory exhausted" when building Docker image

From Dev

No such file or directory, when building scala docker image

From Dev

How to avoid repetitive naming in python packages?

From Dev

How to apply a changelog file to packages in a PPA when building from recipe?

From Dev

How to apply a changelog file to packages in a PPA when building from recipe?

From Dev

Building the MongoDB Docker image

From Dev

How QtCreator is able to avoid the console window when building a Windows application?

From Dev

How do I avoid nested loops when building a nav?

From Dev

Docker: "lstat no such file or directory" error when building image. File is there

From Dev

Can't npm install dependencies when building docker image

From Dev

Met "/bin/bash: no such file" when building docker image from scratch

From Dev

Use a configuration file from the host when building a Docker image

From Dev

Error when building the blackbox_exporter Docker image

From Dev

gem install fails when building an image on Docker for Windows

From Dev

How to avoid building C library with my python package?

From Dev

Building Python Extension Packages for Launchpad

From Dev

How to find out which backported packages are available, and avoid losing packages when manually upgrading?

From Dev

How to find out which backported packages are available, and avoid losing packages when manually upgrading?

From Dev

Reinstalling all Debian packages

From Dev

Run lint when building android studio projects

From Dev

NullPointerException when building Liferay projects with Maven

From Dev

jobserver unavailable when building external projects with CMake

From Dev

Run lint when building android studio projects

From Dev

React Js Error when building projects

From Dev

How to avoid reinstalling dependencies for each job in Gitlab CI

From Dev

How to avoid reinstalling ruby each time I restart?

From Dev

How to avoid manually reinstalling zfs after kernel update

Related Related

  1. 1

    InsecurePlatformWarning when building Docker image

  2. 2

    Assigning port when building flask docker image

  3. 3

    "virtual memory exhausted" when building Docker image

  4. 4

    No such file or directory, when building scala docker image

  5. 5

    How to avoid repetitive naming in python packages?

  6. 6

    How to apply a changelog file to packages in a PPA when building from recipe?

  7. 7

    How to apply a changelog file to packages in a PPA when building from recipe?

  8. 8

    Building the MongoDB Docker image

  9. 9

    How QtCreator is able to avoid the console window when building a Windows application?

  10. 10

    How do I avoid nested loops when building a nav?

  11. 11

    Docker: "lstat no such file or directory" error when building image. File is there

  12. 12

    Can't npm install dependencies when building docker image

  13. 13

    Met "/bin/bash: no such file" when building docker image from scratch

  14. 14

    Use a configuration file from the host when building a Docker image

  15. 15

    Error when building the blackbox_exporter Docker image

  16. 16

    gem install fails when building an image on Docker for Windows

  17. 17

    How to avoid building C library with my python package?

  18. 18

    Building Python Extension Packages for Launchpad

  19. 19

    How to find out which backported packages are available, and avoid losing packages when manually upgrading?

  20. 20

    How to find out which backported packages are available, and avoid losing packages when manually upgrading?

  21. 21

    Reinstalling all Debian packages

  22. 22

    Run lint when building android studio projects

  23. 23

    NullPointerException when building Liferay projects with Maven

  24. 24

    jobserver unavailable when building external projects with CMake

  25. 25

    Run lint when building android studio projects

  26. 26

    React Js Error when building projects

  27. 27

    How to avoid reinstalling dependencies for each job in Gitlab CI

  28. 28

    How to avoid reinstalling ruby each time I restart?

  29. 29

    How to avoid manually reinstalling zfs after kernel update

HotTag

Archive