29
loading...
This website collects cookies to deliver better user experience
├───FlaskApp
└───app.py # Our main application
└───main.py # Used by gunicorn to run the app
└───requirements.txt # The packages we will be using
└───Dockerfile # Used to create the Docker container
└───Procfile # Used to deploy the container to Heroku
$ sudo mkdir FlaskApp
cd
into the directory using:$ cd FlaskApp
app.py
and requirements.txt
.flask==2.0.1
gunicorn==20.1.0
$ pip install -r requirements.txt
app.py
in your favorite code editor and add the following code:#Import the flask module
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
statement = 'Hello World!'
return statement
#Calls the run method, runs the app on port 5000
app.run(host='0.0.0.0', port='5000')
main.py
and add the following code:from app import app
# Gets the app from app.py and runs it
app.run()
$ gunicorn --bind 0.0.0.0:5000 main:app
[2021-06-25 10:33:51 -0400] [1607] [INFO] Starting gunicorn 20.1.0
[2021-06-25 10:33:51 -0400] [1607] [INFO] Listening at: http://0.0.0.0:5000 (1607)
[2021-06-25 10:33:51 -0400] [1607] [INFO] Using worker: sync
[2021-06-25 10:33:51 -0400] [1609] [INFO] Booting worker with pid: 1609
FlaskApp
directory, create a file with no file extension called Dockerfile
and add the following:#Create a ubuntu base image with python 3 installed.
FROM python:3.8
#Set the working directory
WORKDIR /
#copy all the files
COPY . .
#Install the dependencies
RUN apt-get -y update
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install -r requirements.txt
#Expose the required port
EXPOSE 5000
#Run the command
CMD gunicorn main:app
$ docker build -t flask-app .
$ docker run flask-app
2021-06-25T15:08:47.958205+00:00 app[web.1]: [2021-06-25 15:08:47 +0000] [5] [INFO] Starting gunicorn 20.1.0
2021-06-25T15:08:47.958983+00:00 app[web.1]: [2021-06-25 15:08:47 +0000] [5] [INFO] Listening at: http://0.0.0.0:5000 (5)
2021-06-25T15:08:47.958983+00:00 app[web.1]: [2021-06-25 15:08:47 +0000] [5] [INFO] Using worker: sync
2021-06-25T15:08:47.962897+00:00 app[web.1]: [2021-06-25 15:08:47 +0000] [7] [INFO] Booting worker with pid: 7
$ heroku login
$ heroku create <your_app_name>
$ heroku container:login
Login Succeeded
FlaskApp
directory, create a file with no extension called Procfile
and add:web: gunicorn main:app 0.0.0.0:$PORT
app.py
file, we need to add/modify the following lines marked with arrows:# Import OS to get the port environment variable from the Procfile
import os # <-----
# Import the flask module
from flask import Flask
# Create a Flask constructor. It takes name of the current module as the argument
app = Flask(__name__)
@app.route('/')
def hello_world():
statement = 'Hello World!'
return statement
# Create the main driver function
port = int(os.environ.get("PORT", 5000)) # <-----
app.run(host='0.0.0.0', port=port) # <-----
$ heroku container:push web --app <your_app_name>
$ heroku container:release web --app <your_app_name>
Releasing images web to <your_app_name>... done
$ heroku logs --tail --app <your_app_name>
2021-06-25T15:08:45.662062+00:00 heroku[web.1]: Starting process with command `/bin/sh -c gunicorn\ main:app`
2021-06-25T15:08:47.958205+00:00 app[web.1]: [2021-06-25 15:08:47 +0000] [5] [INFO] Starting gunicorn 20.1.0
2021-06-25T15:08:47.958983+00:00 app[web.1]: [2021-06-25 15:08:47 +0000] [5] [INFO] Listening at: http://0.0.0.0:34683 (5)
2021-06-25T15:08:47.958983+00:00 app[web.1]: [2021-06-25 15:08:47 +0000] [5] [INFO] Using worker: sync
2021-06-25T15:08:47.962897+00:00 app[web.1]: [2021-06-25 15:08:47 +0000] [7] [INFO] Booting worker with pid: 7
2021-06-25T15:08:48.409823+00:00 heroku[web.1]: State changed from starting to up