openfaas / python-flask-template

HTTP and Flask-based OpenFaaS templates for Python 3
MIT License
85 stars 86 forks source link

Remove unnecessary package cache files for apt/pip, implement multistage builds to minimize container image #57

Closed rgschmitz1 closed 1 year ago

rgschmitz1 commented 2 years ago

My actions before raising this issue

Expected Behaviour

I'd recommend cleaning up the cache files from apt and pip. tox testing can be performed without additional final image padding by using multistage build mechanisms.

Current Behaviour

Package cache files from apt and pip are creating larger container images. tox also significantly increase in the final container image size as it creates a virtual environment and installs several packages.

Possible Solution

FROM builder AS tester ARG TEST_COMMAND=tox ARG TEST_ENABLED=true RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND" FROM builder AS final ``` Doing the above reduced the deployed container image size by 28% for the python3-flask-debian template (results will likely vary a depending on additional packages installed). Build time didn't appear to be affected by modifications. ## Steps to Reproduce (for bugs) 1. Copy Dockerfile from [gist](https://gist.github.com/rgschmitz1/3c1536b455da3703fb75d9672d556d02). 2. Use faas-cli to build a container image from above Dockerfile. 3. Use faas-cli to build a container image with an unmodified Dockerfile. 4. Compare container image sizes. ## Context I'm a graduate student working on a masters capstone comparing performance and cost of cloud native applications running with open sourced software to proprietary solutions from cloud vendors. I'm also working with a company (BioDepot LLC) that uses Docker for packaging their software. I'd like to pass along some helpful tips that I've learned from maintaining container images. ## Your Environment * FaaS-CLI version ( Full output from: `faas-cli version` ): ``` ___ _____ ____ / _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___| | | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \ | |_| | |_) | __/ | | | _| (_| | (_| |___) | \___/| .__/ \___|_| |_|_| \__,_|\__,_|____/ |_| CLI: commit: b1c09c0243f69990b6c81a17d7337f0fd23e7542 version: 0.14.2 ``` * Docker version `docker version` (e.g. Docker 17.0.05 ): ``` Docker version 20.10.14, build a224086 ``` * Are you using Docker Swarm or Kubernetes (FaaS-netes)? `Kubernetes` * Operating System and version (e.g. Linux, Windows, MacOS): ``` NAME="Ubuntu" VERSION="20.04.4 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.04.4 LTS" VERSION_ID="20.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=focal UBUNTU_CODENAME=focal ``` * Code example or link to GitHub repo or gist to reproduce problem: https://gist.github.com/rgschmitz1/3c1536b455da3703fb75d9672d556d02 * Other diagnostic information / logs from [troubleshooting guide](https://docs.openfaas.com/deployment/troubleshooting)
alexellis commented 1 year ago

@LucasRoesler raised a PR for this, it needs a rebase, then we can get this merged.

Did you already take a look at #59?

rgschmitz1 commented 1 year ago

Hi @alexellis , PR #59 is exactly what I was looking for. Thanks for the support again!