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

后端 未结 3 1180
心在旅途
心在旅途 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:31

    I recommend the approach detailed in this article (section 2). He uses virtualenv so pip install stores all the python code, binaries, etc. under one folder instead of spread out all over the file system. Then it's easy to copy just that one folder to the final "production" image. In summary:

    Compile image

    • Activate virtualenv in some path of your choosing.
    • Prepend that path to your docker ENV. This is all virtualenv needs to function for all future docker RUN and CMD action.
    • Install system dev packages and pip install xyz as usual.

    Production image

    • Copy the virtualenv folder from the Compile Image.
    • Prepend the virtualenv folder to docker's PATH
    0 讨论(0)
  • 2020-12-31 03:35

    The docs on this explain exactly how to do this.

    https://docs.docker.com/engine/userguide/eng-image/multistage-build/#before-multi-stage-builds

    Basically you do exactly what you've said. The magic of multistage build feature though is that you can do this all from one dockerfile.

    ie:

    FROM golang:1.7.3
    WORKDIR /go/src/github.com/alexellis/href-counter/
    RUN go get -d -v golang.org/x/net/html  
    COPY app.go .
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
    
    FROM alpine:latest  
    RUN apk --no-cache add ca-certificates
    WORKDIR /root/
    COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
    CMD ["./app"]  
    

    This builds a go binary, then the next image runs the binary. The first image has all the build tools and the seccond is just a base linux machine that can run a binary.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题