<?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: Anquangman</title>
    <description>The latest articles on Forem by Anquangman (@anquangman).</description>
    <link>https://forem.com/anquangman</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%2F153976%2F352f9fe5-5c01-4d5e-8410-c1029bfc5270.jpg</url>
      <title>Forem: Anquangman</title>
      <link>https://forem.com/anquangman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anquangman"/>
    <language>en</language>
    <item>
      <title>Debug Flask Application in a Docker container</title>
      <dc:creator>Anquangman</dc:creator>
      <pubDate>Wed, 04 Sep 2019 05:43:05 +0000</pubDate>
      <link>https://forem.com/anquangman/debug-flask-application-in-a-docker-container-3l28</link>
      <guid>https://forem.com/anquangman/debug-flask-application-in-a-docker-container-3l28</guid>
      <description>&lt;p&gt;"How can I debug an Flask application that runs inside Docker's container?" If you're asking the same question then this post might help you to answer that question.&lt;/p&gt;

&lt;p&gt;Note: This is my very first post to Dev.to. Please pardon my English.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debugger:
&lt;/h2&gt;

&lt;p&gt;We spend most of you time to track down and kill bugs, debugger can help you nothing but do it faster. The primary advantage to a true debugger is the ability to set breakpoints and begin to step through the code line by line or function by function, and see how the variables change, until you identify what is not working as you expected.&lt;br&gt;
You can debug a python web application such as Flask, Django, or event just a python script in VSCode. To start debug you app you need to install Python extension and write a description to tell VSCode to launch the project.&lt;br&gt;
Install &lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-python.python"&gt;Python extension&lt;/a&gt; and start debugging.&lt;/p&gt;
&lt;h2&gt;
  
  
  Docker
&lt;/h2&gt;

&lt;p&gt;Docker has become one of the most popular technologies in Devops. It provide many benefits such as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Consistent Environment: Containers give developers the ability to create consistent environments that are isolated from others. You can make several containers that are fit the purpose, you can have three containers for development, one for testing, and one for production&lt;/li&gt;
&lt;li&gt;Version Control: Containers can also include software dependencies needed by the application, such as specific versions of programming language and other software libraries.&lt;/li&gt;
&lt;li&gt;Run Anywhere: It doesn’t matter the vehicle, it perfectly works everywhere.&lt;/li&gt;
&lt;li&gt;Isolation: Dependencies or settings within a container will not affect any installations or configurations on your computer, or on any other containers that may be running.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Docker and Debugger
&lt;/h2&gt;

&lt;p&gt;Dockerize an app is fun but It will be a paint to debug. Good new is you still can debug an app inside a container with VSCode but it requires more works. You will need a python library called "ptvsd" for the job.&lt;/p&gt;

&lt;p&gt;You can check out &lt;a href="https://github.com/anquangman/debugging-dockerized-flask-app"&gt;my sample code in Github&lt;/a&gt;&lt;br&gt;
I'm using &lt;em&gt;docker version 19.03.1&lt;/em&gt; and &lt;em&gt;docker-compose version 1.24.1&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I will assume that you main application file is called &lt;code&gt;app.py&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;First&lt;/strong&gt;, add these lines to the top of &lt;code&gt;app.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;ptvsd&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;ptvsd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enable_attach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'0.0.0.0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;ptvsd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_for_attach&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will let you run attach debug to port 4000, you can feel free to change the port but make sure you expose it to you local.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second&lt;/strong&gt;, start you services&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Third&lt;/strong&gt;, add this config into the &lt;code&gt;launch.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Attach Debug&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;python&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;request&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;attach&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;port&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;host&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.0.0.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pathMappings&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:[{&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;localRoot&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path to where the app.py lives on your local&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;remoteRoot&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path to where the app.py lives in app container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;port&lt;/em&gt; value has to match the exposed port in your container.&lt;/p&gt;

&lt;p&gt;Show time&lt;br&gt;
&lt;a href="https://i.giphy.com/media/RJ19FBuZmJ15YGa6mY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/RJ19FBuZmJ15YGa6mY/giphy.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to leave a comment if you need help.&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>flask</category>
      <category>debug</category>
    </item>
  </channel>
</rss>
