When you build and run a FastAPI application in a Podman container, you typically utilize environment variables to configure aspects of your application, such as database connection strings, API keys, or any other settings that should not be hard-coded. Here’s a general approach on how to set up your FastAPI application to utilize environment variables and how to pass those variables into your Podman container.
1. Use Environment Variables in Your FastAPI Application
In your main.py file (or wherever you define your FastAPI app), you can access environment variables using the os module or a library like python-dotenv to load them from a .env file. Here's a simple example using os:
import os
from fastapi import FastAPI
app = FastAPI()
# Access environment variables
database_url = os.getenv("DATABASE_URL", "sqlite:///./test.db") # default to SQLite
@app.get("/")
async def read_root():
return {"Database URL": database_url}
2. Build Your Dockerfile
In your Dockerfile, you can specify how to install dependencies and set up your FastAPI app. Here’s an example Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.9
# Set the working directory
WORKDIR /app
# Copy the requirement files, and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of your application code
COPY . .
# Expose the FastAPI app's port
EXPOSE 8000
# Command to run the FastAPI app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
3. Build the Podman Image
You can build your Podman image from the directory containing your Dockerfile using the following command:
podman build -t myfastapiapp .
4. Run the Podman Container with Environment Variables
When you run your Podman container, you can pass environment variables using the -e flag for each variable. Here’s how you can do that:
podman run -d -p 8000:8000 \
-e DATABASE_URL="postgresql://user:password@localhost/dbname" \
myfastapiapp
5. Optional: Using a .env File
If you prefer to store your environment variables in a .env file, you can use the python-dotenv library to load these variables. First, install python-dotenv:
pip install python-dotenv
Then, create a .env file:
DATABASE_URL=postgresql://user:password@localhost/dbname
Modify your main.py to load the .env file:
import os
from fastapi import FastAPI
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
app = FastAPI()
database_url = os.getenv("DATABASE_URL", "sqlite:///./test.db")
@app.get("/")
async def read_root():
return {"Database URL": database_url}
Just ensure that the .env file is copied into your Docker image during the build process. You can do this by adding a line in your Dockerfile:
COPY .env ./
Final Note
In summary, environment variables in a FastAPI application can be accessed through the os module or loaded from a .env file using python-dotenv. When deploying with Podman, you can specify environment variables at runtime using the -e flag or other methods depending on your deployment strategy.