<?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: Damanpreet Singh</title>
    <description>The latest articles on DEV Community by Damanpreet Singh (@fumblehool).</description>
    <link>https://dev.to/fumblehool</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%2F118956%2Fd2d86d8d-24e6-43a7-84d5-847975f0adf6.jpeg</url>
      <title>DEV Community: Damanpreet Singh</title>
      <link>https://dev.to/fumblehool</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fumblehool"/>
    <language>en</language>
    <item>
      <title>npm ERR! Unexpected end of JSON input while parsing near...</title>
      <dc:creator>Damanpreet Singh</dc:creator>
      <pubDate>Fri, 19 Nov 2021 05:37:17 +0000</pubDate>
      <link>https://dev.to/fumblehool/npm-err-unexpected-end-of-json-input-while-parsing-near-42fn</link>
      <guid>https://dev.to/fumblehool/npm-err-unexpected-end-of-json-input-while-parsing-near-42fn</guid>
      <description>&lt;h6&gt;
  
  
  Actual Behavior:
&lt;/h6&gt;

&lt;p&gt;User tries to install npm packages using &lt;code&gt;npm i&lt;/code&gt;, and gets an error message saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm ERR! Unexpected end of JSON input while parsing near...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h6&gt;
  
  
  Possible Solutions:
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;Clear the npm cache:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm cache clean --force
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Try updating to the latest npm:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i npm@latest -g
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Try deleting &lt;code&gt;package-lock.json&lt;/code&gt; file. It keeps track of exact version of every package that is installed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rm package-lock.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Git: The common solutions.</title>
      <dc:creator>Damanpreet Singh</dc:creator>
      <pubDate>Wed, 30 Dec 2020 16:54:54 +0000</pubDate>
      <link>https://dev.to/fumblehool/git-the-common-solutions-2pcg</link>
      <guid>https://dev.to/fumblehool/git-the-common-solutions-2pcg</guid>
      <description>&lt;p&gt;I have been using git for quite some time now. It seems quite easy to use at first but as one keeps using it, one may face certain difficulties. Thankfully, Git provides solution for relatively common scenarios.&lt;/p&gt;

&lt;h1&gt;
  
  
  Scenario #1 :
&lt;/h1&gt;

&lt;p&gt;Suppose, I've created a project that has use private or secret tokens. Obviously, I would not let my private token end up in the version control. So, I'll have to make sure the file is not accidentally added to the version control.&lt;br&gt;
Also, I may have some files or directories that I don't want to add to the version control( eg - node_modules/ ). But these will keep there as ​untracked files.&lt;/p&gt;
&lt;h3&gt;
  
  
  Solution - GitIgnore
&lt;/h3&gt;

&lt;p&gt;In order to handle the above scenario, git provides &lt;code&gt;gitignore&lt;/code&gt;. It is basically a file that you create in your git repository to ignore some files and/or directories while committing.&lt;br&gt;
Inside the git repository, create a file named &lt;code&gt;.gitignore&lt;/code&gt; and in that file, add all the file names which you don't want to add to the version control. Once you have committed this file, the file or directory names in &lt;code&gt;.gitignore&lt;/code&gt; will be ignored by git.&lt;/p&gt;

&lt;p&gt;A .gitignore may 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%2Ffumblehool.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-12-at-6-46-54-pm.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%2Ffumblehool.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-12-at-6-46-54-pm.png" alt=".gitignore"&gt;&lt;/a&gt;&lt;br&gt;
It will ignore &lt;code&gt;npm-debug.log&lt;/code&gt;, &lt;code&gt;config.js&lt;/code&gt;, and &lt;code&gt;node_modules/&lt;/code&gt; directory. If you want to ignore all &lt;code&gt;.js&lt;/code&gt; files within your repository, then you may add &lt;code&gt;*.js&lt;/code&gt; in your &lt;code&gt;.gitignore&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Read more about this &lt;a href="https://git-scm.com/docs/gitignore" rel="noopener noreferrer"&gt;Here&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  Scenario #2 :
&lt;/h1&gt;

&lt;p&gt;The workflow with git is something like this -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clone a repository to local machine using git clone.&lt;/li&gt;
&lt;li&gt;Make the required changes.&lt;/li&gt;
&lt;li&gt;Commit files.&lt;/li&gt;
&lt;li&gt;Push the files to remote location using git push&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Seems simple isn't it? Well, it is simple but if there all multiple files, it will take a lot of time to clone. Further, if it has a lot of commit history, then the time consumed in cloning increases. &lt;strong&gt;For Example&lt;/strong&gt;- If a repository has &amp;gt;4K commits, every git clone will fetch all of the commit history.&lt;/p&gt;
&lt;h3&gt;
  
  
  Solution - Git Shallow Clone
&lt;/h3&gt;

&lt;p&gt;Let us talk about the above example. The repository has &amp;gt;4K commits. I may not require all of this commit history. The cloning process will be faster if we only wanted the latest commits. Git provides a feature to only clone the required commit history. This is called &lt;code&gt;Git Shallow Clone&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To use it, along with the git clone command pass a flag &lt;code&gt;--depth&lt;/code&gt; and specify the depth of commit history you want. Here is an example -&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%2Ffumblehool.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-12-at-7-08-35-pm1.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%2Ffumblehool.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-12-at-7-08-35-pm1.png" alt="Git shallow clone"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, I'm trying to clone Linux kernel source tree. It has total of 704,972 commits.&lt;/p&gt;

&lt;p&gt;So, a git clone will download 5608186 objects while with --depth 1 the size is reduced to 65237 objects.&lt;/p&gt;

&lt;p&gt;Read more about this &lt;a href="https://git-scm.com/docs/git-clone#git-clone---depthltdepthgt" rel="noopener noreferrer"&gt;Here&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Scenario #3 :
&lt;/h1&gt;

&lt;p&gt;In order to work on a new feature, I've clone the repository. Now, my new changes may break the code. In other words, I'm not 100% sure that my changes will work perfectly. Suppose, while adding new feature, I received a call that another issue is critical and I need to fix it as soon as possible. So I fixed the issue and now I cannot push the code. Why? because the new feature is not fully functional. But the fix has to be deployed. What to do?&lt;/p&gt;

&lt;p&gt;I can again clone the project and fix the critical bug. But, it I'll need to do all the work again.&lt;/p&gt;
&lt;h3&gt;
  
  
  Solution - Git branch
&lt;/h3&gt;

&lt;p&gt;Git provides an independent line of development. We may think of them as a way to request a brand new working directory.&lt;/p&gt;

&lt;p&gt;The commits in branches are isolated from other branches. This means if I've committed a change in one branch, it will not be visible in other branches. By default, we are on the master branch but we can make as many branches as we need.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git branch -&amp;gt; list all available branches&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The output will be like this-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* master
branch_bug_fix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Currently I'm in &lt;code&gt;master&lt;/code&gt; branch. In order to change branch I can use -&lt;br&gt;
&lt;code&gt;git checkout &amp;lt;BRANCH_NAME&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To create a new branch using current branch as source: &lt;code&gt;git checkout -b &amp;lt;NEW_BRANCH_NAME&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To delete a branch use: &lt;code&gt;git branch -d &amp;lt;BRANCH_NAME&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Read more about this &lt;a href="https://git-scm.com/docs/git-branch" rel="noopener noreferrer"&gt;Here&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Scenario #4 :
&lt;/h1&gt;

&lt;p&gt;So, I'm working on a feature and I need to change branch and work on something else. But wait! I haven't committed the code and git would not allow me to switch branches without committing. Also, the work is not complete so I cannot commit changes. The simplest solution is to commit changes with commit message like - feature x incomplete. It will work but it will add unnecessary commit history.&lt;/p&gt;
&lt;h3&gt;
  
  
  Solution - Git Stash
&lt;/h3&gt;

&lt;p&gt;Git stash - Stash the changes in a dirty working directory away.&lt;/p&gt;

&lt;p&gt;It stashes or saved changes temporarily so that user can work on something else, and then user can come back and re-apply them later on. It is very useful if there is a need to switch between different branches and switch the work mid-way through a code change.&lt;/p&gt;

&lt;p&gt;In order to stash changes, use:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;$git stash&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;We can reapply the changes using:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;$git stash apply&lt;/code&gt;&lt;br&gt;
&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%2Ffumblehool.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-13-at-1-25-13-pm1.png%3Fw%3D636" 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%2Ffumblehool.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-13-at-1-25-13-pm1.png%3Fw%3D636" alt="git stash"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read more about this &lt;a href="https://git-scm.com/docs/git-stash" rel="noopener noreferrer"&gt;Here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
    </item>
    <item>
      <title>Vim: Delete all lines containing a pattern</title>
      <dc:creator>Damanpreet Singh</dc:creator>
      <pubDate>Sun, 16 Aug 2020 18:27:15 +0000</pubDate>
      <link>https://dev.to/fumblehool/vim-delete-all-lines-containing-a-pattern-114d</link>
      <guid>https://dev.to/fumblehool/vim-delete-all-lines-containing-a-pattern-114d</guid>
      <description>&lt;p&gt;Use this command to delete all lines containing &lt;code&gt;pattern&lt;/code&gt; -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:g/pattern/d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>vim</category>
    </item>
    <item>
      <title>Vim - add string to end of each line</title>
      <dc:creator>Damanpreet Singh</dc:creator>
      <pubDate>Wed, 12 Aug 2020 12:05:50 +0000</pubDate>
      <link>https://dev.to/fumblehool/vim-add-string-to-end-of-each-line-lde</link>
      <guid>https://dev.to/fumblehool/vim-add-string-to-end-of-each-line-lde</guid>
      <description>&lt;p&gt;Use this command to append &lt;code&gt;,&lt;/code&gt; to end of every line -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:%norm A,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>vim</category>
    </item>
    <item>
      <title>Format JSON in vim</title>
      <dc:creator>Damanpreet Singh</dc:creator>
      <pubDate>Wed, 12 Aug 2020 12:03:28 +0000</pubDate>
      <link>https://dev.to/fumblehool/format-json-in-vim-18lb</link>
      <guid>https://dev.to/fumblehool/format-json-in-vim-18lb</guid>
      <description>&lt;p&gt;Vim command to format JSON file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:%!python -m json.tool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>vim</category>
      <category>json</category>
      <category>formatting</category>
    </item>
    <item>
      <title>Create a Google Chrome Extension in 15 minutes.</title>
      <dc:creator>Damanpreet Singh</dc:creator>
      <pubDate>Sun, 09 Aug 2020 17:01:18 +0000</pubDate>
      <link>https://dev.to/fumblehool/create-a-google-chrome-extension-in-15-minutes-35d5</link>
      <guid>https://dev.to/fumblehool/create-a-google-chrome-extension-in-15-minutes-35d5</guid>
      <description>&lt;p&gt;In this blog, I'm going to build a simple Chrome Extension that gives user a healthy habit whenever he clicks on the "Get a Healthy Habit!!" button.&lt;/p&gt;

&lt;p&gt;The source code is available &lt;a href="https://github.com/fumblehool/HealthyHabits-Chrome-extension"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Initial Set Up
&lt;/h1&gt;

&lt;p&gt;A chrome extension required following files -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;icon.png - this is the icon displayed to user.&lt;/li&gt;
&lt;li&gt;manifest.json - This file contains properties like extensions' name, extension's version, author's name, and &lt;strong&gt;most importantly what permissions the extension requires.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Setting up manifest.json
&lt;/h1&gt;

&lt;p&gt;Our extension does not require any special permission. So our manifest.json will look  this-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "manifest_version": 2,

  "name": "Healthy Habits Plugin",
  "description": "This extension will give show Healthy habits.",
  "version": "1.0",

  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },
  "permissions": [
   "activeTab"
   ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Points to note -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name - name 0f extension&lt;/li&gt;
&lt;li&gt;description - description of what this extension does.&lt;/li&gt;
&lt;li&gt;version - extension's version&lt;/li&gt;
&lt;li&gt;browser_action - here we specify browser actions.&lt;/li&gt;
&lt;li&gt;default_icon - this is the default icon of extension.&lt;/li&gt;
&lt;li&gt;default_popup - this is the html that will appear if user clicks on extension icon.&lt;/li&gt;
&lt;li&gt;permissions - we only need activeTab permission&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read more about manifest.json &lt;a href="https://developer.chrome.com/extensions/manifest"&gt;Here&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  popup.html
&lt;/h1&gt;

&lt;p&gt;Our popup.html will look something like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!doctype html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;HealthyHabits&amp;lt;/title&amp;gt;
    &amp;lt;script src="popup.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;style&amp;gt;
        #habit{
            font-size: 22px;
            text-align: center;
            border: 1px solid red;
            margin-bottom: 10px;
        }
    &amp;lt;/style&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;HealthyHabit&amp;lt;/h1&amp;gt;
    &amp;lt;div id="habit"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;button id="getHabit"&amp;gt;Get A Healthy Habit!!&amp;lt;/button&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  popup.js
&lt;/h1&gt;

&lt;p&gt;This file contains the logic to handle the button click. It will look something like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener('DOMContentLoaded', function() {
    var getHabitButton = document.getElementById('getHabit');
    var count = 0;
    getHabitButton.addEventListener('click', function() {
        var data = {
            "habits":[
                {"habit":"Eat Breakfast Every Morning"},
                {"habit":"Add Fish &amp;amp; Omega-3 Fatty Acids To Your Diet"},
                {"habit":"Get Enough Sleep"},
                {"habit":"Make Social Connections"},
                {"habit":"Exercise For Better Health"},
                {"habit":"Practice Dental Hygiene"},
                {"habit":"Take Up A Hobby"},
                {"habit":"Protect Your Skin"},
                {"habit":"Snack The Healthy Way"},
                {"habit":"Drink Water &amp;amp; Eat Dairy"},
                {"habit":"Drink Tea"},
                {"habit":"Take A Daily Walk"},
                {"habit":"Plan"}
            ]
        }
        document.getElementById('habit').innerHTML =
            data.habits[count%13].habit;
        count = count +1;
  }, false);
}, false);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever the button is pressed, it chooses next habit from data and will set it in our div's content.&lt;/p&gt;

&lt;h1&gt;
  
  
  Testing it out
&lt;/h1&gt;

&lt;p&gt;It’s really easy to test a new extension in Chrome. Type &lt;strong&gt;“chrome://extensions”&lt;/strong&gt; in a tab to bring up the extensions page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dZdWeV81--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/fumblehool/HealthyHabits-Chrome-extension/master/images/extension.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dZdWeV81--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/fumblehool/HealthyHabits-Chrome-extension/master/images/extension.png" alt="Extension"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once on this page, check “Developer mode” to enable loading unpacked extensions. This will allow you to load your extension from a folder. Finally, click “Load unpacked extension” and select the extension folder to load up the extension. You should immediately see the extension show up as a Browser Action with your icon in the toolbar window of the current tab.&lt;/p&gt;

&lt;p&gt;To test out the extension, Simple click the icon for our extension. When the HTML page comes up, click “Get a Healthy Habit!!” and you should immediately see a healthy habit being displayed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4-j_XqJX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/fumblehool/HealthyHabits-Chrome-extension/master/images/UI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4-j_XqJX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/fumblehool/HealthyHabits-Chrome-extension/master/images/UI.png" alt="Extension in Action"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that’s it! We have created our first Google Chrome Extension.&lt;/p&gt;

&lt;p&gt;Resources -&lt;br&gt;
&lt;a href="https://developer.chrome.com/extensions/overview"&gt;https://developer.chrome.com/extensions/overview&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Dockerize Flask App</title>
      <dc:creator>Damanpreet Singh</dc:creator>
      <pubDate>Sun, 26 Jul 2020 14:55:25 +0000</pubDate>
      <link>https://dev.to/fumblehool/dockerize-flask-app-5bgd</link>
      <guid>https://dev.to/fumblehool/dockerize-flask-app-5bgd</guid>
      <description>&lt;p&gt;In this post, I'm going to create a simple Flask app and will run it inside a docker container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;docker - We need to install docker for this. To install docker on your system follow this &lt;a href="https://fumblehool.wordpress.com/2017/06/04/getting-started-with-docker/" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;br&gt;
A Flask app - You can find the sample code &lt;a href="https://gist.github.com/fumblehool/6e99cc9f8819f90cb3b273cc42ef4b65" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Docker Hub image
&lt;/h2&gt;

&lt;p&gt;Since Docker Hub doesn’t have an official Flask repository (at the time of this writing), I’ll be building my own image. There are some non-official images available but it's generally recommended to make your own Dockerfile.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's Create Our Dockerfile
&lt;/h2&gt;

&lt;p&gt;Make Project directory&lt;/p&gt;

&lt;p&gt;Start with creating a new directory; let’s call it flask_project:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir flask_web&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our flask_app.py file will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# simple Flask app
From flask import Flask

app = Flask(__name__)

@app.route("/hello")
def hello():
 return "hello user!!"

@app.route("/")
def index():
 return "hello. You have arrived at '/'"

if __name == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5005)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we need to include Flask in our &lt;code&gt;requirements.txt&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Flask==0.10.1&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Flask Dockerfile
&lt;/h2&gt;

&lt;p&gt;Inside our project directory, we'll create a file named - Dockerfile. your directory will look something like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\-Project
  | 
  |-flask_app.py
  |-Dockerfile
  |-Requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open up the &lt;code&gt;Dockerfile&lt;/code&gt; and add following -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM ubuntu:16.04

MAINTAINER Damanpreet Singh "daman.4880@gmail.com"

RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential

# We copy just the requirements.txt first to leverage Docker cache
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT [ "python" ]

CMD [ "flask_app.py" ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FROM ubuntu:16.04&lt;/code&gt; - We are using ubuntu 16.04 as base image&lt;/p&gt;

&lt;p&gt;&lt;code&gt;MAINTAINER&lt;/code&gt; - Used for author field of image( used when pushing image to docker hub).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;RUN&lt;/code&gt; - It tells docker to run the commands specified. In our case, the commands are apt-get update and install&lt;/p&gt;

&lt;p&gt;&lt;code&gt;COPY&lt;/code&gt; - copies file from first parameter( . ) from host to the destination parameter(/app).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ENTRYPOINT&lt;/code&gt; -  configures the container to run as an executable; only the last ENTRYPOINT instruction executes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building Docker Image
&lt;/h2&gt;

&lt;p&gt;To build docker image from our Dockerfile, run following command -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$docker build -t flask-web-app .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-t&lt;/code&gt; - lets you add custom tag name to the image- for me it is flask-web-app.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.&lt;/code&gt; - here we need to specify the location of Dockerfile.  Since, I'm in the same directory, I'm using &lt;code&gt;.&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Docker Image
&lt;/h2&gt;

&lt;p&gt;To run the Docker Image, run following command -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker run -p 5000:5005 flask-web-app&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-p&lt;/code&gt; is used to specify port forwarding. Here we are specifying docker to forward port no. 5005 of image to port no. 5000 of host OS.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;flask-web-app&lt;/code&gt; is the name of the image that we want to run.&lt;/p&gt;

&lt;p&gt;We can find the container runtime details using following command -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker ps -a&lt;/code&gt;&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%2Ffumblehool.files.wordpress.com%2F2017%2F11%2Fscreen-shot-2017-11-21-at-9-59-56-pm.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%2Ffumblehool.files.wordpress.com%2F2017%2F11%2Fscreen-shot-2017-11-21-at-9-59-56-pm.png" alt="docker ps output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  See the Results
&lt;/h2&gt;

&lt;p&gt;To see the results, open the web browser and point it to  &lt;a href="http://localhost:5000/" rel="noopener noreferrer"&gt;http://localhost:5000/&lt;/a&gt;&lt;/p&gt;

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