FastAPI in Containers - Docker | FastAPI + Docker + Compose

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another exciting session i'm your host mesh gary you're watching me on youtube now and today i'm gonna take you through how to containerize your fast api with using docker and docker compose so without wasting any time let's steep dive into it so i'm gonna create a new folder on my desktop and i'm gonna name it uh let's say fast api or yeah first api will be better i guess and simply drag your folder here and we'll be now using python uh i'm assuming you already have a python available inside your system if you don't have it what you can do is you can now grab your python before that we also need a docker so please search for docker and you can get your docker from here search for docker desktop you can get your docker from here after that you will be using docker compose so what you can do is you can search for docker compose quickly hit enter and you can see you can click here on docker compose and here we go now you can grab your docker compose from here okay these two things you have to have available inside your system after that um once you have python available please make sure um that which version you are using okay so i already have python 3 available so i'll be using python 3 have an m virtual env space v e and v command space dot ah so sorry uh it will generate the virtual environment over here okay now i need to activate so source okay then folder name vnv slash bin slash activate okay hit enter this is it now we are we can proceed with the further things like i can create a main dot pi let's say where i'll be using pip install fast api quickly and uv got okay so this is a tag is this two things are required for proceeding so after that we can also use init dot pi okay so it's all your choice uh so from past api uh import passed api okay then app is equal to first api and after that we'll be using first api now what app dot get and now simply hello world that i'm trying to create okay so let's say root okay and or whatever it may be you can name it whatever like you prefer to use so in return you can return the message or message will be hello world or hello okay this is it this is the best uh simplest example we can say okay so after that what we gonna do is we are gonna create a dockerfile okay quickly so in that we need to define the image image is something that you can choose from docker hub like a python or any specific version okay or any build type like 2.3 i have an alpine or something like that so i'll stick to normal python version i'm assuming it will written three point something in three pointer in something so after that you need to define work directly where you can uh all the things that we are gonna do will uh it will create a app directly inside your container and inside that app directory only all the further commands will be executed so here you need to define uh first we need to grab the uh requirements.txt which is the uh the main file where all the dependencies uh that we can uh install from actually so for that we have a command like pip freeze okay and r e q u i r e m n t s dot t x t okay so it will generate this requirements.txt where all the dependencies are given after that what you can do is you can quickly go here and you can copy this thing from here okay so make sure to name it like let me grab this thing okay so yeah so from here to here okay like inside your core root directory after that you can run the command the command is very easy i guess so pip install hyphen r and the name of your file like this okay so yeah this is it i guess after that we can proceed to the further things like copy all the things from here to here okay so for that you can also use uh getting off now dot docker ignore actually okay to ignore few things like we and we let's say okay and likewise you can ignore the things that you don't wanna um copy or something like that so after that you can expose the port exposes something that allows you to of course expose the port you can use whatever port like you wanna prefer to use it's all your choice no worries after that what we can create is uh the docker compose file okay docker compose.yaml which will be combination of let's say there are lots of containers are out there and you want to merge all container at once so that's why we use docker compose uh if you we are using back end mongodb sql so it's easy to build everything with the single command so for that we need a version so i'm assuming 3.8 is okay after that you can define services you can name it order it may be let's say app or something like that after that you can define the build path okay build path is of course the current directory where the location of docker file we need to define after that you can define the ports where inside ports you can define uh like iphone 8000 colon 8000 after that what you can do is you can provide the name as well if you want or you can provide the command as well okay so command uh you guys are familiar with command right uv con base uh like let's say app is my directory dot the file name colon the function name okay i have an iphone host you can define if you want zero point zero point zero point zero have another boot you can define if you want by default eight thousand is the port so this is it then we can proceed with the docker compose docker compose up okay here we go and it will take a while to grab the dependencies and build the things okay so hopefully i'm missing one more thing over here okay so let me grab quickly so uh nothing is missing almost everything is fine okay now you can see if i go here if i search here you will get this message hello so everything is working fine thank you very much all of you guys for being with me supporting me all the time it's been pleasure to be here let me know what you want from my side definitely i'll make the now useful videos just like that so and don't forget to subscribe my channel if you enjoyed okay so thank you so much all of you guys for being with me thank you very much have a great day
Info
Channel: Mahesh Kariya
Views: 11,061
Rating: undefined out of 5
Keywords:
Id: VRMfz8srFR8
Channel Id: undefined
Length: 8min 40sec (520 seconds)
Published: Fri Mar 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.