How to make your Python Docker images secure, fast & small

<p>In the world of&nbsp;<strong>microservices</strong>, having secure, efficient, and compact&nbsp;<a href="https://www.docker.com/" rel="noopener ugc nofollow" target="_blank"><strong>Docker</strong></a>&nbsp;images is crucial for successful deployments. This blog will explore the key factors that contribute to building such images, including the importance of not running images as the root user, updating and upgrading packages when building the image, considering Docker&rsquo;s layer architecture while writing Dockerfile instructions, and&nbsp;utilizing multistage builds to decrease your image size.&nbsp;Parts of this blog were inspired by a great talk that I recently from Matthijs Brouns, which you can view&nbsp;<a href="https://www.youtube.com/watch?v=kx-SeGbkNPU" rel="noopener ugc nofollow" target="_blank">here</a>.</p> <p>I will be using the CLI tool&nbsp;<a href="https://github.com/wagoodman/dive" rel="noopener ugc nofollow" target="_blank"><strong>Dive</strong></a>&nbsp;to analyze the image and its layers. The aim of the Docker image is to serve as a host for a&nbsp;<a href="https://fastapi.tiangolo.com/" rel="noopener ugc nofollow" target="_blank">FASTAPI</a>&nbsp;server for a machine learning application, with Poetry as its dependency manager. For more information on the motivation to use Poetry instead of pip/pipenv/pip-tools/conda, read&nbsp;<a href="https://andrewbrookins.com/python/why-poetry/" rel="noopener ugc nofollow" target="_blank"><strong>this</strong></a>&nbsp;and<strong>&nbsp;</strong><a href="https://betterprogramming.pub/5-reasons-why-poetry-beats-pip-python-setup-6f6bd3488a04" rel="noopener ugc nofollow" target="_blank"><strong>this</strong></a>&nbsp;blog. Please note that a basic understanding of Docker and&nbsp;<a href="https://docs.docker.com/engine/reference/builder/" rel="noopener ugc nofollow" target="_blank"><strong>Dockerfiles</strong></a>&nbsp;is a prerequisite for this article.</p> <p><a href="https://medium.com/vantageai/how-to-make-your-python-docker-images-secure-fast-small-b3a6870373a0"><strong>Website</strong></a></p>
Tags: Python Docker