Member-only story

Docker Essentials — Image Optimization

Optimize your Docker images for production deployment.

--

Essential Docker Tips and Tricks

Overview

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.

django_slim/
manage.py
django_slim/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py

requirements.txt

I am going to use Uvicorn with Gunicorn to start up the application. Let’s create a requirements.txt file with the following content.

Django
uvicorn
gunicorn

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 gcc, python3-dev, etc but certain Python libraries need them to compile and install properly)
  • It uses the entry_point.sh file to start the application. The file has the following content.

--

--

alpha2phi
alpha2phi

Written by alpha2phi

Software engineer, Data Science and ML practitioner.

No responses yet