Deploy series: in this first episode, we going to cover a basic deploy of docker containers using capRover, DigitalOcean Droplet (VPS) and cloudflare for our DNS.
Often when developing apis with fastapi, or many other frameworks, we need to deploy them. But this often comes with many difficulties, such as providing the correct environment for the project or even needing access to the VPS to edit the content.
To standardize this, more than just use Docker, we can utilize tools to manage our container environment; in this case, CapRover. So let’s dive into it.
Table of contents
Open Table of contents
requirements
- For this post, I assume that you already have a VPS instance ON, in this blog we going to use the digitalOcean droplets. You can feel free to test this tutorial in any online VPS provider, for the cheapest one, I recommend Contabo or Hetzner.
- We going to need a domain, you can buy a very cheap in porkbun. And for DNS management, cloudflare.
- Also, we going to use github (repo link) for code versioning and docker for container build, so, I assume that you already have some knowledge on container/docker side.
Deploy Context
Today, we can resume most of steps to run a application in basically:
- A dns service for our domain, so the client send a request to domain, the request them is processed by a DNS service (Like cloudflare) and later redirect to server
- The server receives the request from cloudflare, and them, process the request and return the request response
- The response come back all the way out to client
So, as this context, we going to replicate this.
Spin up Droplet (DigitalOcean VPS) with docker
- Create an account (you can create in this referral link).
- setup a droplet (choose the custom docker image for VPS OS), use this documentation for it.
- Copy the public IP of the droplet to later process.
Domain and DNS
It is necessary to have a domain (site name) to associate our application in a basic setup, you can choose a domain marketplace and DNS manager preferably, but in this case, we use cloudflare DNS and porkbun, as it offers both tools in one.
- Create a cloudflare account, and them transfer your domain’s DNS management to it.
- Create the DNS record to proxy the requests to your server (please use a *.domain, to allow capRover to create multiple deploys), and do not mark proxy option (we are going to use CapRover certificates), check steps documentation.
install Dependencies
- update the OS and open the ports
# update
sudo apt update && sudo apt upgrade
# open the ports
ufw allow 80,443,3000,996,7946,4789,2377/tcp; ufw allow 7946,4789,2377/udp;
- Run capRover container (can take some minutes)
docker run -d -p 80:80 -p 443:443 -p 3000:3000 -v /var/run/docker.sock:/var/run/docker.sock -v /captain:/captain -e ACCEPTED_TERMS=true caprover/caprover
- Install nodejs and capRover CLI
# install nodejs
sudo apt install nodejs npm
# install caprover CLI
npm install -g caprover
Config and deploy the app
Config capRover server
- to start the capRover config, type:
caprover serversetup
-
Type “Y”:
-
Type your droplet Public IP:
-
Type the DNS record that you created in cloudflare (without the ”*.”):
-
Create a new password for capRover server:
-
Type a valid email for free TLS certificate, to late enable HTTPS:
-
and finally, type the name for your caprover server (you can simply type capRover):
-
Caprover will spin up the management server, and generate a public link for access:
Create a new app
After getting the public management server URL, fallow these Instructions to create a simple app that we going to user in future container deployment.
Caprover manages to hold many apps, so, you can deploy as many containers as you want. We have this app concept, an abstraction of the deployment environment, where you create a new container, defines the port where its running, enable https, websockets and manage de reverse proxy config, for this tutorial, we going to use a very simple app config.
Clone the repo and Deploy the container
Finally, after all this steps we will deploy our container, so, basically, we will follow this simples instructions
Notice, In this deployment, we’re using this python web-api, where we explore a lot of concepts such as asyncio and concurrent tasks (coroutines). You can check a very good content about in this post
- Clone the repo inside the VPS
# CLONE the repo
https://github.com/gabszs/FastApi--Password-Generator.git
# cd into the repo dir
cd FastApi--Password-Generator
- After cd into directory, type:
caprover deploy
-
so, select you caprover instance where the container will be deployed (since we only configured one, you can check the only option)
-
Select an app to host the container, since we created a one called password, lets going to select the password
-
Caprover will ask you about which branch to watch, select the default one, also, it will alert you that any files marked in gitignore will not be pulled into the server, than, type “Y”
-
Caprover will build the container, and them, deploy it into the your domain, in the end, it will display the final URL, where you can check your deployment
Conclusion
Congratulations, your container is deployed, and also, your server, so you can also deploy as many containers as you want.
For this Api, you can check the final result in this link
Later, I’ll post more content about container and apps deploy, tackling content and technologies such as portainer, kubernetes, rancher, argoCD (gitops), so stay in track to new posts.
And if you like the post, had any feedback or question, you can send me a message on whatsapp or email.