Search code examples
pythondockerdocker-composeamazon-ecs

Decrease Docker Image Size


So I am trying to deploy a multi container service(web, worker, redis) using docker-compose to ECS. The service has a ML model which requires Tensorflow and Keras as dependencies. After running the pip install -r requirements.txt, the image size increases to more than 2gb, 1.75gb of which are dependencies. Is there a way to decrease this image size? Since deploying such a large image on cloud has a lot of cons?

Dockerfile for web:

FROM python:3.6
WORKDIR .
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
CMD ["python", "views.py"]
requirements.txt:
Flask==1.1.2
numpy==1.18.5
opencv-python==4.2.0.34
Pillow==7.1.2
redis==3.5.3
rq==1.4.3
boto3
pymongo
Keras==2.2.4
tensorflow==1.15.0
matplotlib

Solution

  • Given that your dependencies are causing the increase of 1.75GB you should start there. There is no easy silver bullet. These are the first steps:

    1. See how much the individual dependencies increase the image. Start with biggest offender.
    2. See if the dependencies allow for a different installation variants. Maybe there is a version that does not need the full library.
    3. See if some parts of the dependencies can be deleted manually. If you do the deleting right after pip install --no-cache-dir -r requirements.txt && rm -rf somepath the docker image will not grow.
    4. Next you could search for a more optimized base image. You could try python:3.6-slim instead.