Containerise your Python Flask using Docker and deploy it onto Heroku
Containerise your Python Flask using Docker and deploy it onto Heroku
Python Flask application can be directly deployed onto Heroku. But sometimes you would want to have more control on your deployment and don’t want to depend on Heroku all the time.
This is why I tried to containerise the Python application using Docker and then deploy the Docker container onto Heroku. In this way, you are not limited to the features offered by Heroku and can move your application to any cloud provider. All you have to do is just deploy the Docker image onto your new provider.
Also using Docker opens up a whole new world of services which may not be offered on Heroku. And by the way did I mention it is free?
Let us create a simple Python Flask Application which prints “Flask Dockerised and deployed to Heroku” when it is called.
Create the Python script app.py which holds our application. The difference from the usual Flask application is in line 3 where we get the port from Heroku. Without this line, Heroku would not be able to bind the port and instead you would be getting an error message saying “Web process failed to bind $PORT within 60 seconds of launch”.
1
2
3
4
5
6
7
8
9
10
11
from flask import Flask
import os
app = Flask(\_\_name\_\_)
port = int(os.environ.get("PORT", 5000))
[@app](http://twitter.com/app "Twitter profile for @app").route('/')
def hello\_world():
return 'Flask Dockerized and deployed to Heroku'
if \_\_name\_\_ == '\_\_main\_\_':
app.run(debug=True,host='0.0.0.0',port=port)
Create the requirements.txt file to hold all your dependencies. In our case we need only Flask.
1
Flask==0.10.1
Create the Dockerfile to build the image and run the Flask application. You could use Ubuntu or Python base image. I have used Ubuntu as my base image and installed Python in it.
1
2
3
4
5
6
7
8
FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT \["python"\]
CMD \["app.py"\]
Build the Docker image locally and run to make sure that the service is running locally.
1
2
3
docker build -t flask-heroku:latest .
docker run -d -p 5000:5000 flask-heroku
Opening the localhost on port 5000 should open our “not so complex” output.
Deploying the application to Heroku
Once you have your application running locally, we will start the actual work of deploying it in Heroku.
Login to Heroku container. It would open the browser and prompt you to login with your Heroku credentials if you are not logged in already.
1
heroku container:login
You would get a message as “Login Succeeded” if everything goes right.
It is time to create a new Heroku application. You could either choose a name or let Heroku create a magic name for your app.
1
2
3
4
5
heroku create
or
heroku create yourawesomeapp
Make sure you are inside the folder where you created the 3 files — app.py, requirements.txt and the almighty Dockerfile.
Execute the below command to create the container onto Heroku
heroku container:push web –app yourawesomeapp
You will get the below messages as response which says that the Docker image has been built, tagged and successfully pushed.
We are almost there with completing our deployment. The container is pushed but not released yet. I’m not exactly sure what could be the reason to have it in pushed stage before releasing. Anyways, the below command would release the container.
1
heroku container:release web --app yourawesomeapp
Once it is released, you would get the message as done.
Now it is time to check out our awesome app running on Heroku.
https://yourawesomeapp.herokuapp.com
Yay! It worked !!
This is a simple application but once we get all the things sorted, it could be expanded to any scale.
The code used is available at https://github.com/ksashok/Flask-Docker-Heroku