Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Reduce size of the image #23

Open
jarshwah opened this issue Dec 4, 2018 · 3 comments
Open

Reduce size of the image #23

jarshwah opened this issue Dec 4, 2018 · 3 comments

Comments

@jarshwah
Copy link

jarshwah commented Dec 4, 2018

One way to reduce the final size of the image is to start from a more minimal base. There are alpine builds that can help with this, but it might mean there's a bunch of different setup to be done for databases etc, or it might be impossible to have the same support with alpine. See below for an edited version of an alpine based build I use in a work project:

FROM python:3.6-alpine

ENV PYTHONUNBUFFERED 1

WORKDIR /code

# Different for alpine
RUN addgroup -g 1000 -S django \
    && adduser -u 1000 -S django -G django \
    && mkdir -p /etc/ /code/webstatic/collectedstatic /code/webstatic/usermedia \
    && pip install pipenv

COPY Pipfile* /code/

# virtual "groups" packages together so they can be removed as a unit
RUN apk update \
  && apk add --virtual build-deps make gcc python3-dev musl-dev libffi-dev \
  && apk add postgresql-dev nodejs nodejs-npm \
  && pipenv install --ignore-pipfile --system \
  && npm install \
  && apk del build-deps

# Production specific below
COPY other files

USER django:django
ENTRYPOINT [ "docker-entrypoint.sh" ]

Then there's the possibility of using build files and copy-from to further reduce the final image size.

@orf
Copy link
Owner

orf commented Dec 5, 2018

Thanks Josh! I will experiment with using the alpine images and put the results here.

I don't know how much your dockerfile above is edited, but adding ENV PIP_NO_CACHE_DIR=off can reduce image size by a fair bit as well.

@orf orf closed this as completed in #24 Dec 5, 2018
@orf orf reopened this Dec 5, 2018
@orf
Copy link
Owner

orf commented Dec 5, 2018

Oops, didn't mean to close the issue as there is still more we can do.

Regarding using the alpine images: I'd like to explore this, but it's a fair bit of work. We need to ensure each of the system packages has a compatible version. For example gdal is still in testing: https://pkgs.alpinelinux.org/packages?name=gdal&branch=edge

By using the -slim package and also by using ENV PYTHONDONTWRITEBYTECODE=1 (which adds about 50+mb!) the image goes down from 500mb compressed to 260mb. Locally it goes down about 600mb to 740mb 🎉

@jarshwah
Copy link
Author

jarshwah commented Dec 5, 2018

Half the size for basically free is a great result 🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants