Skip to content

Commit 07064ae

Browse files
authored
📝 Update deployment files and docs (#660)
1 parent 3de18e5 commit 07064ae

File tree

8 files changed

+269
-102
lines changed

8 files changed

+269
-102
lines changed

.env

-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ SENTRY_DSN=
3838
# Flower
3939
FLOWER_BASIC_AUTH=
4040

41-
# Traefik
42-
TRAEFIK_PUBLIC_NETWORK=traefik-public
43-
TRAEFIK_TAG=traefik
44-
TRAEFIK_PUBLIC_TAG=traefik-public
45-
4641
# Configure these with your own Docker registry images
4742
DOCKER_IMAGE_BACKEND=backend
4843
DOCKER_IMAGE_CELERYWORKER=celery

backend/app/core/config.py

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class Settings(BaseSettings):
1717
# 60 minutes * 24 hours * 8 days = 8 days
1818
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
1919
SERVER_HOST: AnyHttpUrl
20-
# BACKEND_CORS_ORIGINS is a JSON-formatted list of origins
21-
# e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \
22-
# "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]'
2320
BACKEND_CORS_ORIGINS: list[AnyHttpUrl] | str = []
2421

2522
@field_validator("BACKEND_CORS_ORIGINS", mode="before")

deployment.md

+110-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,124 @@
11
# FastAPI Project - Deployment
22

3-
You can deploy the using Docker Compose with a main Traefik proxy outside handling communication to the outside world and HTTPS certificates.
3+
You can deploy the project using Docker Compose in a remote server.
44

5-
And you can use CI (continuous integration) systems to do it automatically.
5+
It expects you to have a Traefik proxy handling communication to the outside world and HTTPS certificates.
6+
7+
And you can use CI (continuous integration) systems to deploy automatically.
68

79
But you have to configure a couple things first.
810

9-
## Traefik network
11+
## Preparation
12+
13+
* Have a remote server ready and available.
14+
* Configure the DNS records of your domain to point to the IP of the server you just created.
15+
* Install and configure [Docker](https://docs.docker.com/engine/install/).
16+
* Create a remote directory to store your code, for example:
17+
18+
```bash
19+
mkdir -p /root/code/fastapi-project/
20+
```
21+
22+
## Public Traefik
23+
24+
We need a Traefik proxy to handle incoming connections and HTTPS certificates.
25+
26+
### Traefik Docker Compose
27+
28+
Copy the Traefik Docker Compose file to your server, to your code directory. You could do it with `rsync`:
29+
30+
```bash
31+
rsync -a docker-compose.traefik.yml [email protected]:/root/code/fastapi-project/
32+
```
33+
34+
### Traefik Public Network
35+
36+
This Traefik will expect a Docker "public network" named `traefik-public` to communicate with your stack(s).
37+
38+
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.
1039

11-
This stack expects the public Traefik network to be named `traefik-public`.
40+
To create a Docker "public network" named `traefik-public` run:
1241

13-
If you need to use a different Traefik public network name, update it in the `docker-compose.yml` files, in the section:
42+
```bash
43+
docker network create traefik-public
44+
```
45+
46+
### Traefik Environment Variables
47+
48+
The Traefik Docker Compose file expects some environment variables to be set.
1449

15-
```YAML
16-
networks:
17-
traefik-public:
18-
external: true
50+
Create the environment variables for HTTP Basic Auth.
51+
52+
* Create the username, e.g.:
53+
54+
```bash
55+
export USERNAME=admin
56+
```
57+
58+
* Create an environment variable with the password, e.g.:
59+
60+
```bash
61+
export PASSWORD=changethis
62+
```
63+
64+
* Use openssl to generate the "hashed" version of the password and store it in an environment variable:
65+
66+
```bash
67+
export HASHED_PASSWORD=$(openssl passwd -apr1 $PASSWORD)
1968
```
2069

21-
Change `traefik-public` to the name of the used Traefik network. And then update it in the file `.env`:
70+
* Create an environment variable with the domain name, e.g.:
2271

2372
```bash
24-
TRAEFIK_PUBLIC_NETWORK=traefik-public
73+
export DOMAIN=fastapi-project.example.com
2574
```
75+
76+
* Create an environment variable with the email for Let's Encrypt, e.g.:
77+
78+
```bash
79+
80+
```
81+
82+
### Start the Traefik Docker Compose
83+
84+
Now with the environment variables set and the `docker-compose.traefik.yml` in place, you can start the Traefik Docker Compose:
85+
86+
```bash
87+
docker compose -f docker-compose.traefik.yml up -d
88+
```
89+
90+
## Deploy the FastAPI Project
91+
92+
Now that you have Traefik in place you can deploy your FastAPI project with Docker Compose.
93+
94+
You could configure the variables in the `.env` file to match your domain, or you could override them before running the `docker compose` command.
95+
96+
For example:
97+
98+
```bash
99+
export DOMAIN=fastapi-project.example.com
100+
```
101+
102+
And then deploy with Docker Compose:
103+
104+
```bash
105+
docker compose -f docker-compose.yml up -d
106+
```
107+
108+
For production you wouldn't want to have the overrides in `docker-compose.override.yml`, so you would need to explicitly specify the file to use, `docker-compose.yml`.
109+
110+
## URLs
111+
112+
Replace `fastapi-project.example.com` with your domain:
113+
114+
Frontend: https://fastapi-project.example.com
115+
116+
Backend API docs: https://fastapi-project.example.com/docs
117+
118+
Backend API base URL: https://fastapi-project.example.com/api/
119+
120+
PGAdmin: https://pgadmin.fastapi-project.example.com
121+
122+
Flower: https://flower.fastapi-project.example.com
123+
124+
Traefik UI: https://traefik.fastapi-project.example.com

docker-compose.override.yml

+21-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ version: "3.3"
22
services:
33

44
proxy:
5+
image: traefik:v2.3
6+
volumes:
7+
- /var/run/docker.sock:/var/run/docker.sock
58
ports:
69
- "80:80"
710
- "8090:8080"
@@ -10,10 +13,13 @@ services:
1013
# Enable Docker in Traefik, so that it reads labels from Docker services
1114
- --providers.docker
1215
# Add a constraint to only use services with the label for this stack
13-
# from the env var TRAEFIK_TAG
14-
- --providers.docker.constraints=Label(`traefik.constraint-label-stack`, `${TRAEFIK_TAG?Variable not set}`)
16+
- --providers.docker.constraints=Label(`traefik.constraint-label`, `traefik-public`)
1517
# Do not expose all Docker services, only the ones explicitly exposed
1618
- --providers.docker.exposedbydefault=false
19+
# Create an entrypoint "http" listening on port 80
20+
- --entrypoints.http.address=:80
21+
# Create an entrypoint "https" listening on port 443
22+
- --entrypoints.https.address=:443
1723
# Enable the access log, with HTTP requests
1824
- --accesslog
1925
# Enable the Traefik log, for configurations and errors
@@ -23,8 +29,12 @@ services:
2329
# Enable the Dashboard and API in insecure mode for local development
2430
- --api.insecure=true
2531
labels:
26-
- traefik.http.routers.${STACK_NAME?Variable not set}-traefik-public-http.rule=Host(`${DOMAIN?Variable not set}`)
27-
- traefik.http.services.${STACK_NAME?Variable not set}-traefik-public.loadbalancer.server.port=80
32+
# Enable Traefik for this service, to make it available in the public network
33+
- traefik.enable=true
34+
- traefik.constraint-label=traefik-public
35+
# Dummy https-redirect middleware that doesn't really redirect, only to
36+
# allow running it locally
37+
- traefik.http.middlewares.https-redirect.contenttype.autodetect=false
2838

2939
db:
3040
ports:
@@ -72,6 +82,13 @@ services:
7282
args:
7383
INSTALL_DEV: ${INSTALL_DEV-true}
7484

85+
frontend:
86+
build:
87+
context: ./frontend
88+
args:
89+
- VITE_API_URL=http://${DOMAIN?Variable not set}
90+
- NODE_ENV=development
91+
7592
networks:
7693
traefik-public:
7794
# For local dev, don't expect an external Traefik network

docker-compose.traefik.yml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
services:
2+
traefik:
3+
image: traefik:v2.3
4+
ports:
5+
# Listen on port 80, default for HTTP, necessary to redirect to HTTPS
6+
- 80:80
7+
# Listen on port 443, default for HTTPS
8+
- 443:443
9+
restart: always
10+
labels:
11+
# Enable Traefik for this service, to make it available in the public network
12+
- traefik.enable=true
13+
# Use the traefik-public network (declared below)
14+
- traefik.docker.network=traefik-public
15+
# Define the port inside of the Docker service to use
16+
- traefik.http.services.traefik-dashboard.loadbalancer.server.port=8080
17+
# Make Traefik use this domain (from an environment variable) in HTTP
18+
- traefik.http.routers.traefik-dashboard-http.entrypoints=http
19+
- traefik.http.routers.traefik-dashboard-http.rule=Host(`traefik.${DOMAIN?Variable not set}`)
20+
# traefik-https the actual router using HTTPS
21+
- traefik.http.routers.traefik-dashboard-https.entrypoints=https
22+
- traefik.http.routers.traefik-dashboard-https.rule=Host(`traefik.${DOMAIN?Variable not set}`)
23+
- traefik.http.routers.traefik-dashboard-https.tls=true
24+
# Use the "le" (Let's Encrypt) resolver created below
25+
- traefik.http.routers.traefik-dashboard-https.tls.certresolver=le
26+
# Use the special Traefik service api@internal with the web UI/Dashboard
27+
- traefik.http.routers.traefik-dashboard-https.service=api@internal
28+
# https-redirect middleware to redirect HTTP to HTTPS
29+
- traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
30+
- traefik.http.middlewares.https-redirect.redirectscheme.permanent=true
31+
# traefik-http set up only to use the middleware to redirect to https
32+
- traefik.http.routers.traefik-dashboard-http.middlewares=https-redirect
33+
# admin-auth middleware with HTTP Basic auth
34+
# Using the environment variables USERNAME and HASHED_PASSWORD
35+
- traefik.http.middlewares.admin-auth.basicauth.users=${USERNAME?Variable not set}:${HASHED_PASSWORD?Variable not set}
36+
# Enable HTTP Basic auth, using the middleware created above
37+
- traefik.http.routers.traefik-dashboard-https.middlewares=admin-auth
38+
volumes:
39+
# Add Docker as a mounted volume, so that Traefik can read the labels of other services
40+
- /var/run/docker.sock:/var/run/docker.sock:ro
41+
# Mount the volume to store the certificates
42+
- traefik-public-certificates:/certificates
43+
command:
44+
# Enable Docker in Traefik, so that it reads labels from Docker services
45+
- --providers.docker
46+
# Do not expose all Docker services, only the ones explicitly exposed
47+
- --providers.docker.exposedbydefault=false
48+
# Create an entrypoint "http" listening on port 80
49+
- --entrypoints.http.address=:80
50+
# Create an entrypoint "https" listening on port 443
51+
- --entrypoints.https.address=:443
52+
# Create the certificate resolver "le" for Let's Encrypt, uses the environment variable EMAIL
53+
- --certificatesresolvers.le.acme.email=${EMAIL?Variable not set}
54+
# Store the Let's Encrypt certificates in the mounted volume
55+
- --certificatesresolvers.le.acme.storage=/certificates/acme.json
56+
# Use the TLS Challenge for Let's Encrypt
57+
- --certificatesresolvers.le.acme.tlschallenge=true
58+
# Enable the access log, with HTTP requests
59+
- --accesslog
60+
# Enable the Traefik log, for configurations and errors
61+
- --log
62+
# Enable the Dashboard and API
63+
- --api
64+
networks:
65+
# Use the public network created to be shared between Traefik and
66+
# any other service that needs to be publicly available with HTTPS
67+
- traefik-public
68+
69+
volumes:
70+
# Create a volume to store the certificates, even if the container is recreated
71+
traefik-public-certificates:
72+
73+
networks:
74+
# Use the previously created public network "traefik-public", shared with other
75+
# services that need to be publicly available via this Traefik
76+
traefik-public:
77+
external: true

0 commit comments

Comments
 (0)