In the last article we talked about making our pydantic documentation as robust as possible. In this part, we’ll focus on containerizing the Weather Forecast API using Docker. By the end of this tutorial, your API will be running inside a Docker container, alongside a PostgreSQL database, all orchestrated using Docker Compose.
Why Dockerize? 🛠️
Docker simplifies application deployment by packaging your application and its dependencies into a portable container. With Docker:
- You can ensure consistency across development, staging, and production environments.
- Easily scale and deploy your application.
- Run multiple services, such as your FastAPI app and PostgreSQL database, together in an isolated environment.
Goals for Part 4 🎯
- Create a
Dockerfile
to containerize the FastAPI app. - Add a
docker-compose.yml
file to orchestrate the API and PostgreSQL. - Set up a
.env
file to manage environment variables. - Run the app and database together using Docker Compose.
Here’s my requirements.txt file:
|
|
Step 1: Create the Dockerfile
The Dockerfile
defines the steps to build a Docker image for the FastAPI app.
Sample Dockerfile
|
|
Step 2: Configure Docker Compose
Docker Compose helps manage multi-container applications, like our API and database, using a single YAML file.
Sample docker-compose.yml
|
|
Step 3: Add Environment Variables
Environment variables are managed using a .env
file to keep sensitive data out of the codebase.
Sample .env
|
|
Step 4: Build and Run the Containers
Build the Docker Images
Run the following command to build the images defined in docker-compose.yml
:
|
|
Run the Containers
Start the containers with:
|
|
Your FastAPI app will now be accessible at http://localhost:8000
.
Step 5: Database Initialization
The PostgreSQL database will be initialized automatically. However, you need to create the schema if it doesn’t exist.
Auto-Running the Schema Script
To automate schema creation, include the schema initialization in the CMD
or ENTRYPOINT
of the Dockerfile
, or add a custom init.sql
script to the db
service.
Example: Adding an init.sql
Script
Create a file named
init.sql
:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
CREATE TABLE IF NOT EXISTS hourly_weather_forecast ( start_time TIMESTAMP PRIMARY KEY, id SERIAL, row_start_datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP, row_end_datetime TIMESTAMP, is_current BOOLEAN DEFAULT TRUE, end_time TIMESTAMP NOT NULL, temperature INTEGER, temperature_unit VARCHAR(10), relative_humidity INTEGER, wind_speed VARCHAR(50), wind_direction VARCHAR(10), short_forecast VARCHAR(255), icon VARCHAR(255) );
Update the
docker-compose.yml
file:1 2 3 4 5
db: image: postgres:15 volumes: - postgres_data:/var/lib/postgresql/data - ./init.sql:/docker-entrypoint-initdb.d/init.sql
Step 6: Verify the Setup
Visit the API documentation: Open
http://localhost:8000/docs
in your browser to access the FastAPI auto-generated Swagger UI.Test the API: Use
curl
or a tool like Postman to test your endpoints.Inspect the Database: Connect to the database with:
1
docker exec -it <container_id> psql -U api -d weather