Search⌘ K

Containerize a Python Application

Understand how to containerize a Python web application by creating a FastAPI app, writing a Dockerfile, building a Docker image, and running it as a container. This lesson helps you apply Docker concepts for Python frameworks and deploy APIs quickly and securely.

We begin by creating a simple web application in FastAPI. Then, we use that application to create a Docker image, and finally, we launch a container from that image.

FastAPI is a modern, fast (high performance) web framework for building APIs with Python 3.6+ based on standard Python-type hints. Though FastAPI is used in this lesson, we can apply what we learn to other Python frameworks, such as Flask, Django, and so on.

The application is a web API that returns a random name. If we haven’t cloned the course’s repository yet, we can clone it by running the following:

git clone https://github.com/abiodunjames/docker-lessons.git

We can find the source code for this lesson in the path-to-docker-lessons/python-fastapi/exercise directory.

Inside the exercise directory, there are two main files:

...