简体   繁体   English

减小 Docker 镜像大小

[英]Decrease Docker Image Size

So I am trying to deploy a multi container service(web, worker, redis) using docker-compose to ECS.所以我正在尝试使用 docker-compose 将多容器服务(web、worker、redis)部署到 ECS。 The service has a ML model which requires Tensorflow and Keras as dependencies.该服务有一个 ML 模型,需要 Tensorflow 和 Keras 作为依赖项。 After running the pip install -r requirements.txt, the image size increases to more than 2gb, 1.75gb of which are dependencies.运行pip install -r requirements.txt后,镜像大小增加到2g多,其中1.75gb是依赖。 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:用于网络的 Dockerfile:

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

Given that your dependencies are causing the increase of 1.75GB you should start there.鉴于您的依赖项导致 1.75GB 的增加,您应该从那里开始。 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.如果您在pip install --no-cache-dir -r requirements.txt && rm -rf somepath之后立即删除,则 docker 映像将不会增长。
  4. Next you could search for a more optimized base image.接下来,您可以搜索更优化的基础映像。 You could try python:3.6-slim instead.你可以试试python:3.6-slim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM