mirror of
https://github.com/fastapi/full-stack-fastapi-template.git
synced 2026-11-03 12:29:42 +00:00
♻️ Simplify Traefik deployment, make it part of the stack (#2412)
This commit is contained in:
committed by
GitHub
parent
75c388ba1c
commit
0137c3f2c3
+9
-107
@@ -2,7 +2,7 @@
|
||||
|
||||
You can deploy the project using Docker Compose to a remote server.
|
||||
|
||||
This project expects you to have a Traefik proxy handling communication to the outside world and HTTPS certificates.
|
||||
The deployment Docker Compose configuration includes Traefik to handle HTTPS and route incoming traffic to the application.
|
||||
|
||||
You can use CI/CD (continuous integration and continuous deployment) systems to deploy automatically, there are already configurations to do it with GitHub Actions.
|
||||
|
||||
@@ -10,101 +10,13 @@ But you have to configure a couple things first. 🤓
|
||||
|
||||
## Preparation
|
||||
|
||||
* Have a remote server ready and available.
|
||||
* Configure DNS records pointing to the server for the application domain and any supporting service subdomains you want to expose, e.g. `fastapi-project.example.com`, `traefik.fastapi-project.example.com`, and `adminer.fastapi-project.example.com`. Do the same for staging domains such as `staging.fastapi-project.example.com`.
|
||||
* Have a remote server ready and available. Use a separate server for each environment, for example one for staging and one for production.
|
||||
* Configure DNS records pointing to the server for the application domain and any supporting service subdomains you want to expose, e.g. `fastapi-project.example.com` and `adminer.fastapi-project.example.com`.
|
||||
* Install and configure [Docker](https://docs.docker.com/engine/install/) on the remote server (Docker Engine, not Docker Desktop).
|
||||
|
||||
## Public Traefik
|
||||
|
||||
We need a Traefik proxy to handle incoming connections and HTTPS certificates.
|
||||
|
||||
You need to do these next steps only once.
|
||||
|
||||
### Traefik Docker Compose
|
||||
|
||||
* Create a remote directory to store your Traefik Docker Compose file:
|
||||
|
||||
```bash
|
||||
mkdir -p /root/code/traefik-public/
|
||||
```
|
||||
|
||||
Copy the Traefik Docker Compose file to your server. You could do it by running the command `rsync` in your local terminal:
|
||||
|
||||
```bash
|
||||
rsync -a compose.traefik.yml root@your-server.example.com:/root/code/traefik-public/
|
||||
```
|
||||
|
||||
### Traefik Public Network
|
||||
|
||||
This Traefik will expect a Docker "public network" named `traefik-public` to communicate with your stack(s).
|
||||
|
||||
This way, there will be a single public Traefik proxy that handles the communication (HTTP and HTTPS) with the outside world, and then behind that, you could have one or more stacks with different domains, even if they are on the same single server.
|
||||
|
||||
To create a Docker "public network" named `traefik-public` run the following command in your remote server:
|
||||
|
||||
```bash
|
||||
docker network create traefik-public
|
||||
```
|
||||
|
||||
### Traefik Environment Variables
|
||||
|
||||
The Traefik Docker Compose file expects some environment variables to be set in your terminal before starting it. You can do it by running the following commands in your remote server.
|
||||
|
||||
* Create the username for HTTP Basic Auth, e.g.:
|
||||
|
||||
```bash
|
||||
export USERNAME=admin
|
||||
```
|
||||
|
||||
* Create an environment variable with the password for HTTP Basic Auth, e.g.:
|
||||
|
||||
```bash
|
||||
export PASSWORD=changethis
|
||||
```
|
||||
|
||||
* Use openssl to generate the "hashed" version of the password for HTTP Basic Auth and store it in an environment variable:
|
||||
|
||||
```bash
|
||||
export HASHED_PASSWORD=$(openssl passwd -apr1 $PASSWORD)
|
||||
```
|
||||
|
||||
To verify that the hashed password is correct, you can print it:
|
||||
|
||||
```bash
|
||||
echo $HASHED_PASSWORD
|
||||
```
|
||||
|
||||
* Create an environment variable with the domain name for your server, e.g.:
|
||||
|
||||
```bash
|
||||
export DOMAIN=fastapi-project.example.com
|
||||
```
|
||||
|
||||
* Create an environment variable with the email for Let's Encrypt, e.g.:
|
||||
|
||||
```bash
|
||||
export EMAIL=admin@example.com
|
||||
```
|
||||
|
||||
**Note**: you need to set a different email, an email `@example.com` won't work.
|
||||
|
||||
### Start the Traefik Docker Compose
|
||||
|
||||
Go to the directory where you copied the Traefik Docker Compose file in your remote server:
|
||||
|
||||
```bash
|
||||
cd /root/code/traefik-public/
|
||||
```
|
||||
|
||||
Now with the environment variables set and the `compose.traefik.yml` in place, you can start the Traefik Docker Compose running the following command:
|
||||
|
||||
```bash
|
||||
docker compose -f compose.traefik.yml up -d
|
||||
```
|
||||
|
||||
## Deploy the FastAPI Project
|
||||
|
||||
Now that you have Traefik in place you can deploy your FastAPI project with Docker Compose.
|
||||
You can deploy your FastAPI project with Docker Compose.
|
||||
|
||||
**Note**: You might want to jump ahead to the section about Continuous Deployment with GitHub Actions.
|
||||
|
||||
@@ -175,7 +87,6 @@ export FRONTEND_HOST="https://${DOMAIN?Variable not set}"
|
||||
You can set several other environment variables:
|
||||
|
||||
* `PROJECT_NAME`: The name of the project, used in the API for the docs and emails.
|
||||
* `STACK_NAME`: The name of the stack used for Docker Compose labels and project name, this should be different for `staging`, `production`, etc. You could use the same domain replacing dots with dashes, e.g. `fastapi-project-example-com` and `staging-fastapi-project-example-com`.
|
||||
* `BACKEND_CORS_ORIGINS`: A list of additional allowed CORS origins separated by commas. The frontend served by FastAPI uses the same origin and doesn't need to be added.
|
||||
* `FIRST_SUPERUSER`: The email of the first superuser, this superuser will be the one that can create new users.
|
||||
* `SMTP_HOST`: The SMTP server host to send emails, this would come from your email provider (E.g. Mailgun, Sparkpost, Sendgrid, etc).
|
||||
@@ -201,19 +112,17 @@ With the environment variables in place, you can deploy with Docker Compose:
|
||||
|
||||
```bash
|
||||
cd /root/code/app/
|
||||
docker compose -f compose.yml build
|
||||
docker compose -f compose.yml up -d
|
||||
docker compose -f compose.yml -f compose.deploy.yml build
|
||||
docker compose -f compose.yml -f compose.deploy.yml up -d
|
||||
```
|
||||
|
||||
For production you wouldn't want to have the overrides in `compose.override.yml`, that's why we explicitly specify `compose.yml` as the file to use.
|
||||
The `compose.deploy.yml` file adds the deployment settings to the shared configuration in `compose.yml`, including HTTPS and automatic certificate handling. Explicitly listing these files also excludes the local development settings in `compose.override.yml`.
|
||||
|
||||
## Continuous Deployment (CD)
|
||||
|
||||
You can use GitHub Actions to deploy your project automatically. 😎
|
||||
|
||||
You can have multiple environment deployments.
|
||||
|
||||
There are already two environments configured, `staging` and `production`. 🚀
|
||||
There are already two environment deployments configured, `staging` and `production`. Each environment should be deployed to a separate server. 🚀
|
||||
|
||||
### Install GitHub Actions Runner
|
||||
|
||||
@@ -299,10 +208,7 @@ For each GitHub Environment (`staging` and `production`), configure the required
|
||||
|
||||
The current Github Actions workflows expect these secrets:
|
||||
|
||||
* `DOMAIN_PRODUCTION`
|
||||
* `DOMAIN_STAGING`
|
||||
* `STACK_NAME_PRODUCTION`
|
||||
* `STACK_NAME_STAGING`
|
||||
* `DOMAIN`
|
||||
* `EMAILS_FROM_EMAIL`
|
||||
* `FIRST_SUPERUSER`
|
||||
* `FIRST_SUPERUSER_PASSWORD`
|
||||
@@ -326,10 +232,6 @@ If you need to add extra environments you could use those as a starting point.
|
||||
|
||||
Replace `fastapi-project.example.com` with your domain.
|
||||
|
||||
### Main Traefik Dashboard
|
||||
|
||||
Traefik UI: `https://traefik.fastapi-project.example.com`
|
||||
|
||||
### Production
|
||||
|
||||
Application (frontend and API): `https://fastapi-project.example.com`
|
||||
|
||||
Reference in New Issue
Block a user