<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Yusuff Jamal</title>
    <description>The latest articles on Forem by Yusuff Jamal (@erenaspire7).</description>
    <link>https://forem.com/erenaspire7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F408918%2Ff181e650-d041-4f87-9001-bc8f8db77330.png</url>
      <title>Forem: Yusuff Jamal</title>
      <link>https://forem.com/erenaspire7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/erenaspire7"/>
    <language>en</language>
    <item>
      <title>Deploying a Dockerized Flask App to Heroku</title>
      <dc:creator>Yusuff Jamal</dc:creator>
      <pubDate>Mon, 15 Jun 2020 06:43:49 +0000</pubDate>
      <link>https://forem.com/erenaspire7/deploying-a-dockerized-flask-app-to-heroku-5h7j</link>
      <guid>https://forem.com/erenaspire7/deploying-a-dockerized-flask-app-to-heroku-5h7j</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9t-hQJMU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o0m7qye387sujfvge108.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9t-hQJMU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o0m7qye387sujfvge108.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/photos/webyw4NsFPg?utm_source=dropbox_paper&amp;amp;utm_medium=referral"&gt;Andy Holmes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Welcome! This tutorial is going to be divided into 3 parts.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* Part 1 - Create A Basic Flask App
* Part 2 - Dockerize the Application
* Part 3 - Deploy the Application to Heroku
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Prerequisites:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Git/Github - https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- Flask - https://flask.palletsprojects.com/en/1.1.x/installation/
- Heroku - https://devcenter.heroku.com/articles/heroku-cli#download-and-install
- Your Fav Code Editor (VSCode or Atom)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Part 1 - Create A Basic Flask App
&lt;/h2&gt;

&lt;p&gt;Our Flask App is going to be a basic “Hello World!” app. &lt;/p&gt;

&lt;p&gt;Firstly, create the directory for our project&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir Flask-Docker-App

$ cd Flask-Docker-App
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, we’d initialize Git&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git init
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Open your code editor and create 4 files.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- app.py - Our main script
- .gitignore - A file that tells Git to ignore some files within your project
- README.md - The ReadMe file for your project.
- requirements.txt - This specifies all the packages used in your project. 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Set up a virtual environment to use for our application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ python3 -m venv env
$ source env/bin/activate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When Flask is installed(see link above), it downloads other packages in order to make it work efficiently. In order to add these packages to our “requirements.txt” file. We do&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ python -m pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, we’d add the following code to app.py&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
  return "Hello World!"

if __name__ == '__main__':
  app.run()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, we’d run app.py&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ python app.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The Flask App would be hosted on &lt;a href="http://localhost:5000/"&gt;http://localhost:5000/&lt;/a&gt;.  End the server by pressing “Ctrl + C”.&lt;/p&gt;

&lt;p&gt;Congratulations! You’ve made your first “Hello World!” Flask App.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 2 - Dockerize the Application
&lt;/h2&gt;

&lt;p&gt;For this section, you’d need Docker installed.&lt;/p&gt;

&lt;p&gt;On your code editor, create a “Dockerfile”(no extensions)&lt;/p&gt;

&lt;p&gt;Then, edit your “Dockerfile”&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3.8.1

ENV APP_HOME /app
WORKDIR $APP_HOME

COPY . /app

RUN pip install -r requirements.txt

ENTRYPOINT ["python app.py"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Before building your image, your app.py file needs to be edited&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if __name__ == '__main__':
  app.run(debug = True, host = '0.0.0.0')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, Build the docker image&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker build -t &amp;lt;your username&amp;gt;/flask-docker .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And run it&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker run -it -p 2000:5000 &amp;lt;your username&amp;gt;/flask-docker
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This allows you to test your app locally. It’s hosted on &lt;a href="http://localhost:2000"&gt;http://localhost:2000&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;End the server by pressing “Ctrl + C”.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 3 - Deploy the Application to Heroku
&lt;/h2&gt;

&lt;p&gt;Heroku needs to be installed (see link above). Create an heroku account, then run&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku login
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Once logged in, open your code editor and create an “heroku.yml” file. Then add this to the file&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;build:
  docker:
    web: Dockerfile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If your “Dockerfile” is within another folder&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;build:
  docker:
    web: &amp;lt;some-folder&amp;gt;/Dockerfile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next, we create our heroku app&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku create &amp;lt;app-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And add the remote branches&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote add &amp;lt;branch-name&amp;gt; &amp;lt;app-link&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Heroku runs your app depending on the ports available. In several instances, It might not assign the port 5000 which means our app crashes. In order to fix this error, we’d add this to our “app.py”&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

# ----Remainder of Code----

if __name__ == '__main__':
  port = int(os.environ.get('PORT', 5000))
  app.run(host = '0.0.0.0', port = port)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This tells our app to use the port assigned by heroku, if not, use 5000.&lt;/p&gt;

&lt;p&gt;Then, we’d need to commit changes made so far. This is done by:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .


git commit -m "First Commit"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But, heroku needs to know your app is ran within a container. So, we type&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku stack:set container
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally, we’d push our changes&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git push &amp;lt;branch-name&amp;gt; master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Congratulations! You’ve made it to the end of this tutorial. I hope you learnt some new things about Docker and Heroku.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>python</category>
      <category>flask</category>
      <category>heroku</category>
    </item>
  </channel>
</rss>
