<?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: Duy Nguyen Hoang</title>
    <description>The latest articles on DEV Community by Duy Nguyen Hoang (@duynguyenhoang).</description>
    <link>https://dev.to/duynguyenhoang</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%2F216943%2F377a4969-0791-495d-a353-d1138dd07ebd.jpeg</url>
      <title>DEV Community: Duy Nguyen Hoang</title>
      <link>https://dev.to/duynguyenhoang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/duynguyenhoang"/>
    <language>en</language>
    <item>
      <title>Setup debugger for your Fastapi project with Vscode and Docker compose</title>
      <dc:creator>Duy Nguyen Hoang</dc:creator>
      <pubDate>Fri, 08 Jul 2022 20:04:43 +0000</pubDate>
      <link>https://dev.to/duynguyenhoang/setup-debugger-for-your-fastapi-project-with-vscode-and-docker-compose-4i23</link>
      <guid>https://dev.to/duynguyenhoang/setup-debugger-for-your-fastapi-project-with-vscode-and-docker-compose-4i23</guid>
      <description>&lt;p&gt;At Trustyou, we are dockerizing our Fastapi projects for all  the steps from linting, mypy, black... Moreover we use docker-compose to run some tests as well as the application locally. And there is a question raised, how could we to debug our application? &lt;/p&gt;

&lt;p&gt;Our requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Running Fastapi application using docker-compose&lt;/li&gt;
&lt;li&gt;Be able to debug application&lt;/li&gt;
&lt;li&gt;Auto reload&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are some guides out their but in this tutorial I would like to simplify it and go step by step with you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Setup "Run and Debug"
&lt;/h4&gt;

&lt;p&gt;We need to create a configuration file &lt;code&gt;.vscode/launch.json&lt;/code&gt;. This file contains information for &lt;code&gt;Run and Debug&lt;/code&gt; tab. We can add this file to git but ignoring all order files in &lt;code&gt;.vscode&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;File: .vscode/launch.json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/code"
                }
            ]
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create docker-compose.debug.yml file
&lt;/h4&gt;

&lt;p&gt;This file is quite similar to our docker-compose.yml, except it expose port 5678. This port is used by &lt;a href="https://github.com/microsoft/debugpy" rel="noopener noreferrer"&gt;debugpy&lt;/a&gt;, vscode debugger will communicate with container via this port.&lt;/p&gt;

&lt;p&gt;File: docker-compose.debug.yml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.4'

services:
  fastapi-vscode-debug-setup:
    image: fastapi-vscode-debug-setup
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - ./hello_world:/code/hello_world
    command: ["sh", "-c", "pip install debugpy -t /tmp &amp;amp;&amp;amp; python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn hello_world.main:app --host 0.0.0.0 --port 8000 --reload"]
    environment:
      - APP_MESSAGE=Hello debugger
    ports:
      - 8000:8000
      - 5678:5678
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After we have everything is set up. Now it time to debug our application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debugging
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Start docker-compose debugging by: Right click on &lt;code&gt;docker-compose.debug.yml&lt;/code&gt; file and click &lt;code&gt;Compose Up&lt;/code&gt;. You can also run command &lt;code&gt;docker compose -f docker-compose.debug.yml up&lt;/code&gt;. They are both the same&lt;/li&gt;
&lt;li&gt;Go to &lt;code&gt;Run and Debug&lt;/code&gt;, select &lt;code&gt;Python: Remote Attach&lt;/code&gt; and click &lt;code&gt;Start Debugging&lt;/code&gt; button &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%2Fxyb0305es5n3bw87iha8.png" alt="Start debugging"&gt;
&lt;/li&gt;
&lt;li&gt;If there is nothing wrong, the debugger will connect to container and you can add a break point to test it 🎉. For example, I added a breakpoint to &lt;code&gt;main.py&lt;/code&gt; and access &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt;, you would expect something similar to image happens &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%2Fuohzp9ysi26oiqg9i6ua.png" alt="Debug with breakpoint"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to stop debugging, Right click on &lt;code&gt;docker-compose.debug.yml&lt;/code&gt; file and click &lt;code&gt;Compose Down&lt;/code&gt; or stop docker compose from your termimal&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This can be applied to not only Fastapi/Python project but also for other projects. You could check them &lt;a href="https://code.visualstudio.com/docs/containers/docker-compose" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check all the code in this repository &lt;a href="https://github.com/duynguyenhoang/fastapi-vscode-debug-setup" rel="noopener noreferrer"&gt;https://github.com/duynguyenhoang/fastapi-vscode-debug-setup&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/docs/containers/debug-common" rel="noopener noreferrer"&gt;https://code.visualstudio.com/docs/containers/debug-common&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/docs/containers/debug-python" rel="noopener noreferrer"&gt;https://code.visualstudio.com/docs/containers/debug-python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/docs/containers/docker-compose" rel="noopener noreferrer"&gt;https://code.visualstudio.com/docs/containers/docker-compose&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>fastapi</category>
      <category>vscode</category>
      <category>docker</category>
      <category>dockercompose</category>
    </item>
  </channel>
</rss>
