Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Docker Demo

A minimal example showing how to run a FastAPI application inside a Docker container. This project demonstrates containerizing a simple API with a Dockerfile and running it using uvicorn.

Project Structure

fastapi-docker-demo/
├── app
│   └── main.py
├── Dockerfile
└── requirements.txt

Application Overview

The API exposes two endpoints:

Method Endpoint Description
GET / Returns a simple message and current timestamp
POST /items Accepts an item payload and returns it with a processing timestamp

Data Model

The API uses a Pydantic model to validate request data.

class Item(BaseModel):
    name: str
    price: float
    is_available: bool = True

Example request body:

{
  "name": "Laptop",
  "price": 1200.50,
  "is_available": true
}

Requirements

  • Docker installed
  • Internet connection to pull the Python base image

Dependencies defined in requirements.txt:

fastapi
uvicorn[standard]

Build Docker Image

From the project root directory:

docker build -t fastapi-docker-demo .

Run the Container

docker run -p 8000:8000 fastapi-docker-demo

The API will now be available at:

http://localhost:8000

Test the API

Root Endpoint

curl http://localhost:8000

Example response:

{
  "message": "FastAPI running inside Docker 🚀",
  "timestamp": "2026-03-12T10:00:00"
}

Create Item

curl -X POST http://localhost:8000/items \
-H "Content-Type: application/json" \
-d '{"name":"Phone","price":699.99}'

Example response:

{
  "received_item": {
    "name": "Phone",
    "price": 699.99,
    "is_available": true
  },
  "processed_at": "2026-03-12T10:02:00"
}

Interactive API Docs

FastAPI automatically generates API documentation.

Interface URL
Swagger UI http://localhost:8000/docs
ReDoc http://localhost:8000/redoc

Dockerfile Explanation

Key steps in the Dockerfile:

  1. Use a lightweight Python base image.
  2. Set a working directory inside the container.
  3. Install dependencies from requirements.txt.
  4. Copy the application source code.
  5. Expose port 8000.
  6. Start the API server with uvicorn.

Run Without Docker (Optional)

For local development:

pip install -r requirements.txt
uvicorn app.main:app --reload

Then open:

http://localhost:8000/docs

License

This project is for demonstration and learning purposes.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages