<?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: Rop Ronald</title>
    <description>The latest articles on DEV Community by Rop Ronald (@rop_ronald).</description>
    <link>https://dev.to/rop_ronald</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%2F516581%2F32c4899f-e77f-47ca-904b-98013b3e449d.jpg</url>
      <title>DEV Community: Rop Ronald</title>
      <link>https://dev.to/rop_ronald</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rop_ronald"/>
    <language>en</language>
    <item>
      <title>Getting started with FastAPI and Docker</title>
      <dc:creator>Rop Ronald</dc:creator>
      <pubDate>Sat, 13 Nov 2021 20:00:16 +0000</pubDate>
      <link>https://dev.to/rop_ronald/getting-started-with-fastapi-and-docker-1g5f</link>
      <guid>https://dev.to/rop_ronald/getting-started-with-fastapi-and-docker-1g5f</guid>
      <description>&lt;p&gt;Today we are going to get started with FastAPI and Docker, but first let’s start by learning some basics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is FastAPI?&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.&lt;/em&gt; It is a perfect python frameworks for building RESTful APIs as it is fast and ease to use and learn. &lt;br&gt;
It takes advantage of Python type hints for parameter declaration which enables data validation (via Pydantic) and OpenAPI/Swagger documentation.&lt;br&gt;
It is built on top of Starlette, which supports the development of asynchronous APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker&lt;/strong&gt; is an open source platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. &lt;br&gt;
Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host. You can easily share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker file&lt;/strong&gt; is a text file that contains all the commands used to create an image.&lt;br&gt;
&lt;strong&gt;Docker image&lt;/strong&gt; is a read-only template with instructions for creating a Docker container.&lt;br&gt;
&lt;strong&gt;A Docker container&lt;/strong&gt; is a runnable instance of an image. It is isolate from other containers and from its host machine.&lt;/p&gt;

&lt;p&gt;That’s a brief introduction to docker.&lt;/p&gt;

&lt;p&gt;Next we are going to dockerize FastAPI application.&lt;/p&gt;

&lt;p&gt;First we are going to create a folder where our application will be placed. We will name our folder “FastAPI_Docker”&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;With our directory ready we’ll move inside it and create a virtual environment. &lt;strong&gt;NB&lt;/strong&gt;. These commands are for windows operating system.&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 venv venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to activate our virtual environment by issuing 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;venv\Scripts\activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After activating our virtual environment it’s now time to install FastApi and its dependencies. FastAPI uses uvicorn as a server to run its code. &lt;br&gt;
Execute the following commands.&lt;br&gt;
&lt;/p&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install “uvicorn[standard]”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we need to export our dependencies to requirements.txt file to be used later in docker.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Its now time to write our first FastAPI code. &lt;br&gt;
Create a file main.py and have these lines of codes in it. &lt;br&gt;
&lt;code&gt;main.py&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HDgM_m7Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xowwcu239bmudvvpd7vx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HDgM_m7Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xowwcu239bmudvvpd7vx.png" alt="Image description" width="880" height="716"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it's time to create our dockerfile to be use to create the image.&lt;br&gt;
&lt;code&gt;Dockerfile&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A8dbQMdP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odr1j5xhvylcdqpob48l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A8dbQMdP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odr1j5xhvylcdqpob48l.png" alt="Image description" width="880" height="716"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets build our docker image and name it &lt;em&gt;fastapi&lt;/em&gt;&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 fastapi .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After it is done building we can run it with the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 8000:8000 -ti fastapi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lc8tr2zd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksjgc12643zdc1pwue74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lc8tr2zd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksjgc12643zdc1pwue74.png" alt="Image description" width="624" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we navigate to the url of &lt;em&gt;&lt;a href="http://127.0.0.1:8000"&gt;http://127.0.0.1:8000&lt;/a&gt;&lt;/em&gt; in our browser we can see the output below which is our up running from a docker container.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Uvc43Fs0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zc16dmo7a1o6m2gmut9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Uvc43Fs0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zc16dmo7a1o6m2gmut9m.png" alt="Image description" width="624" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wF7HDQxL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/un2s1itcpqjdx7fi381n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wF7HDQxL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/un2s1itcpqjdx7fi381n.png" alt="Image description" width="624" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
