have you seen this meme it's true that the fast API performance is really really fast I think that's why it name is fast API but it's a python framework which is really really mindblowing and very easy to use but what if you can use the same fast API with Docker so I had created one fast API course which is the bestest video of my channel so I thought that how it would be good if I can use Docker to set up the fast API on my local machine in that way I don't have to create the virtual environment I don't have to worry about what python version I'm using and I can isolate everything inside a container from my local machine and one more benefit what if you can give the docker file and the docker compose file to your friend or the colleague so that they will have the exactly same environment where you are working on your local machine whether it's Mac OS Linux windows or any operating system it will going to work exactly same you just need Docker and that's it so it's now time to see how we can use Docker to set up for fast API so welcome to bit fils I'm I'm your host saruk and this video is dockerizing the fast API with ease so it's now time to get started but before I will say that hey if you think that this video is helpful for you smash that like button and give me some likes because I am hungry for likes no that's not the case but if you genuinely feel that this video is helpful your like will encourage me to create more such kind of videos and if you are new to this channel consider subscribing by hitting that subscribe button so two things you have to do if you think this is a good video one is smashing hitting that like button and hitting the Subscribe button if you're not a subscriber so let's now start this video to dockerize our fast API application we first need to create a virtual environment so on the terminal I'm going to type Python 3 VV with the hyphen m flag and I'm going to call VV so we can just say hey we want to have venv it's up to you whatever the name you want to give so since I have given venv you can see thatv EnV directory is there and now my vscode is saying that hey is this a new virtual environment do you want to activate it for your workspace and I say yes this means whenever I create a new terminal you can see my virtual environment is automatically activated now if this is not by chance activated on your vs code then you just need to say source and then go inside this VNV bin and hit this activate file which is like this so VNV bin activate and this will activate your virtual environment now once your virtual environment is ready it's time to use pip 3 to install various things first we need the fast API obviously and also we need uvi con like that so these two will be installed inside our virtual environment and once this is done we can get started by writing our fast API code so first of all here I say from Fast API import fast API and then create an app of fast API like this and once this is done then we need to Simply create a function so I say hey function is going to be the index and index means returning the message of hello world but to make it a simple route so that we can hit it we just need to say hey app do get on the slash that means if I go on the base URL then it will return message as hello world now it's time to run how we can run remember we have installed the UV icon so we say UV icon and then we Define the file name which is called Main and the app name which is app so colon app once this is done you can see if I go on Local Host 8,000 we have this hello world message wow our application is ready the first thing we need to do is PIP three and we need to freeze to all the requirement to requirements. txt file once this is done you can see all the requirements we have is here on our requirements. txt file great now very first thing we need to do to dockerize it is create a Docker file now in this Docker file I'm going to use from Python 3 point which one 9 10 11 which one we should use so let's go to the docker Hub search for python so Python and here we go so python image is with the latest word version of 3.
9 and we want to use the slim version so 3. 9 and I'm going to use the slim version great then I'll Define the work directory so work D is going to be the app and then I will copy everything from requirement. txt to requirement.
txt inside this worker which is already defined then we say hey run pip 3 installation ation so once we have all the dependencies in our container then we run this command inside of the container once again once everything is there then we copy the code so that's why we say dot and Dot so first Dot and the first requirement. txt whenever we write means the local file so when I say requirement. txt I mean local requirement.
txt and second if I type requirement. txt that means it's going to be inside of the work D which we have defined as the app once this is done then it's going to be simple we just need to say command and then instead of whatever this GitHub co-pilot is suggesting we need to say UV ion mean app and I think that's it great so we are done with this and now let's see how we can run it so let's build the image so I say Docker build and and I'm going to give a tag called Fast API and say dot means find a Docker file in the root directory so it's going to create a Docker file for me and if everything is working fine we should see successfully creating a image so it's creating and installing everything is done now if I say images now I have this fast API image I can run it so Docker run and right right now I'm going to use RM which will be like once container is done then remove the container so that we don't have a bunch of containers it means interactive we Define Port as 8,000 because remember we had the 8,000 and the image name is fast API once this container is going to run I want to run the bash on it you can see bash is there now we are inside of the container that's great if I LS you can see main. py is there requirement.
txt is there Docker file is there if I do LS a then you can see we also have V EnV why do we need the virtual environment inside of our container we don't want that so for that we need to create a Docker ignore file so we say dot Docker ignore file and we say hey I don't want to push V EnV whenever we create any image okay so I'm going to press contrl D to get out of the container and that's why I use the RM flag so that we will not have unnecessary container here so I'm going to remove the image so remove image of fast API basically I'm going to remove every other image so that it will be really cleaner now so it's done and then I say Docker build fast API once again and it has created so Docker run container once again and now if I say LSA you can see we don't have the dotv EnV file now it's time to run the application and see if we will be able to see that on Local Host 8,000 or not and remember we just need to say V uh sorry UV ion main app it's good you can see 127. 0. 0.
1 8000 and it should work but no because here instead of this Local Host what we need to do we need to say hey the host is actually 0. 0. 0.
0 this is the one which we are going to use so that this will be the defaulted to the outside of the docker container and now if we run it you can see this hello world is there now since we are able to successfully run this container and uh create this image time for the docker file so that we don't have to write all that thing like this on our terminal every time so what we are going to do is we need to create a compose file compose do yaml so compose do yml and now version of this is three uh so not hash three then which Services I'm going to use so Services is going to be the web and this is actually the container I'm going to use and then I'm going to say hey build from dot now dot means the root file where the composed file is find a Docker file and build a image from that now I need to Define some ports and Port is going to be 8,000 um 8,000 colon 8,000 this is the port which we are going to map so yes that's it so I'm going to just see if we have any container or not no there is no container any images yes we have the image so I don't want to get this image so I'm going to remove it and now I will say Docker compose and I will use up command so when I say Docker compose up it will go inside this compost. WL file and then it will create the container called web and inside that it will create the image from this Docker file and Define the port right here so see it says it is is once again created on Local Host 8,000 no remember I have to use the host of 0. 0.
0 So why it's doing that first of all how this command is running remember when I defined the docker file inside that we done all these things but last command is really important this command only run when you create the container out of this image so when we create the container out of this image this command runs which is normal so I need to say hey I'm going to say host is going to be 0. 0. 0.
0 once this is done then we can stop this container or stop this Docker compose by pressing control C and it will take exactly 10 second right here and once again I run this compose up and this time it still using this why remember this uh where is that remember this compose create the image and once the image is created it will not create it again and again so there is one way that you can obviously go remove the image and then recreate it but instead of doing that what you can do you can use a flag called build now this flag will say that every time you run the compost file do not use already image create the image if there is any image to be created now it has created and you can see since the new image is there it is now using this 0. 0. 0.