<?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: Joachim Watkidog</title>
    <description>The latest articles on DEV Community by Joachim Watkidog (@j0kim).</description>
    <link>https://dev.to/j0kim</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%2F748078%2F12cfd686-edff-4db6-a46e-892fa13abe43.jpg</url>
      <title>DEV Community: Joachim Watkidog</title>
      <link>https://dev.to/j0kim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/j0kim"/>
    <language>en</language>
    <item>
      <title>Getting started with FastAPI and Docker</title>
      <dc:creator>Joachim Watkidog</dc:creator>
      <pubDate>Sun, 14 Nov 2021 10:47:26 +0000</pubDate>
      <link>https://dev.to/j0kim/getting-started-with-fastapi-and-docker-204j</link>
      <guid>https://dev.to/j0kim/getting-started-with-fastapi-and-docker-204j</guid>
      <description>&lt;p&gt;In this beginner’s guide, I will take you through the basic steps to get your first application running using FastAPI and Docker. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table of contents&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;FastAPI&lt;/li&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;li&gt;Building a FastAPI application with Docker&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  FastAPI
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;FastAPI&lt;/strong&gt; is a fast, modern, high performance, open source Python web framework used to build APIs with Python 3.6+. It is a minimalistic framework and quite new with a smaller community compared to Django and Flask but when it comes to performance, it is the fastest by far. It supports asynchronous programming, which is a plus. To learn more about fastAPI, visit their official docs &lt;a href="https://fastapi.tiangolo.com/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Docker&lt;/strong&gt; is a containerization platform. It enables you to build, run and ship your application, packages your code and all its dependencies enabling your application to run consistently across different computing environments.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;container&lt;/strong&gt; is a virtualized environment for running an application. It includes everything needed for an application to run.&lt;/p&gt;

&lt;p&gt;Docker uses the client-server architecture. The client component communicates with the server (the Docker Engine) using a RESTful API. The Docker Engine builds and runs the Docker containers. &lt;/p&gt;

&lt;h2&gt;
  
  
  Building your first FastAPI and Docker application
&lt;/h2&gt;

&lt;p&gt;In this section, we are going to dockerize a simple python application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Requirements
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Linux or Mac computer. You can also use Windows but some commands are different.&lt;/li&gt;
&lt;li&gt;Python 3.6+&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We shall start by installing a virtual environment in which we will create our application.&lt;/p&gt;

&lt;p&gt;Install a virtual environment - virtualenv&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Navigate to a directory of choice and create the virtual environment.&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 virtualenv hello-app-env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Activate the virtual environment&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Change directory into the virtual environment created and install fasAPI and uvicorn. We'll use uvicorn to run our application.&lt;br&gt;
&lt;/p&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;p&gt;Create a requirements.txt file. This lists all the packages required by our application.&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; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we are all set up. Let's create a python file &lt;code&gt;main.py&lt;/code&gt; in your &lt;code&gt;src&lt;/code&gt; directory and add the following code:&lt;br&gt;
&lt;code&gt;/src/main.py&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;from fastapi import FastAPI

app = fastAPI()

@app.get("/")
def index():
    return {"Greeting": "Hello World!"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; use @app.post() if you're accepting sensitive data from the user like usernames and passwords.&lt;/p&gt;

&lt;p&gt;Create a Docker file called &lt;code&gt;Dockerfile&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;FROM python:3.9

COPY ./src /app/src
COPY requirements.txt /app

WORKDIR /app

RUN pip3 install -r requirements.txt

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host=0.0.0.0", "--reload"]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;--reload&lt;/code&gt; will automatically update changes when made so you won't have to re-run the container.&lt;/p&gt;

&lt;p&gt;To build your Docker image, open your terminal and enter:&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 hello-world-env .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the docker 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 run -p 8000:8000 --name hello-app hello-world-env 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In this case, hello-app is the name I'm giving to the container, hello-world-env is the image&lt;/p&gt;

&lt;p&gt;To verify that the image and container exist, run these commands in your terminal. The directory you are in does not matter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker image ls
&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;docker container ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;docker run&lt;/code&gt; command will spin up your local server and you can view your results through this URL &lt;a href="http://localhost:8000"&gt;http://localhost:8000&lt;/a&gt; or &lt;a href="http://127.0.0.1:8000"&gt;http://127.0.0.1:8000&lt;/a&gt; ``&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;As you can see, it's quite easy to create an application with FastAPI and also dockerize it. And as you know by now, you learn code best by trying it out yourself through building stuff. So try and create another application and see if you can have something visible at the end. &lt;/p&gt;

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