<?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: kOr3s</title>
    <description>The latest articles on DEV Community by kOr3s (@k0r3s).</description>
    <link>https://dev.to/k0r3s</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%2F717971%2Fd0a58553-69ca-4fae-8cee-7eb71fe20899.png</url>
      <title>DEV Community: kOr3s</title>
      <link>https://dev.to/k0r3s</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/k0r3s"/>
    <language>en</language>
    <item>
      <title>Getting Started With Fast API and Docker</title>
      <dc:creator>kOr3s</dc:creator>
      <pubDate>Mon, 15 Nov 2021 09:25:12 +0000</pubDate>
      <link>https://dev.to/k0r3s/getting-started-with-fast-api-and-docker-1ldn</link>
      <guid>https://dev.to/k0r3s/getting-started-with-fast-api-and-docker-1ldn</guid>
      <description>&lt;h1&gt;
  
  
  FASTAPI
&lt;/h1&gt;

&lt;h2&gt;
  
  
  What is fastAPI
&lt;/h2&gt;

&lt;p&gt;FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints to validate, serialize, and deserialize data, and automatically auto-generate OpenAPI documents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why fastAPI
&lt;/h2&gt;

&lt;p&gt;FastAPI has is one of the latest python api's for web development. It has some of this features:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. It is indeed fast
&lt;/h4&gt;

&lt;p&gt;It is fast when we compare it to other major Python frameworks like Flask and Django.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Support for asynchronous code
&lt;/h4&gt;

&lt;p&gt;This is one of exciting feature of FastAPI that it supports asynchronous code out of the box using the async/await Python keywords.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Very short development time
&lt;/h4&gt;

&lt;p&gt;It takes like 8 lines of code to comeup with a hello, word program&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Easy testing
&lt;/h4&gt;

&lt;p&gt;It has a feature of test driven development with the help using the &lt;a href="https://fastapi.tiangolo.com/tutorial/testing/"&gt;TestClient&lt;/a&gt; provided by fastAPI.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. Excellent documentation
&lt;/h4&gt;

&lt;p&gt;It has one of the easiest &lt;a href="https://fastapi.tiangolo.com/"&gt;documentation&lt;/a&gt; that one can understand.&lt;/p&gt;

&lt;h4&gt;
  
  
  6. Easy deployment
&lt;/h4&gt;

&lt;p&gt;You can easily deploy your FastAPI app via Docker using FastAPI provided &lt;a href="https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker"&gt;docker image&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Python 3.6+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installation of fastapi on windows.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install fastapi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installation on Mac and Linux
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install fastapi uvicorn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;First create a folder at your own location of choice&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir foldername &amp;amp;&amp;amp; cd foldername
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;create a virtual environment inside the folder you created&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m venv env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;activate the virtual environment created called env&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source env/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a python file with the following code and save as myapp.py&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="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Depends&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;root&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Hello World!'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then to run the server, we use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; uvicorn myapp:app --reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To know it it works with no error, you will see this in your commandline&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [84109] using statreload
INFO:     Started server process [84111]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to open in browser, use the following link&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://127.0.0.1:8000/docs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you will see the following output&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U3LqZZtd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8thigl8xfeqj8m4ogec0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U3LqZZtd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8thigl8xfeqj8m4ogec0.png" alt="Image description" width="880" height="402"&gt;&lt;/a&gt;&lt;br&gt;
before going to docker, we can create a requirement.txt file that has all the tools that you have installed in one place by running;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 freeze &amp;gt; requirement.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  DOCKER
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; is an open source platform for building ,deploying and managing containerized applications.&lt;br&gt;
Docker container is a virtualized runtime environment that provides isolation capabilities for separating the execution of applications from the underpinning system&lt;/p&gt;
&lt;h3&gt;
  
  
  Why docker
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;speed - docker has both execution speed,startuo speed and operational speed.&lt;/li&gt;
&lt;li&gt;consistency - Build an image once and use it any where. The same image that is used to run the tests is used in production. This avoids the works in my machine problems.&lt;/li&gt;
&lt;li&gt;Flexibility - Containers are portable. Well to an extent (as long as the host is running some form of linux or linux vm).&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Building a docker image
&lt;/h4&gt;

&lt;p&gt;Create a docker file and save with this commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; python:3.9&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /code&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; ./requirements.txt ./requirements.txt&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--no-cache-dir&lt;/span&gt; &lt;span class="nt"&gt;--upgrade&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; /code/requirements.txt

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; ./app &lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then build your image&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t myimage ./
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>beginners</category>
      <category>fastapi</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
