Skip to content

Commit d8542ac

Browse files
committed
Added article README
1 parent 2fa5ce3 commit d8542ac

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

README.md

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# Hellofly-python
2+
3+
In our Hands-On section, we show how to deploy a deployable image file using Flyctl. Now we are going to deploy an application from source. In this _Getting Started_ article, we look at how to deploy a Python application on Fly.
4+
5+
## _The Hellofly-python Application_
6+
7+
You can get the code for the example from [the hellofly Github repository](https://github.com/fly-examples/hellofly-python). Just `git clone https://github.com/fly-examples/hellofly-python` to get a local copy.
8+
9+
The hellofly-python application is, as you'd expect for an example, small. It's a Python application that uses the [Flask](https://flask.palletsprojects.com/) web framework. Here's all the code form `hellofly.py`:
10+
11+
```python
12+
from flask import Flask, render_template
13+
14+
app = Flask(__name__)
15+
16+
@app.route('/')
17+
@app.route('/<name>')
18+
def hello(name=None):
19+
return render_template('hello.html', name=name)
20+
```
21+
22+
Flask is set up to route request to a `hello` function which in turn passes a `name` value (taken from the requests path)to a function to render a template. The template resides in the `templates` directory under the name `hello.html`. It too is very simple too:
23+
24+
```html
25+
<!DOCTYPE html>
26+
<html lang="en">
27+
<head>
28+
</head>
29+
<body>
30+
<h1>Hello from Fly</h1>
31+
{% if name %}
32+
<h2>and hello to {{name}}</h2>
33+
{% endif %}
34+
</body>
35+
</html>
36+
```
37+
38+
We're using a template as it makes it easier to show what you should do with assets that aren't the actual application.
39+
40+
You will need to install Flask itself, or at least set up virtual environments as recommended in the [Flask Install](https://flask.palletsprojects.com/en/1.1.x/installation/#virtual-environments) guide.
41+
42+
Once you have activated the virtual environment, run:
43+
44+
```
45+
python -m pip install -r requirements.txt
46+
```
47+
48+
Which will load Flask and other required packages. One of those packages will be `gunicorn` which isn't a Flask dependency, but will be used when we deploy the app to Fly.
49+
50+
## _Testing the Application_
51+
52+
Flask apps are run with the `flask run` command, but before you do that, you need to set an environment variable `FLASK_APP` to say which app you want to run.
53+
54+
```cmd
55+
FLASK_APP=hellofly flask run
56+
```
57+
```out
58+
* Serving Flask app "hellofly"
59+
* Environment: production
60+
WARNING: This is a development server. Do not use it in a production deployment.
61+
Use a production WSGI server instead.
62+
* Debug mode: off
63+
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
64+
```
65+
66+
This will run our hellofly app and you should be able to connect to it locally on localhost:5000.
67+
68+
Now, let's move on to deploying this app to Fly.
69+
70+
## _Install Flyctl and Login_
71+
72+
We are ready to start working with Fly and that means we need `flyctl`, our CLI app for managing apps on Fly. If you've already installed it, carry on. If not, hop over to [our installation guide](/docs/getting-started/installing-flyctl/). Once that is installed you'll want to [login to Fly](/docs/getting-started/login-to-fly/).
73+
74+
## _Configure the App for Fly_
75+
76+
Each Fly application needs a `fly.toml` file to tell the system how we'd like to deploy it. That file can be automatically generated with the command `flyctl init` command. We are going to use one of Fly's builtin deployment configurations for python
77+
78+
```cmd
79+
flyctl init
80+
```
81+
```output
82+
? App Name (leave blank to use an auto-generated name) hellofly-python
83+
84+
? Select organization: demo (demo)
85+
86+
? Select builder: python
87+
Python builtin
88+
Builtins use port 8080
89+
New app created
90+
Name = hellofly-python
91+
Organization = personal
92+
Version = 0
93+
Status =
94+
Hostname = <empty>
95+
96+
App will initially deploy to lhr (London, United Kingdom) region
97+
98+
Wrote config file fly.toml
99+
```
100+
101+
You'll be asked for an application name first. We recommend that you go with the autogenerated names for apps to avoid namespace collisions. We're using `hellofly-python` here so you can easily spot it in configuration files.
102+
103+
Next you'll be prompted for an organization. Organizations are a way of sharing applications between Fly users. When you are asked to select an organization, there should be one with your account name; this is your personal organization. Select that.
104+
105+
Flyctl also asks you to select a builder. Builders are responsible for constructing the Docker image of your application which is then deployed to Fly's Firecracker VMs. The simplest to use are the builtin builders, which we recommend you use here. Select Python (Python Builtin). If you want to know more about the various builders, see [_Builders and Fly_](/docs/reference/builders/).
106+
107+
One thing to know about the builtin Python builder is that it will automatically copy over the contents of the directory to the deployable image. This is how you can move static assets such as templates and other files to your application. The other thing to know is that it uses a Procfile to run the application; Procfiles are used on other platforms to deploy Python applications so we keep it simple. The Procfile contains instructions for starting the application. Here's the contents of ours:
108+
109+
```Procfile
110+
web: gunicorn hellofly:app
111+
```
112+
113+
This says the web component of the application is served by `gunicorn` (which we mentioned earlier when talking about dependencies) and that should run the hellofly Flask app as we set up for Flask.
114+
115+
## _Inside `fly.toml`_
116+
117+
The `fly.toml` file now contains a default configuration for deploying your app. In the process of creating that file, `flyctl` has also created a Fly-side application slot of the same name, "hellofly". If we look at the `fly.toml` file we can see the name in there:
118+
119+
```toml
120+
# fly.toml file generated for hellofly-python on 2021-01-13T15:21:15Z
121+
122+
app = "hellofly-python"
123+
124+
[build]
125+
builtin = "python"
126+
127+
kill_signal = "SIGINT"
128+
kill_timeout = 5
129+
130+
[[services]]
131+
internal_port = 8080
132+
protocol = "tcp"
133+
134+
[services.concurrency]
135+
hard_limit = 25
136+
soft_limit = 20
137+
138+
[[services.ports]]
139+
handlers = ["http"]
140+
port = "80"
141+
142+
[[services.ports]]
143+
handlers = ["tls", "http"]
144+
port = "443"
145+
146+
[[services.tcp_checks]]
147+
interval = 10000
148+
timeout = 2000
149+
```
150+
151+
The `flyctl` command will always refer to this file in the current directory if it exists, specifically for the `app` name/value at the start. That name will be used to identify the application to the Fly service. The rest of the file contains settings to be applied to the application when it deploys.
152+
153+
We'll have more details about these properties as we progress, but for now, it's enough to say that they mostly configure which ports the application will be visible on.
154+
155+
## _Deploying to Fly_
156+
157+
We are now ready to deploy our app to the Fly platform. At the command line, just run:
158+
159+
```cmd
160+
flyctl deploy
161+
```
162+
163+
This will lookup our `fly.toml` file, and get the app name `hellofly-python` from there. Then `flyctl` will start the process of deploying our application to the Fly platform. Flyctl will return you to the command line when it's done.
164+
165+
## _Viewing the Deployed App_
166+
167+
Now the application has been deployed, let's find out more about its deployment. The command `flyctl status` will give you all the essential details.
168+
169+
```cmd
170+
flyctl status
171+
```
172+
```output
173+
App
174+
Name = hellofly-python
175+
Owner = demo
176+
Version = 0
177+
Status = running
178+
Hostname = hellofly-python.fly.dev
179+
180+
Deployment Status
181+
ID = 0cdc72fe-3db9-aa52-eb84-5c3552053b1e
182+
Version = v0
183+
Status = successful
184+
Description = Deployment completed successfully
185+
Instances = 1 desired, 1 placed, 1 healthy, 0 unhealthy
186+
187+
Instances
188+
ID VERSION REGION DESIRED STATUS HEALTH CHECKS RESTARTS CREATED
189+
0530d622 0 lhr run running 1 total, 1 passing 0 40s ago
190+
```
191+
192+
As you can see, the application has been with a DNS hostname of `hellofly-python.fly.dev`, and an instance is running in London. Your deployment's name will, of course, be different.
193+
194+
## _Connecting to the App_
195+
196+
The quickest way to connect to your deployed app is with the `flyctl open` command. This will open a browser on the HTTP version of the site. That will automatically be upgraded to an HTTPS secured connection (for the fly.dev domain).
197+
198+
199+
to connect to it securely. Add `/name` to `flyctl open` and it'll be appended to the URL as the path and you'll get an extra greeting from the hellofly-python application.
200+
201+
202+
## _Bonus Points_
203+
204+
If you want to know what IP addresses the app is using, try `flyctl ips list`:
205+
206+
```cmd
207+
fly ips list
208+
```
209+
```out
210+
TYPE ADDRESS CREATED AT
211+
v4 213.188.195.22 2m1s ago
212+
v6 2a09:8280:1:502e:4ce8:6058:7f09:62a 1m58s ago
213+
```
214+
215+
And you can always run `flyctl` as `fly` if you want to save a few keystrokes.
216+
217+
## _Arrived at Destination_
218+
219+
You have successfully built, deployed, and connected to your first Python application on Fly.

0 commit comments

Comments
 (0)