DEV Community

Sumeet Dugg
Sumeet Dugg

Posted on

How I Solved a Deployment Nightmare Using Docker and FastAPI

The Problem I Faced
I was working at a Python development company where we had to deploy applications on every client machine. Each time a new feature was added or a bug was fixed, we had to manually install or update the application on multiple systems.

This process was:

  • Time-consuming
  • Error-prone
  • Difficult to maintain

Every deployment felt like repeating the same exhausting cycle.

The Idea That Changed Everything
One day, I suggested a simple but powerful idea to my team:

“What if we install Docker on a central server and allow all clients to access the application from there?”

Instead of installing applications on every machine, we could:

  • Run the application once on a server

  • Allow clients to access it via a browser using an IP address

Example:

http://<server-ip>:8000
Enter fullscreen mode Exit fullscreen mode

The Result
This approach completely transformed our workflow:

  • No need to install software on every client machine
  • Centralized updates (update once, reflect everywhere)
  • Easy access through browsers
  • Clean and scalable architecture

Now, every client had separate access to the application just by visiting the server address.

Why Docker Made This Possible
Before Docker:

  • Applications were installed individually
  • Dependencies conflicted
  • Environment issues were common

After Docker:

  • Everything runs inside containers
  • Applications become portable
  • Same setup works anywhere in the world

What is FastAPI (Quick Overview)
FastAPI is a Python-based framework built on top of Starlette. It leverages the Asynchronous Server Gateway Interface (ASGI) technology to perform asynchronous tasks efficiently. FastAPI also validates data using Pydantic, ensuring that incoming requests meet the required schema, as illustrated in the diagram below.

FastAPI runs on the Uvicorn server, which is an asynchronous web server that uses the ASGI standard to run Starlette applications. Uvicorn acts as a handler — it receives HTTP requests and forwards them to Starlette for processing. FastAPI further enhances Starlette’s routing system by adding built-in data validation and automatic API documentation features.

“When I first started learning FastAPI, it was a bit challenging for me to understand its functionalities, especially since I came from a Java programming background. However, as I spent more time working with it, writing code in FastAPI gradually became easier and more intuitive.
What I found most impressive was its asynchronous nature, which allows it to handle multiple HTTP requests at the same time efficiently. It also performs automatic data validation — if the incoming data is invalid, it immediately raises clear exceptions, which makes debugging much simpler.
Another feature that stood out to me was how easy FastAPI makes it to explore and test application endpoints. It comes with a built-in Swagger UI, where you can view and interact with all your APIs. For example, by visiting http: localhost:8000/docs, you can see a complete list of endpoints available in your application and test them directly from the browser.”

Building a Simple FastAPI Application
Let’s create a basic CRUD API.

  1. Create main.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List

app = FastAPI()

# -----------------------------
# Data Model
# -----------------------------
class Task(BaseModel):
    id: int
    title: str
    completed: bool = False

# Fake in-memory database
tasks: List[Task] = []

# -----------------------------
# CREATE (POST)
# -----------------------------
@app.post("/tasks", response_model=Task)
def create_task(task: Task):
    for t in tasks:
        if t.id == task.id:
            raise HTTPException(status_code=400, detail="Task already exists")
    tasks.append(task)
    return task

# -----------------------------
# READ (GET ALL)
# -----------------------------
@app.get("/tasks", response_model=List[Task])
def get_tasks():
    return tasks

# -----------------------------
# READ (GET ONE)
# -----------------------------
@app.get("/tasks/{task_id}", response_model=Task)
def get_task(task_id: int):
    for task in tasks:
        if task.id == task_id:
            return task
    raise HTTPException(status_code=404, detail="Task not found")

# -----------------------------
# UPDATE (PUT)
# -----------------------------
@app.put("/tasks/{task_id}", response_model=Task)
def update_task(task_id: int, updated_task: Task):
    for index, task in enumerate(tasks):
        if task.id == task_id:
            tasks[index] = updated_task
            return updated_task
    raise HTTPException(status_code=404, detail="Task not found")

# -----------------------------
# DELETE
# -----------------------------
@app.delete("/tasks/{task_id}")
def delete_task(task_id: int):
    for index, task in enumerate(tasks):
        if task.id == task_id:
            tasks.pop(index)
            return {"message": "Task deleted"}
    raise HTTPException(status_code=404, detail="Task not found")
Enter fullscreen mode Exit fullscreen mode

Understanding Docker
Consider Docker as a warehouse robot. Before Docker, workers (like traditional “dockers”) had to carry and manage each package separately, which was time-consuming and inefficient. After the introduction of Docker, every package is packed into standardized containers, making transportation and management much easier and reducing manual effort.

Docker is based on two key concepts: images and containers. To understand this, think of an image as a blueprint of a house, and a container as the actual house built from that blueprint. You can create as many containers (houses) as you want from a single image (blueprint), ensuring consistency across all environments.

Now, let’s create the project file structure.

_

Note: The .gitignore and README.md files are not essential for this setup, so you can skip creating them.
_

How I Added a FastAPI Application to Docker
If your system does not have Docker pre-installed, you can install it from the official documentation here:
https://docs.docker.com/desktop/setup/install/windows-install/

For now, I am installing Docker on my local system. However, you can follow the same procedure to install it on a central server as well.

Creating a Dockerfile

  1. Create a file named Dockerfile or dockerFile(no extension):
# 1. Base image (Python)
FROM python:3.10-slim

# 2. Set working directory
WORKDIR /app

# 3. Copy requirements file
COPY requirements.txt .

# 4. Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# 5. Copy project files
COPY . .

# 6. Expose port
EXPOSE 8000

# 7. Run FastAPI app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Enter fullscreen mode Exit fullscreen mode
  1. Requirements File Create requirements.txt:
fastapi
uvicorn
Enter fullscreen mode Exit fullscreen mode

Build and Run the Docker Container
Step 1: Build Image

docker build -t fastapi-app .
Enter fullscreen mode Exit fullscreen mode

Now, open the Docker Desktop UI. You will see the image you created listed in the “Images” section, as shown below.

Step 2: Run Container

docker run -d -p 8000:8000 fastapi-app
Enter fullscreen mode Exit fullscreen mode

Open your browser and go to http://localhost:8000/docs/ . You will see the output as shown below:

Final Thoughts
What started as a small idea turned into a major improvement for our team.

Key Takeaways:

  • Docker eliminates repetitive installations
  • FastAPI makes backend development fast and efficient
  • Centralized deployment saves time and effort
  • Clients can access applications easily via browser

Closing Note
Sometimes, the best solutions are not complex — they just require a shift in thinking.

Instead of asking:

“How do we install this everywhere?”

Ask:

“How can we run this once and access it everywhere?”

That question changed everything for me — and it might do the same for you.

Top comments (0)