2

When I build an image of new python application needing TensorFlow (import tensorflow), every time docker installs TensorFlow of 520 MB.

How to avoid this? Means download tensorflow only once and use it while building many images?

Dockerfile

FROM python:3

WORKDIR /usr/src/app

COPY model.py .
COPY model_08015_07680.h5 .
COPY requirements.txt .
COPY images .
COPY labels.txt .
COPY test_run.py .

RUN pip install --no-cache-dir -r requirements.txt

CMD ["python","./test_run.py"]

requirements.txt

numpy
opencv-python
tensorflow
rph
  • 2,104
  • 2
  • 13
  • 24
lnx
  • 318
  • 2
  • 12
  • Can you add `Dockerfile` ? – nischay goyal Jun 06 '20 at 13:01
  • i have added dockerfile – lnx Jun 06 '20 at 13:03
  • Have a look below and let me know if you are still facing any issues – nischay goyal Jun 06 '20 at 13:05
  • 1
    I'd strongly recommend you to have a look at [Dockerfile Best Practices](https://www.docker.com/blog/intro-guide-to-dockerfile-best-practices/), especially tips 10 (Fetch dependencies in a separate step) and 11 (Use multi-stage builds to remove build dependencies). If you want to reuse your dependency for several different images, your best option is to create a base image with TensorFlow yourself or search for one at [Docker Hub](https://hub.docker.com/search?q=tensorflow&type=image). – rph Jun 06 '20 at 13:19
  • Does this answer your question? [How to avoid reinstalling packages when building Docker image for Python projects?](https://stackoverflow.com/questions/25305788/how-to-avoid-reinstalling-packages-when-building-docker-image-for-python-project) – rph Jun 06 '20 at 13:26

2 Answers2

1

You don't need to copy each file separately, this is not optimal.

Also, remember docker is built by layers, so every line that seems likely to change goes to the bottom.

FROM python:3

WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
#Copy everything
COPY . .    
CMD ["python","./test_run.py"]
paltaa
  • 2,985
  • 13
  • 28
0

Please use the below Dockerfile which is bit optimised as it will not install dependencies again and again, until/unless you are changing requirements.txt

FROM python:3

WORKDIR /usr/src/app

#Copy Requirements
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

#Copy everything later, as below parts will be changing and above part will be used from cache
COPY model.py .
COPY model_08015_07680.h5 .
COPY images .
COPY labels.txt .
COPY test_run.py .


CMD ["python","./test_run.py"]
nischay goyal
  • 3,206
  • 12
  • 23
  • wait wait my doubt is 1.now i am containerizing one python application needing tensorflow. so docker will install tensorflow 2.But now if i want to conatainarize completely different python application needing tensorflow , then docker will again install tensorflow so how to avoid this? – lnx Jun 06 '20 at 13:12
  • Then for that purpose, create 1 base image with tensorflow and then use that base image in each `Dockerfile` – nischay goyal Jun 06 '20 at 13:14
  • can you please provide code(like how to create base image of tensorflow and use it another dockerfile)? it will help me a lot – lnx Jun 06 '20 at 13:15
  • 1
    If you want a `tensorflow` specific base image, then use this `tensorflow/tensorflow` image which already exists on dockerhub. If you are trying to install something else as well, then create a base image by running the first 3 lines – nischay goyal Jun 06 '20 at 13:17