<?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>DEV Community: Samora Machel</title>
    <description>The latest articles on DEV Community by Samora Machel (@machel).</description>
    <link>https://dev.to/machel</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%2F701157%2F67a14d58-d263-4fb8-a3c7-8c8493a04878.png</url>
      <title>DEV Community: Samora Machel</title>
      <link>https://dev.to/machel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/machel"/>
    <language>en</language>
    <item>
      <title>Consuming API Using Flask and Containerization with Docker</title>
      <dc:creator>Samora Machel</dc:creator>
      <pubDate>Fri, 01 Oct 2021 22:09:40 +0000</pubDate>
      <link>https://dev.to/machel/consuming-api-using-flask-and-containerization-with-docker-20em</link>
      <guid>https://dev.to/machel/consuming-api-using-flask-and-containerization-with-docker-20em</guid>
      <description>&lt;p&gt;Am sure you've heard about API and wondered what is this everyone is talking about. Say no more, by the end you'll have a high level understanding about APIs and be able to consume the in your back-end application.&lt;br&gt;
Let's get started then!&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an API ?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The acronym &lt;strong&gt;&lt;em&gt;API&lt;/em&gt;&lt;/strong&gt; stands for Application Programming Interface. This allows 2 or more computers or softwares to communicate for data manipulation. It can be used to consume, update or delete data. &lt;a href="https://www.altexsoft.com/blog/engineering/what-is-api-definition-types-specifications-documentation/" rel="noopener noreferrer"&gt;To find out more about API I recommend using the following link&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we got that out of the way we can go to the real stuff. &lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt; Getting Started &lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We are going to build an anime streaming website with a subscription option.&lt;br&gt;
We are going to consume 2 APIs&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://payments.beyonic.com/users/login/?next=/" rel="noopener noreferrer"&gt;Beyonic API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aniapi.com/docs/resources/anime/" rel="noopener noreferrer"&gt;Ani API&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Beyonic API&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is used for payments thus we'll be helpful while implementing our subscription section
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Ani API&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We'll consume this API in order to retrieve the animes to display&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to follow along. I'd recommend using this repository&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/SamoraMachel" rel="noopener noreferrer"&gt;
        SamoraMachel
      &lt;/a&gt; / &lt;a href="https://github.com/SamoraMachel/AnimeTV" rel="noopener noreferrer"&gt;
        AnimeTV
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This is a flask application used for browsing, searching and watching animes. The application is built on top of AniAPI and Beyonic API
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;AnimeTV&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/SamoraMachel/AnimeTV./cover.jpg"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FSamoraMachel%2FAnimeTV.%2Fcover.jpg" alt="Cover Image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is a flask application used for browsing, searching and watching animes. The application is built on top of AniAPI and Beyonic API&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Prequisites&lt;/h3&gt;

&lt;/div&gt;
&lt;p&gt;In order to setup the website you'll first need to install depedancies and setup the .env file
to install all the dependancies. First move into the &lt;code&gt;src&lt;/code&gt; folder and execute the command&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;After a successful installation, create a &lt;code&gt;.env&lt;/code&gt; file inside the src folder and add in&lt;/p&gt;
&lt;div class="highlight highlight-text-adblock notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;BEARER_TOKEN=" &amp;lt;your aniapi token&amp;gt; "

BASE_URL = 'https://api.aniapi.com/v1'

WEBSITE_URL = 'http://localhost:5000/'&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;To get the aniapi token, You'll have to signup to the &lt;a href="https://aniapi.com/login/" rel="nofollow noopener noreferrer"&gt;AniAPI Website&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To run the application run :&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;python main.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/SamoraMachel/AnimeTV" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;h3&gt;
  
  
  Prequisites
&lt;/h3&gt;

&lt;p&gt;First we set up our work area. We are going to use a virtual environment to avoid system and application dependencies collision&lt;br&gt;
We first download the virtualenv library using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;virtualenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can then instantiate our environment using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; virtualenv AnimeTv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are going to use the following libraries which will aid in the construction of our application and also consumption of our the APIs&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flask&lt;/li&gt;
&lt;li&gt;beyonic&lt;/li&gt;
&lt;li&gt;requests&lt;/li&gt;
&lt;li&gt;python dotenv&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;this can be installed using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;flask beyonic requests python-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Consuming API
&lt;/h3&gt;

&lt;p&gt;Since our main focus is how to work with API, you can get the project template in the &lt;a href="https://github.com/SamoraMachel/AnimeTV" rel="noopener noreferrer"&gt;repository above&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are going to use the concept of abstraction. Well divide our application into 4 parts &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;view&lt;/code&gt; - this will be responsible for rendering our templates &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;utils&lt;/code&gt; - this will be responsible for consumption of APIs and then abstracting it into functions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main&lt;/code&gt; - this will be responsible running the application &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;config&lt;/code&gt; - this will be responsible for configuration changes on the application &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can then create each file respectively like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;src&lt;span class="se"&gt;\&lt;/span&gt;
  view.py
  main.py
  utils.py
  config.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;first we configure our application :&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa12jznd3turdkfhuc3v4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa12jznd3turdkfhuc3v4.png" alt="flask configuration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we can then use the configured application instance to instantiate our application in the main.py like so:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fupjiedvr1g965r83rtze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fupjiedvr1g965r83rtze.png" alt="Flask instantiation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we create the utility function which will be used to produced data which can be rendered by our views&lt;/p&gt;

&lt;p&gt;First we have to create a &lt;code&gt;.env&lt;/code&gt; file so that we can place our api token and base url there. Therefore it should look something like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifxb6dvkcfn25urth4d1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifxb6dvkcfn25urth4d1.png" alt="flask env file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;utils.py&lt;/code&gt; we can get the environment variables and use them. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnih4mfdaits8b1pqfnek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnih4mfdaits8b1pqfnek.png" alt="getting environment variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are going to use the request library to consume the APIs as we pass in the API tokens in the header section. We first create a header dictionary for holding all the metadata for the header &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff09dk9dfq8wjxwpyeiiy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff09dk9dfq8wjxwpyeiiy.png" alt="API tokens"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The requests library has various methods but we are majorly going to focus on the get method.&lt;br&gt;
which can be used like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;URL_HEADERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;since at times you might have network problem while fetching data or something might just happen during fetching, if not checked, this might break your code. Therefore it is advisable to always place this request in a try and catch block in order to prevent the code from breaking.&lt;br&gt;
To reduce the number of request constantly made to the server we'll have a global object holding the data fetched.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1onz1sdjl0fqoe8g49dd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1onz1sdjl0fqoe8g49dd.png" alt="Consuming API"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our functions thus returns the data fetched&lt;/p&gt;

&lt;p&gt;Since the beyonic API have been abstracted by the beyonic library we can then use the classes and functions created.&lt;br&gt;
To create a new payment we use the Payment class and call the method create on it passing in the data captured from the user.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8pnreg8tpd70xsahs8c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8pnreg8tpd70xsahs8c.png" alt="Beyonic API"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also get the currencies and transaction from the library like so:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7drl0v4cepvjq3c2t86u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7drl0v4cepvjq3c2t86u.png" alt="Beyonic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we have abstracted all the functionalities we require we can then get back to our view.py and use the functions.&lt;/p&gt;

&lt;p&gt;First we need to display all the animes within the page. We first get all the animes and genres using the functions from the utils package and then pass it to our template like so:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl0yvh9g9dj9oshedevaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl0yvh9g9dj9oshedevaw.png" alt="flask template"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When someone makes a subscription we are required to make a new payment thus the data will be posted to our application and be processed one one of the function in the utils library like so:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdbfbmv2wu1aarcnwv4u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdbfbmv2wu1aarcnwv4u.png" alt="beyonic payment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;once we are done creating our views, we then import then to our main.py otherwise the application will not find routes to go to:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6s71zrukbso4f3s63mgo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6s71zrukbso4f3s63mgo.png" alt="flask"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Dockerize our application
&lt;/h4&gt;

&lt;p&gt;we can then dockerize our application using the following docker file :&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6lnrgymaz7c4847qyqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6lnrgymaz7c4847qyqh.png" alt="Docker"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can then build and run your application using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker image build &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-t&lt;/span&gt; animetv
docker run animetv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hooray!!! You now know how to consume API &lt;/p&gt;

&lt;p&gt;To learn more about docker and flask &lt;a href="https://dev.to/machel/getting-started-with-docker-flask-5bm7"&gt;use the following&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>docker</category>
    </item>
    <item>
      <title>Getting Started with Docker &amp; Flask</title>
      <dc:creator>Samora Machel</dc:creator>
      <pubDate>Mon, 13 Sep 2021 09:12:44 +0000</pubDate>
      <link>https://dev.to/machel/getting-started-with-docker-flask-5bm7</link>
      <guid>https://dev.to/machel/getting-started-with-docker-flask-5bm7</guid>
      <description>&lt;p&gt;Probably your wondering what docker is all about, or your just heard it from you friends. Worry no more cause you've come to the right post.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is docker ?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Docker is a containerization software used to bundle your project into an executable source component shipping with dependencies and operating system libraries that enable the source code to run.&lt;/li&gt;
&lt;li&gt;In simple terms docker is a revolutionary software that enable one to bundle his software and enable the programmer run his program in any other computer without pre-installing necessary libraries every time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Flask ?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Flask is a python framework used for creating web application. Why flask? Flask is know for it fast nature and also it is more flexible when you want no restriction in terms of coding paradigm. Its also easy to learn as compared to Django.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;

&lt;p&gt;Use the following links to install both docker and flask &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.docker.com/engine/install/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://phoenixnap.com/kb/install-flask" rel="noopener noreferrer"&gt;Flask&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After successful installation, we are going to create a simple flask project that handles both GET and POST. &lt;/p&gt;

&lt;p&gt;Create an empty directory named &lt;strong&gt;&lt;em&gt;src&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
inside this directory is where we are going to place our program.&lt;/p&gt;

&lt;p&gt;Create and three files like so&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnpk3hp5omh4n0kukddm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnpk3hp5omh4n0kukddm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The

&lt;code&gt;Dockerfile&lt;/code&gt;

will be used to hold our docker code.&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
&lt;li&gt;The

&lt;code&gt;requirements.txt&lt;/code&gt;

will be used to hold the dependencies of our project.&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
&lt;li&gt;The

&lt;code&gt;main.py&lt;/code&gt;

will be used to hold contents of our program.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Working with Flask
&lt;/h2&gt;

&lt;p&gt;In order to first get started with Flask we first import the class Flask from the module flask&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyim83hr7w2ggcnz64p1z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyim83hr7w2ggcnz64p1z.png" alt="importing flask"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We then create an instance of our app using the Flask class&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ica1mk6652bq2rbkew8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ica1mk6652bq2rbkew8.png" alt="flask"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the apps' instance we can then create route ( Endpoints ) for our web application. The app is used as a decorator in this instance where we provide: &lt;br&gt;
&lt;strong&gt;&lt;em&gt;the name of the endpoint&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;the method for the endpoint&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://www.pythontutorial.net/advanced-python/python-decorators/" rel="noopener noreferrer"&gt;Learn more about decorators&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since this is an introduction course we are going to build a simple hello world application.&lt;br&gt;
For a simple web page to display hello world in the page we can add the following&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5832i22qlk6hz1k04n0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5832i22qlk6hz1k04n0v.png" alt="python decorators"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In case you want your endpoint to be used for more than a GET request we can easily add the methods to the method list like so&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdn0yipez3sr8xot1fjp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdn0yipez3sr8xot1fjp.png" alt="python decorators"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To run our application well have to call run on the apps' instance. We can optionally provide arguments like: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;debug - Tells the app to run in debug or production mode&lt;/li&gt;
&lt;li&gt;host - defines the host of the application&lt;/li&gt;
&lt;li&gt;port - defines the port number for the application to run in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the arguments are not provided then it will use the default values.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgeazek3bsjt2y2zisss2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgeazek3bsjt2y2zisss2.png" alt="flask run"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can then use the following command to run your flask application&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Containerizing our Application
&lt;/h2&gt;

&lt;p&gt;Now that we have our miniature flask application now we can containerize it using docker.&lt;br&gt;
Moving to our &lt;strong&gt;&lt;em&gt;Dockerfile&lt;/em&gt;&lt;/strong&gt; we can first add the following code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9wtdoh13qbn84klorsz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9wtdoh13qbn84klorsz.png" alt="docker code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;FROM&lt;/code&gt; - defined the base image or the operations&lt;br&gt;
&lt;code&gt;COPY&lt;/code&gt; - copies files and folders from the source to the destination filesystem path&lt;br&gt;
&lt;code&gt;WORKDIR&lt;/code&gt; - sets the working directory of the destination filesystem&lt;br&gt;
&lt;code&gt;RUN&lt;/code&gt; - runs the commands on top of the current image&lt;br&gt;
&lt;code&gt;EXPOSE&lt;/code&gt; - define the network port for our container to listen to during runtime&lt;br&gt;
&lt;code&gt;CMD&lt;/code&gt; - defines the command for executing our &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We then have to build our application so that it may setup the environment and also download the necessary file for it to run. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7adhynwshia018q0h3hz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7adhynwshia018q0h3hz.png" alt="docker run"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We specify the container name. We have names ours &lt;code&gt;getting-started&lt;/code&gt; and also the path of our application.&lt;/p&gt;

&lt;p&gt;Since we exposed our application to run on port 5000. We can then run our application like so&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdxhotze5dyau6czsw2gh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdxhotze5dyau6czsw2gh.png" alt="docker"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;our application with the run on port 5000.&lt;br&gt;
Our app will be bundled into a container which you can easily share with your fellow programmers.&lt;br&gt;
Hope this article was insightful and you were able to grasp something about docker and flask. If you need a deep dive into &lt;a href="https://docs.docker.com/" rel="noopener noreferrer"&gt;docker&lt;/a&gt; or &lt;a href="https://flask.palletsprojects.com/en/2.0.x/" rel="noopener noreferrer"&gt;flask&lt;/a&gt; you can follow the links to help you.&lt;/p&gt;

&lt;p&gt;Good luck with you endeavors. &lt;/p&gt;

</description>
      <category>docker</category>
      <category>flask</category>
      <category>webdev</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
