top of page
Search
Writer's pictureGal Cohen

Python and Docker multistage build

Updated: Apr 24, 2021




Here I suggest a neat way of creating lean python docker images, using multistage build and pip wheel archive

In my previous post I suggested a way of creating a lean image, by installing different compilers (gcc, python-dev etc.) in order to build our project dependencies.

After installation of all the dependencies, we remove the unnecessary (for run-time) compilers:

FROM alpine:3.7

MAINTAINER GalCo

ADD requirements.txt /

RUN apk add --update \

python \

python-dev \

py-pip \

build-base \

postgresql-dev \

gcc \

musl-dev \

linux-headers \

nginx \

bash \

&& rm -r -f /var/cache/apk/* \

&& pip install -r /requirements.txt \

&& apk del gcc \

python-dev \

build-base \

musl-dev \

linux-headers

What we see here, is that in the last 5 lines, we are removing the compile-time dependencies

Python multistage build with wheel archive

The new feature of docker called multistage-build, allows us doing so, in such manner that we don't need to remove the redundant dependencies. Instead we use a different image, and copying our project with its already compiled dependencies:

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=/shadow_reporting/wheels -r requirements.txt

As you can see, we are using as statement in the first FROM statement, this is aliasing the first image with name base

To use the dependencies without reinstalling the compilers in the target image, we use pip wheel archive.

Have a good one!


9,082 views0 comments
bottom of page