[Photo by vivek kumar on Unsplash, modified(cropped)]
I started surveying the Dev DigitalOcean Hackathon with an article on deploying a Rust application.
However, since Rust is a much technical language, and it is not so far supported by DigitalOcean App Platform, I decided to go the Python way for this hackathon.
Grab your laptop and a beer (or some spiked eggnog: it's still Christmas season after all!).
NOTE: read the official hackathon page. If you are following this tutorial to prepare for your own hackathon, there is a link to a form to fill in order to get 50$ for 60 days, good to test the environment and to participate to the hackathon without weighing too much on the wallet. Merry Christmas and a happy new year to us!
First Build
I will give for granted that you are using a POSIX like shell (mostly Bash, but I use ZSH).
So the following is just routine:
mkdir geobot
cd geobot
In the above you can change geobot to give your project a different name.
Now we can create a virtual environment for our app:
python3 -m venv venv
Since we need to push our app to GitHub in order to trigger the DigitalOcean builds, we need a .gitignore
.
We could create our own, but I advise to use a ready-made one, from the official GitHub repo:
curl -L -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
Now we can make our project's folder into a git repository:
git init
Time to activate our virtual environment:
source venv/bin/activate
(Remember: when you have finished, in order to deactivate the environment, just give the command deactivate
straight to the shell).
Now we need to install our first dependencies.
We will use Flask as API Server Framework, and Gunicorn as WSGI HTTP server (think of it as an Apache for the Python frameworks).
pip install Flask gunicorn
We can also create a requirements.txt now, and in case of need, update it later on:
pip freeze > requirements.txt
Now let's create a minimalist Flask app, called app.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello world!'
We need to create a configuration file for Gunicorn, called gunicorn_config.py:
bind = "0.0.0.0:8080"
workers = 2
That will bind your server to the port 8080
of the localhost, from which DigitalOcean Apps will fetch it.
First Deploy
Now it's time to push to GitHub and deploy on DO App Platform.
Let's prepare our local repo:
git add -A
git commit -m 'First Commit'
Now you can create a new empty repo on GitHub, that we can enable as remote on the local one, and push our first commit to:
git remote add origin https://github.com/<yourname>/geobot.git
git push origin master
NOTE: very soon GitHub will change the defaults, and replace master
with main
. There will be a transparent method provided by GitHub to do so, therefore we will patiently wait the tools are in place (the official guides say to wait still). Otherwise branch now to main
(git branch -M main
) and push to main
instead than to master
.
Time for the DigitalOcean part.
Let's point our browser to the DO App Platform Homepage.
Click Launch Your App
: if it's the first time it will ask you to connect your GitHub account. Just follow the procedure.
At the end you will be prompted to choose a repository
Click on Next
and in page 2 you will be prompted to pick a name and a region of deploy
Click on Next
and in page 3 it should have detected everything for you (that is, Python and even Gunicorn!). In case it has not, please refer to the following image for the right settings:
Everything is OK, but the Run Command
: click Edit
in there and add app:app
at the end.
It should be:
gunicorn --worker-tmp-dir /dev/shm app:app
Now you can save and resume the wizard.
Upon clicking Next
we will be given the opportunity to choose our Monthly plan and the size of our virtual server (available maximum resources).
Notice that the free plan is not available for web services, but only to host static resources.
Notice too in the bottom left of the above picture: DO defaults to the small S
(10$ per month) server; however, there is an option for an XS
(extra small) server (5$/Mo.); you can opt for that, at least when you are not deploying a business app, but just testing the environment.
Upon clicking on Launch Basic App
you will be redirected to the page of your app, and you should see it loading the deploy
If you click on view Logs
(on the top right), you can take a look to see if the deploy got stuck somewhere.
Once everything is done, you should see a Deployed successfully!
message, and a link to the app under the app name:
If you follow the link, you should see our Flask app greeting us from the browser window:
Always an emotion!
Playing with your First App
You can go ahead and change the greeting message in app.py, for example:
@app.route('/')
def hello_world():
return 'Hello from DO App Platform!'
Then, if you commit to GitHub
git add -A
git commit -m 'Showing Personalized Greeting Message'
git push origin master
you will see App Platform "instantly" rebuild the app for us:
At the end you will see the updated message:
Notice that while it is still building, DigitalOcean will keep the old version running, and when the deploy is completed it will substitute the new version for the old.
Quite neat!
Conclusion
For today it's all folks!
If you are completely new to this world, I hope you could follow the tutorial without problems.
Do not hesitate to write in the comments section.
As usual: stay excited, because something good's coming at the horizon!
Till next time!
Top comments (0)