top | item 22184591

(no title)

Deradon | 6 years ago

I'd structure the Dockerfile in another way with the "python:3.8-alpine" approach.

E.g. something like:

  FROM python:3.8-alpine
  
  RUN apk add --no-cache --virtual build-dependencies gcc build-base freetype-dev libpng-dev openblas-dev && \
    pip install --no-cache-dir matplotlib pandas && \
    apk del build-dependencies
This way you won't keep the build dependencies in the layer and the final image. Maybe not all of the packages are build-dependencies, but at least there is no need to keep the gcc and build-base package around. Long story short, wrap your "pip install", "bundle install", "yarn install" around some "apk add .. && apk del" within one RUN.

Yes, it would still compile for quite some time, but the final image size is not "851MB" but "489MB". Still larger than the "363MB" of the "python-slim" version. Guess "pip install" will keep some build artifacts around?

discuss

order

je42|6 years ago

alternatively you can create a multi staged build.

also you dont need clean up the builder, since the unused files are not used in the final image.

also add a new file you can just append it to the builder. (not rebuild required of the previous installs required since you don't care about the size of the builder image )

  FROM python:3.8-alpine as builder
  
  RUN apk add --virtual build-dependencies gcc build-base freetype-dev libpng-dev openblas-dev
  RUN pip install matplotlib pandas

  FROM python:3.8-alpine
  # a couple of copy commands to copy binaries and py files from builder to the final image. 
  COPY --from=builder /usr/bin /usr/bin
  COPY --from=builder /usr/local/lib/ /usr/local/lib/

verst|6 years ago

Exactly. This is what I always do.

I would never want any build tools in the final Alpine image.

Arnavion|6 years ago

TIL about `--virtual`. This is very useful.

>-t, --virtual NAME

>Instead of adding all the packages to 'world', create a new virtual package with the listed dependencies and add that to 'world'; the actions of the command are easily reverted by deleting the virtual package

daze42|6 years ago

Your guess is correct. Pip does keep a cache of packages around. It's easy to fix by setting the `--no-cache-dir` switch when installing:

`pip install --no-cache-dir -r requirements.txt`