<?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: Magoto_devv</title>
    <description>The latest articles on DEV Community by Magoto_devv (@kenny_me).</description>
    <link>https://dev.to/kenny_me</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%2F744272%2F51690526-23f3-45e2-a3fd-71178f3656b8.jpeg</url>
      <title>DEV Community: Magoto_devv</title>
      <link>https://dev.to/kenny_me</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kenny_me"/>
    <language>en</language>
    <item>
      <title>Getting Started With FastAPI and Docker</title>
      <dc:creator>Magoto_devv</dc:creator>
      <pubDate>Sun, 14 Nov 2021 07:11:39 +0000</pubDate>
      <link>https://dev.to/kenny_me/getting-started-with-fastapi-and-docker-1hd7</link>
      <guid>https://dev.to/kenny_me/getting-started-with-fastapi-and-docker-1hd7</guid>
      <description>&lt;p&gt;For those of us in the tech industry, we may have come across the phrase 'API' several times. It is important to clarify what an API means. An API is the gateway to your application, the interface that users and other services can use to interact with it. Traditionally, building APIs has been a challenging task until recently when web frameworks like FastAPI were introduced. FastAPI is a modern, fast and high-perfomance web framework for building APIs with Python 3.6+ based on standard Python type hints. Today, we are going to take a deep dive into how FastAPI works and also learn what Docker is.&lt;/p&gt;

&lt;p&gt;Since I have already outlined what FastAPI is, I shall discuss a few thing about Docker. Docker is an open source containerization platform which enables developers to package applications into containers, standardized executable components combining application source code with the operating system libraries and dependencies needed to run that code in any environment. &lt;/p&gt;

&lt;p&gt;A Docker container is a virtualized runtime environment &lt;br&gt;
that provides isolation capabilities for separating the execution of applications from the underpinning system.  Containers simplify delivery of distributed applications, and have become increasingly popular as organizations shift to cloud-native development and hybrid multi-cloud environments. It is worth noting that developers can create containers without Docker, but Docker makes it easier, simpler, and safer to build, deploy, and manage containers. A docker container can be viewed as an instance of a Docker image.&lt;/p&gt;

&lt;p&gt;A docker image is a read-only, inert template that comes with guidelines for deploying containers. In Docker, everything basically revolves around images. An image entails a collection of files (also called layers) that pack together all the necessities such as dependencies, source code, and libraries needed to set-up a completely functional container environment.&lt;/p&gt;

&lt;p&gt;Going back to FastAPI, the web framework is extremely fast thanks to the out of the box support of the async feature of Python 3.6+. As such, FastAPI is the recommended tool to use for the latest versions of Python. Examples of tech giants using FastAPI to build their applications are Microsoft, Uber and Netflix.&lt;/p&gt;

&lt;p&gt;When developing Python apps, it is advisable to use a virtual environment to speed up and clean your overall project workflow. Below, I will show you how to dockerize a simple FastAPI application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing Virtual Environment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1 - Installing a virtual environment&lt;/strong&gt;&lt;br&gt;
pip3 install virtualenv&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2 - Creating virtual environment&lt;/strong&gt;&lt;br&gt;
python3 -m venv luxenv&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3 - Activating the virtual environment&lt;/strong&gt;&lt;br&gt;
source luxenv/bin/activate&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4 - Installing FastAPI&lt;/strong&gt;&lt;br&gt;
pip3 install fastapi&lt;br&gt;
pip3 install uvicorn&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5 - creating requirements.txt file&lt;/strong&gt;&lt;br&gt;
pip3 freeze &amp;gt; requirements.txt&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ensure you have the app.py application.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;from fastapi import FastAPI&lt;/p&gt;

&lt;p&gt;app = FastAPI()&lt;/p&gt;

&lt;p&gt;@app.get("/")&lt;br&gt;
def read_root():&lt;br&gt;
    return { "Lux app": "Welcome to Lux app" }&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6 - create Dockerfile with the following code:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FROM python:3.6&lt;/p&gt;

&lt;p&gt;COPY . /src&lt;/p&gt;

&lt;p&gt;COPY ./requirements.txt /src/requirements.txt&lt;/p&gt;

&lt;p&gt;WORKDIR src&lt;/p&gt;

&lt;p&gt;EXPOSE 8000:8000&lt;/p&gt;

&lt;p&gt;RUN pip install -r requirements.txt&lt;/p&gt;

&lt;p&gt;CMD [ "python", "app.py" ]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7 - Build Docker image called demo&lt;/strong&gt;&lt;br&gt;
docker build -t luxapp .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8 - Run Docker image called demo&lt;/strong&gt;&lt;br&gt;
docker run -p 8000:8000 -t -i luxapp&lt;/p&gt;

&lt;p&gt;Once you are done with that, you can visit [&lt;a href="http://127.0.0.1:8000/"&gt;http://127.0.0.1:8000/&lt;/a&gt;] where you will see the app details displayed on your browser.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are a few additional tips for you:&lt;/strong&gt;&lt;br&gt;
docker system prune --&amp;gt; cleans up any resources (images, containers, volumes, networks) that are dangling. Dangling means not associated with a container&lt;/p&gt;

&lt;p&gt;docker system prune -a --&amp;gt; removes any stopped containers and all unused images (not just dangling mages)&lt;/p&gt;

&lt;p&gt;docker images --&amp;gt; lists all images&lt;/p&gt;

&lt;p&gt;docker rmi image_id image_id --&amp;gt; deletes docker images with the given image_id&lt;/p&gt;

&lt;p&gt;docker run --rm --&amp;gt; automatically deletes a container that isn't going to be used again&lt;/p&gt;

&lt;p&gt;docker ps -a -f status=exited --&amp;gt; lists all exited containers&lt;/p&gt;

&lt;p&gt;docker rm $(docker ps -a -f status=exited -q) --&amp;gt; removes/deletes all exited containers&lt;/p&gt;

&lt;p&gt;docker push --&amp;gt; push to docker hub&lt;br&gt;
&lt;strong&gt;Incase of an error when pushing to the docker hub, try the following commands:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;$ docker login&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;$ docker tag first-image {docker-hub-username}/{default-repo-folder-name}:first-image&lt;/p&gt;

&lt;p&gt;$ docker push {docker-hub-username}/{default-repo-folder-name}:first-image&lt;/p&gt;

&lt;p&gt;e.g. I have a public repository like this; magoto/luxapp so commands would be:&lt;/p&gt;

&lt;p&gt;$ docker tag first-push magoto/uxapp:first-push&lt;/p&gt;

&lt;p&gt;$ docker push magoto/luxapp:first-push&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good Luck!&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/kmagoto"&gt;My GitHub&lt;/a&gt;&lt;/p&gt;

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