day-18

FastAPI Docker Infrastructure Demo

This repository demonstrates a production-ready Docker setup for a FastAPI application integrated with PostgreSQL and Redis.

Features

Prerequisites

Getting Started

1. Build and Run

To start the entire application stack, run:

docker compose up -d

2. Verify Services

Check the status of the containers:

docker compose ps

3. Test API Endpoints

4. Stop and Cleanup

To stop the services:

docker compose stop

To remove the containers and networks (retaining data):

docker compose down

To remove the containers, networks, AND database volumes:

docker compose down -v

Docker Concepts Learned

  1. Images vs Containers: Used python:3.11-slim and postgres:15-alpine as base images.
  2. Dockerfile: Defined multi-stage builds and non-root users.
  3. Volumes: Configured postgres_data for database persistence.
  4. Networks: Used a bridge network named backend for service communication.
  5. Healthchecks: Ensured dependencies (PostgreSQL/Redis) are ready before the App starts.

Verification Results Automated Tests I verified the application’s functionality using curl:

FastAPI Root: Successfully returned {“message”:”FastAPI is running in Docker!”}. Redis Integration: Incremented the counter via GET /hits. PostgreSQL Integration: Successfully created and listed items via POST /items/ and GET /items/. TIP

You can visit http://localhost:8000/docs in your browser to interact with the API via Swagger UI!