|
1 | 1 | # FastAPI Project - Deployment
|
2 | 2 |
|
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. |
4 | 4 |
|
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. |
6 | 8 |
|
7 | 9 | But you have to configure a couple things first.
|
8 | 10 |
|
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. |
10 | 39 |
|
11 |
| -This stack expects the public Traefik network to be named `traefik-public`. |
| 40 | +To create a Docker "public network" named `traefik-public` run: |
12 | 41 |
|
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. |
14 | 49 |
|
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) |
19 | 68 | ```
|
20 | 69 |
|
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.: |
22 | 71 |
|
23 | 72 | ```bash
|
24 |
| -TRAEFIK_PUBLIC_NETWORK=traefik-public |
| 73 | +export DOMAIN=fastapi-project.example.com |
25 | 74 | ```
|
| 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 |
0 commit comments