Self-Hosting FastAPI APIs with Traefik and Docker: A Guide to Free HTTPS Hosting

Posted by

How To Self-Host FastAPI APIs with Traefik and Docker | Free HTTPS Self-Hosting

How To Self-Host FastAPI APIs with Traefik and Docker

Are you looking to self-host your FastAPI APIs with Traefik and Docker? In this article, we’ll walk you through the process of setting up your own self-hosted FastAPI APIs with free HTTPS support using Traefik and Docker. Let’s get started!

What You’ll Need

Before we begin, make sure you have the following tools installed on your system:

  • Docker
  • Traefik
  • FastAPI

Setting Up Traefik

First, we need to set up Traefik as our reverse proxy and load balancer. To do this, create a traefik.toml configuration file with the following content:


[entryPoints]
[entryPoints.web]
address = ":80"

[entryPoints.web.http]
[entryPoints.web.http.redirections]
entryPoint = "web"
[entryPoints.web.http.redirections.entryPoint]
to = "websecure"

[entryPoints.websecure]
address = ":443"

[providers.docker]
exposedByDefault = false

Next, you’ll need to run Traefik as a Docker container using the following command:


docker run -d -v /var/run/docker.sock:/var/run/docker.sock -v $PWD/traefik.toml:/traefik.toml -p 80:80 -p 443:443 -l traefik.frontend.rule=Host:monitor.yourdomain.com -l traefik.port=8080 traefik:v2.2

Creating FastAPI APIs

Now that Traefik is set up, it’s time to create your FastAPI APIs. First, create a new directory for your FastAPI project and then create a new file called main.py with the following content:


from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"Hello": "World"}

Next, create a Dockerfile in the same directory with the following content:


FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8

COPY ./app /app

Finally, create a docker-compose.yaml file with the following content:


version: "3"
services:
app:
build: .
ports:
- "8000:80"
labels:
- "traefik.enable=true"
- "traefik.http.routers.app.rule=Host(`yourdomain.com`)"
- "traefik.http.routers.app.entrypoints=websecure"
- "traefik.http.services.app.loadbalancer.server.port=80"

Running Your FastAPI APIs

To run your FastAPI APIs with Traefik and Docker, use the following command in the directory where your docker-compose.yaml file is located:


docker-compose up -d

That’s it! You’ve now successfully self-hosted your FastAPI APIs with Traefik and Docker, and you also have free HTTPS support thanks to Traefik’s automated Let’s Encrypt integration. Happy coding!

0 0 votes
Article Rating
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@soujuniortech
10 months ago

thanks, bro!

@chrisellison3705
10 months ago

Am i being thick? It keeps asking me to sign up with a company email address to get a 14 day free trial after i click on the sign up button..

@user-vi9mf2vt5c
10 months ago

Keep it going! This is exactly what I was searching for.

@aaronvadomorales2294
10 months ago

This is great!!! Thank you.