Container Image

The first thing we need is a recipe for building a container image of the application. It’s often called a Dockerfile, but Podman uses the more generic name, Containerfile. (Calling every container “Docker” is not unlike calling every vacuum cleaner a “Hoover”.)

Base image#

This guide uses Red Hat’s Universal Base Images (UBI), which are solid, trusted, and free to use and redistribute. They’re also built to run on OpenShift. There are UBI images with Python ready installed:

podman pull registry.access.redhat.com/ubi9/python-312:latest

The latest UBI Python image is Python 3.12, which every package in this project supports.

Containerfile#

Add a Containerfile to the root of the project:

Containerfiledockerfile
FROM registry.access.redhat.com/ubi9/python-312:latest

LABEL maintainer="Richard Walker"

ENV PYTHONUNBUFFERED=1 \
    PYTHONIOENCODING=UTF-8 \
    PIP_NO_CACHE_DIR=1

WORKDIR /opt/app-root/src

# Install the requirements first, so this layer is reused when only the code changes
COPY requirements.txt ./
RUN pip install --upgrade pip && pip install -r requirements.txt

COPY . ./

# OpenShift runs containers as a random user id in group 0 (root group),
# so give group 0 the same permissions as the owner
USER 0
RUN chgrp -R 0 /opt/app-root/src && chmod -R g=u /opt/app-root/src
USER 1001

EXPOSE 8080

CMD ["./start.sh"]
  • PYTHONUNBUFFERED=1 makes Python write log output straight away, rather than holding it back, so it shows up in the container’s logs immediately. PIP_NO_CACHE_DIR=1 stops pip keeping downloads in the image.
  • requirements.txt is copied, and the requirements installed, before the rest of the code. Podman reuses unchanged steps from the previous build, so when only your code changes, this slow step is skipped.
  • The UBI image runs as user 1001. OpenShift goes further: it runs containers as a random user id, such as 1000690000, which isn’t in the image at all, but is always in group 0. So chgrp -R 0 and chmod -R g=u give group 0 the same permissions as the owner. This is the standard way to make an image work on OpenShift.
  • EXPOSE 8080 documents the port. On OpenShift, applications use ports above 1024, since they can’t run as root.

Keeping things out of the image#

COPY . ./ copies the whole project directory, which would include your virtual environment, local data and, worst of all, your .env file of secrets. A .containerignore file lists what to leave out:

.containerignoretext
# Keep the image small, and secrets and local data out of it
.git
.env
*.env
venv/
mnt/
**/__pycache__/
.pytest_cache/
test_*.py
conftest.py
openshift/

Starting the application#

The container runs a small script when it starts:

start.shbash
#!/bin/bash
# Container entrypoint: prepare the data directories, update the database, start the app
set -euo pipefail

mkdir -p mnt/db mnt/uploads
alembic upgrade head
exec fastapi run main.py --host 0.0.0.0 --port 8080 --proxy-headers --forwarded-allow-ips='*'
chmod +x start.sh
  • It creates the db and uploads directories, in case the volume mounted at mnt is new and empty.
  • alembic upgrade head brings the database up to date. Running it at startup means deploying a new version of the application also updates the database. (With a single copy of the application, as here, that’s safe. With several, you’d run migrations once, as a separate step.)
  • fastapi run starts the application in production mode, without auto-reload, listening on all addresses on port 8080. --proxy-headers and --forwarded-allow-ips tell it to trust the X-Forwarded-* headers from OpenShift’s router, which handles HTTPS in front of it.
  • exec replaces the script with the application, so it receives the signal to shut down when OpenShift stops the container.

TLS isn’t configured in the application: on OpenShift, the Route handles HTTPS, as you’ll see.

Build and run#

Build the image:

podman build -t localhost/example-for-you:1.0.0 .

Before going anywhere near OpenShift, run it the way OpenShift will: as an arbitrary user id in group 0, with a volume for its data, and the settings in an env file:

local.envtext
CONF_MAIN_DOMAIN=http://localhost:8080
CONF_AUTH_SECRET_KEY=paste-a-generated-key-here
CONF_AUTH_COOKIE_SECURE=false
podman volume create example-for-you-data
podman run -d --name example-for-you --user 12345:0 -p 8080:8080 \
  --env-file local.env \
  -v example-for-you-data:/opt/app-root/src/mnt:U \
  localhost/example-for-you:1.0.0

--user 12345:0 runs as a user that doesn’t exist in the image, in group 0. :U on the volume gives that user ownership of it, which is what OpenShift does for its volumes. (Keep local.env outside the project, or add it to .gitignore: it holds a secret.)

Check the logs:

podman logs example-for-you
INFO  [alembic.runtime.migration] Running upgrade  -> 655fbffd975e, init
INFO  [alembic.runtime.migration] Running upgrade 655fbffd975e -> d08def412c0a, added teaser
...
INFO  [alembic.runtime.migration] Running upgrade b1a2244b9822 -> 50f4368ceb78, added auth tokens
 ⚡️ Starting FastAPI in production mode
INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)

The application should now be at http://localhost:8080, with a fresh database. (If localhost doesn’t connect, try http://127.0.0.1:8080: Podman may only publish the port on IPv4.) To create a user to log in with, run the script from the Authentication section inside the container:

podman exec -it example-for-you python -m db.create_user admin@example.com Ada Admin --author --superuser

Some other useful commands:

podman ps -a                            # list containers, including stopped ones
podman exec -it example-for-you bash    # a shell inside the container
podman stop example-for-you
podman rm example-for-you
note

This was tested while writing the guide: the image runs as an arbitrary user, migrates an empty database, and serves the site. The Swagger docs and APIs are off, as CONF_ENABLE_APIS defaults to false.