How do I reduce a python (docker) image size using a multi-stage build?

后端 未结 3 1196
心在旅途
心在旅途 2020-12-31 03:05

I am looking for a way to create multistage builds with python and Dockerfile:

For example, using the following images:

1st image: install

3条回答
  •  再見小時候
    2020-12-31 03:40

    ok so my solution is using wheel, it lets us compile on first image, create wheel files for all dependencies and install them in the second image, without installing the compilers

    FROM python:2.7-alpine as base
    
    RUN mkdir /svc
    COPY . /svc
    WORKDIR /svc
    
    RUN apk add --update \
        postgresql-dev \
        gcc \
        musl-dev \
        linux-headers
    
    RUN pip install wheel && pip wheel . --wheel-dir=/svc/wheels
    
    FROM python:2.7-alpine
    
    COPY --from=base /svc /svc
    
    WORKDIR /svc
    
    RUN pip install --no-index --find-links=/svc/wheels -r requirements.txt
    

    You can see my answer regarding this in the following blog post

    https://www.blogfoobar.com/post/2018/02/10/python-and-docker-multistage-build

提交回复
热议问题