How to Deploy Docker Container on Heroku? | Part - 2
In the previous article, we’ve seen about How to Deploy a Flask app on Heroku. If you haven’t seen it yet, click the below link to see it.
Structure of Folder -
app/
app.py
requirements.txt
Dockerfile
docker-compose.yml
- Create a New Folder, Go inside it. Initialize it as Git Repository.
mkdir your-folder-name
cd your-folder-namegit init
2. Create a Simple Hello World Flask Application
app.py
from flask import Flask
from os import environapp = Flask(__name__)@app.route('/')
def hello():
return "Hello World!"if __name__ == '__main__':
app.run(debug = True, host = '0.0.0.0', port=environ.get("PORT", 5000))
3. Add the libraries which you have used in this Application in requirements.txt
requirements.txt
flask
4. Create a Dockerfile to dockerize the Flask Application
Dockerfile
FROM python:3.8-alpineWORKDIR /appADD . /appRUN pip install -r requirements.txtCMD ["python", "app.py"]EXPOSE 5000
5. Create a docker-compose.yml file
docker-compose.yml
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/app
6. Run locally by using the below command and check whether it is working or not
sudo docker-compose up
Press Ctrl + C to stop the running container.
7. Login into your Heroku and Docker account
heroku loginsudo docker login --username=<username> --password=<password>
8. Login into Heroku Container
sudo heroku container:login
9. Create a new Heroku app and add the Existing local directory to Remote Heroku Repository
heroku create <app-name>heroku git:remote -a <app-name>
10. Push and Release
sudo heroku container:push web --app <app-name>sudo heroku container:release web --app <app-name>
Open browser and Enter the URL https://<app-name>.herokuapp.com/
You can see the output Hello World! in the browser.
Thank you for reading this article.