Docker Essentials — Image Optimization
Regardless of your role as a developer, sysadmin, or data scientist, Docker should be no stranger to you. In this article, let’s explore how we can optimize a Python Django-based application image from >400 MB to < 30 MB for production deployment.
The guidelines should apply to other programming languages, front-end or back-end frameworks you choose to build an application.
Setting up a Django-based App
Let’s start by setting up a Django-based app.
You can easily create a Django application following the documentation.
$ django-admin startproject django_slim
A project with the following structure is created.
I am going to use Uvicorn with Gunicorn to start up the application. Let’s create a
requirements.txt file with the following content.
Dockerfile Without Optimization
Let’s create a Dockerfile to dockerize the application.
- The Dockerfile uses the Python slim buster image.
- It installs all the dependencies needed to compile and install the libraries. (Note: In this simple sample we may not need
python3-dev, etc but certain Python libraries need them to compile and install properly)
- It uses the
entry_point.shfile to start the application. The file has the following content.