FastAPI in Containers - Docker + MongoDB | DockerHub | 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 spanish car here you're watching me on youtube now and today i'm gonna take you through how to dockerize your fast api or any flask or django api and how to create an image and how to create container and how to create a docker compose file everything that you needs to know about uh dockerize your cost api mongodb post sql that you're going to learn in this session so without wasting any time let's deep dive into it so you have to have one editor available before proceeding so we can quickly create a new folder over here we can name it whatever it may be as usual i'm gonna name it app this time just cop that drag your folder on the top of your waste code open your terminal from here or you can use command j okay if you're on mac or control plus backtick if you're on windows okay and quickly uh do one thing uh like create a docker file first so uh after that uh quickly if you have a python 3 check i have python 3 so what i'll do is i'll use python 3 okay half an amp virtual envy of env so it's going to great vnv over here and this is it after that i'm going to create a app folder over here where inside that i'm going to create main dot pi quickly where i'm going to implement my first api so for that i need the dependencies so i'm going to do one thing python 3 or we can first i need to enable my virtual environment so what that i'm gonna use slash vnv okay tab okay slash i'm gonna use so slash bin slash activate okay hit enter no search file or directory because we are using slash over here remove it this is done okay cool so now what we are gonna do is we are gonna install the dependencies so pip install i just need a fast api for now and uv gone here we gone this is it i guess hit enter and now before proceeding you have to have docker available okay so here i'm gonna write the program from fast api import fast api and app is equal to first api uh after that we are gonna do add the rate okay gate and create your oh written something hello what is it finish this is the simplest uh possible hello world program we have made in fast api after that do one thing create a init file empty in it file okay so yeah this is it and after that we need to create pip breeze what we can say requirements.txt i hope i have made spelling mistake now this will go with the same it will generate the requirement.txt where whatever the dependencies we have installed you can see over here and in our dependencies so yeah this is it now inside dockerfile we can uh simply write from uh what we can say on python3 okay so you can define python and version 3.9 let's say okay and after that point zero let's say and hyphen you can use alpine built or to save the time okay uh hit enter and after that you can specify your work directory so let's use the code let's say and after that you can now grab all the things or you can do one thing like slash you can say requirements.txt let me quickly uh rename it okay uh slash so where i'm gonna grab this thing so uh slash code slash uh we can say here okay so after that we are gonna run this thing we need to uh hit run command to install the dependencies or you can do one thing to define no cash dir or upgrade as well if you want like that it's all up to you ctrl b to close this thing after that you can grab rest of the things uh like dot slash app folder i have that i'm gonna get in app okay so i'm gonna copy this app folder over there okay and this is it after that we can run this thing uh in run we need to define uv con okay now after that we can say app.main okay colon app of course have an iphone reload you can use it's all up to you if you want you can use reload as well but before that we need to define host first okay so host is something uh uh where you can define 0.0.0.0 for localhost okay and here i think we can define port as well so default port we we are assuming that we'll use port 80 over here so this is it now what you can do is you can quickly install this uh plugin over here you can see so i don't have any containers for now so i'm not able to see anything over here so you can grab this from here which is very helpful just search for docker and you can get it from here so you can push your docker images to docker hub so it's very useful now this is the tag is so what we can do is we're gonna create a docker image first so so for that we use command docker build space dot okay it's gonna create a new image but before that we need to define a tag okay so tag is something that is gonna we are gonna name it the name of image whatever it may be let's say name image name or let's say app or whatever it may be that you can choose so what it will do it will find the docker file over here so once it gets the docker file it will create the image okay okay one more thing that we have missed this is something inside this code okay so if you need to do one thing like it should be like this so this is it after that we'll quickly rerun the command and now here we go it will take some time till then what we can do is we can now go here quickly let me resize this okay so till then we'll create a docker compose okay so [Music] we can okay so docker compose is something that we use when you have more than one containers let's say if you have a react app angular view app okay and you wanna uh compose all the images together okay let's say if you're working on mongodb postgres okay so there might be a chance that uh you're working on let's say mongodb so you need to define a version first of course so currently we are using let's say 3.8 then you can define your services over here inside services you can define your app let's say okay so app is my current app that is i'm working on okay so inside that now we need to define a build okay so build where you need to define a path of docker files so i have docker5 here okay so i'll say dot dot slash or something like that after that you can define the ports codes something uh that we have defined over there which was 80 okay yes okay after that uh let me quickly change this thing uh okay so after that uh it is depends on okay uh some other container let's say let's say there is or post receive we are working on so you can write uh here let's say let me create a db for store we can write uh depends on directly over here as well okay and service name service name is something you can write db that it depends on db and db after that you can create like i need a db uh where you can define let's say image okay image post list let's say okay so this i don't need this much of things just remote okay just go with the first so okay and after that you need to define boards which is 27017 you can define environment variables as well which is for uh username id password if you wanna set okay like it depends but for now this is very minimal approach that we will follow okay where i just need these two things now um whenever you are working on any uh in the database so you need to persist the data because every time when we restart the container it will automatically uh flash the data so if you want to persist the data you can define volumes volumes is something like you can create a local repository okay now it will create a repository over here okay if i write something like that so it will create a data repository over here and all the data which will create inside that container okay it's going to preserve here so next time it will automatically get volume from here so this is how we have connected the mongodb so now you can access the seven two seven zero one seven port inside here inside app as well [Music] okay uh one more thing okay uh this is done okay have made one change okay instead of alpine version i'm using this one already so you can check there on official uh dock uh there are few versions for that alpine version is available okay so this is it i guess so if i go here and if i click here on docker if i try to refresh this thing i don't have any containers now we have just created one image so what you can do you can go here in images and inside that you will get this app over here okay the latest build one minute ago what will do if you want to create a container you can quickly hit this command or docker run okay to run the container hyphen have a name to provide the name of container so we'll say app app container or whatever it may be after that you can provide that mode like in detach mode or in interactive mode okay interactive it means you can you you can see the logs over here okay in detach mode it will run in a background after that you can provide the port okay same that we have defined over here okay like these all the things we are doing from command line so two seven zero one seven colon two seven zero one seven okay and the name of image that we have made app okay so app this is it enter it will create a container you can see here content is running if i uh go here okay um so here what we can do is we can quickly search for localhost okay so there is nothing is running over here okay cool i hope i'm missing something okay it was it was a 270 let me delete this thing first okay so how the port was wrong it is support of homework so docker uh ps you can write to see the running images okay running containers and if you wanna delete this thing you can write docker rm and paste this id or any three letters first three or letters hit enter again okay uh you cannot remove because this is running so you can write force here forcefully turn this off okay so now if i do docker ps you can see there is nothing now again i'll try to rebuild this thing okay uh sorry to run this thing again but this time i'll choose port 80 okay so 8080 again hit enter this is done this is running over here okay if you right click you can see you can see logs from here as well open in browser as well so if i click here it will automatically open in browser and you can see hello world over here okay now it's time to use the docker compose instead of this thing okay you can remove this from here as well so you can first you need to stop this thing otherwise uh it won't like uh or you can remove this thing so you need to define individual antennas this one and okay so this is the power of using a gui okay it's all up to you what you use after that you can search for docker compose okay so after that you will get this very uh you can click here on get started okay so from here you can let me search docker compose install docker compose okay this is available for the system so you can grab uh your package from here okay after that once you have it you can search for docker compose f1 version okay so same thing that we are gonna do with docker compost now now i'm gonna comment this out to run this thing only okay now you can provide the container name as well here if you want okay container name like app again okay so this is my container name so i'm gonna run this thing docker compose up it's going to uh create a it will run all the things again okay this is done uh you can see the image is also created over here and new app is running over here if i go again i hopefully this will work again okay this is running perfectly fine if i need mongodb you can write db okay here otherwise you can write other things as well so now what i you you can do is you can go to the docker hub quickly okay to publish this uh image that you have just made okay like you can click here you can sign in okay so you need to create one let me close this things which is not needed okay so you can create a new repository over here okay just name it okay pi test or fast api let's say quickly okay here you can see you just need to define this thing uh you need to tag that particular thing okay so after that let's create quickly okay and uh this is it i guess and if i go here here let me stop this thing quickly so i have image already with the name of app so what i i'll do is i'll try to push this thing over here on docker hub so let's see how we can do so we have a command okay this one so docker tag this is your local image so we'll see docker tag your local image name is app and your uh docker hub image name is it's mahesh karya okay slash fast api like this this is my username but it won't work if you are you are not logged in already so what you have to do is let me copy here okay so we can use it later okay cool so what you have to do is you just need to first log in over here so what i'll do will log in here quickly okay with username my username is it's mahesh karya okay and password of course so password you need to create uh you need to go here inside settings account settings where you will get a security over here okay on default security uh sorry security okay here you need to create access token and name it like for fast api i'm creating token let's say so just you will get this token this is one time token so you just need to grab this thing and save it somewhere else otherwise it won't work okay and once you hit enter after that this is this is the password that you are using now you need to hit with this password std iron okay this is also right because it's going to save somewhere so this is vulnerable okay but it's okay for now for demo we are using something like that i'll also tell you the solution i have already told you guys in last video okay you just what you have to do is here you just need to use hyphen iphone password okay have fun s t d i n okay and here you can do is echo with the equal you can save inside some file and now you can read file here let's say this is my password okay sorry it was the password here password here and pipe operator hit enter something like that so okay this is it so now we need to run this command uh docker tag okay so now i'll hit enter so this is done now i just need to push this thing okay so what i'll do is i'll quickly copy and close okay now if you guys remember in here uh we got the command okay docker push and tag name so make sure to use uh the tag name okay v1 you can use otherwise it will by default it will update in latest one okay so this is uh it's updating okay you can see so in a while once this is done it will automatically push your image over there okay it will take some time i guess till then so uh oh no it is so yeah this is uh done okay now you can see here as well in registries okay if you just you just need to click here on connect so it will ask for where you wanna connect okay you can publish anywhere like in aws okay gitlab there are lots of other options azure okay so thanks okay cool so now what we'll do is we'll quickly go here and refresh the page so now you can see the latest one will get over here quickly okay so last minute we got our first latest so now you can take a pull from here if you want okay instead of push you can pull here and you can share with your friends and something like that so yeah thank you so much this is all we have to do guys like this uh it i guess okay if you want to use postgres this is also easy instead of that we'll write postdress uh the port 5432 one i guess something like that and yeah this is it so thank you very much all of you guys for being with me supporting me all the time it's been pleasure to be here and please let me know inside comment box what you want from my side uh thank you so much for your time and don't forget to subscribe my channel if you haven't already uh good day bye thank you
Info
Channel: Mahesh Kariya
Views: 8,335
Rating: undefined out of 5
Keywords:
Id: YGrfexNwsFU
Channel Id: undefined
Length: 19min 52sec (1192 seconds)
Published: Sat Feb 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.