Containerize a Python application
Prerequisites
- You have installed the latest version of Docker Desktop.
Overview
Containerizing your application means packaging it together with its dependencies, configuration, and runtime into a single portable unit called a container image. Running that image creates a container, an isolated process that behaves the same on any machine, whether it's your laptop, a CI runner, or a production server.
In this section, you'll containerize a simple
FastAPI web application. You'll write a
Dockerfile that describes how to build the image, add a compose.yaml file
that defines how Docker runs your container, and then build and start the
application with one command.
You'll use Docker Hardened Images as the base. These are minimal, secure Python images maintained by Docker.
Create the application
The sample application is a minimal FastAPI service with a single endpoint
that returns a JSON greeting. Create the following files in a new
python-docker-example directory. To create all the files at once, switch to
the Scaffold script tab in the file browser and copy the shell command.
If you already have Python installed and want to verify the app works before containerizing it, you can run it locally:
$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install -r requirements.txt
$ uvicorn app:app --reload
NoteOn Windows, activate the virtual environment with
.venv\Scripts\activateinstead ofsource .venv/bin/activate.
If you don't have Python installed, skip ahead to the next section. The remaining steps run the application in a container, with no local Python required.
Create the Docker assets
Sign in to the DHI registry so Docker can pull the Python base images during the build. The available Python images are listed in the catalog.
$ docker login dhi.io
Add the following three files to your python-docker-example directory. The
Dockerfile describes how to build the image, compose.yaml defines how
Docker runs the container, and .dockerignore keeps unwanted files out of the
build context.
TipGordon, Docker's AI assistant, can generate Docker assets for your project. Ask Gordon to create a Dockerfile, Compose file, and
.dockerignoretailored to your application.
To learn more about each file, see the following:
Run the application
Inside the python-docker-example directory, run the following command in a
terminal.
$ docker compose up --build
Open a browser and view the application at http://localhost:8000. You should see a simple FastAPI application.
In the terminal, press ctrl+c to stop the application.
Run the application in the background
You can run the application detached from the terminal by adding the -d
option. Inside the python-docker-example directory, run the following command
in a terminal.
$ docker compose up --build -d
Open a browser and view the application at http://localhost:8000.
To see the OpenAPI docs you can go to http://localhost:8000/docs.
You should see a simple FastAPI application.
In the terminal, run the following command to stop the application.
$ docker compose down
For more information about Compose commands, see the Compose CLI reference.
Summary
In this section, you learned how you can containerize and run your Python application using Docker.
Related information:
Next steps
In the next section, you'll take a look at how to set up a local development environment using Docker containers.