Learn Docker to Make Deployment Easy (step-by-step)

3.54k views1893 WordsCopy TextShare
Eric Roby
Docker allows us to keep our local environment similar to our production environment. We will use Fa...
Video Transcript:
hey friends in this video we will go over Docker images Docker containers and Docker files in fast API we will learn about how Docker images and containers ensure consistency across multiple development testing and production environments this is because the same Docker image and really the container can run identically on any system that supports Docker reducing the it works on my machine problem now before we can start this tutorial the very first thing you need to do is install Docker on your personal machine if you have not installed Docker I will leave a link below where you can download Docker whether you have like the Mac silicon chip Intel chip Windows it doesn't matter I will leave the link below so you can install Docker on your machine now once you set up Docker desktop you will have something like this where we have containers and we have images now an image is like a blueprint of what your application will look like like a container is going to be a self-contained environment that is going to hold all the dependencies for your application now here I already have a virtual environment already set up for my application so all I really need to do to get started is I am going to create a new folder called app and inside app I'm going to create a new file called main. py I'm then going to say pip install fast API and uicorn and now we can start creating our application however before we get started we also want to go in here and say new file uncore and nitor dopy this is how we can create packages in Python so without doing this our application won't know this is a python package it'll just know there's a python file sitting in a empty folder so now that it knows it's a python package we can start building our app and just like always the very first thing we're going to do is need to import all of these dependencies and I actually need to do a pip install for SQL Alchemy all right that'll get rid of all the yellow lines under all of these dependencies I am next going to create our application with a database URL which goes to a sqlite database where we just set up our engine session local and base next we're going to create our database dependency with our dependency annotated which is going to be used for our database dependency injection and then lastly we're just going to do it all in the same file we're just going to Define a simple user model where we're going to create a class of user which inherits base we're going to create this database table inside our SQL light database called users with an ID and a name and then we're going to initialize this database the very first time this application is ran now the next thing we're going to add is just a app. get user where we can get all of the users by running a dbquery user.
all this will fetch all of our users and we'll be able to instantiate a database by using our dependency injection and then lastly we want to be able to do ana. poost with our user where we can create a user pass in a name run a background task and then use our dependency injection for our database and really what I'm doing here is we're just instantiating a new object saving that object to the database and then I'm running a print message background task which is right here which is just going to print to our terminal the user and then the name of that user is created successfully all right so now that we have this the next thing that we' want to create is a requirements. text file and we can do this by saying pip freeze greater than requirements.
text now when you do this we're going to get a requirement . text file with all of our dependencies right here and we need this for our Docker because when we have our image and we create our container it's going to want to know all of these dependencies so it can create the exact environment that we have running on our machine and now for the last thing that we need to create we can say new file and we need to create something called a Docker file now a Docker file is going to hold all of the instructions for our image to be able to create that Docker container so the very first thing we need to do is tell Docker what version of python we want to install on our container so I'm just going to say from python 3. 9 the next thing we need to add is the working directory inside the container to the app now this is how we set where the code is going to live inside the docker container we then need to copy over the requirements.
text file from our local directory to the app directory in our Docker container once it's built so it knows to download all these dependencies and this dot just means from the same directory so we can see that requirements is going to be in the same directory now after we get the requirements. text we need to do a pip install of our requirements. text so pip install D requirements.
text that should look fairly normal the new changes we're adding is this Noh directory this is to tell Docker that we don't want to cash the installed packages to save space we want to reinstall all these dependencies each time we run the new container and lastly the dot upgrade this is going to upgrade all of the packages every single time this is ran to the absolute newest version so if we look at a requirements. text we have specified versions inside here but in our Docker file we're overriding this to upgrade all of those dependencies now you could remove this and it's going to install the exact version that's in your requirements. text file but for this tutorial I'm I'm going to leave this upgrade inside we then want to copy the entire app directory which contains our main.
py file and in other files in the future but for us right now it's just the main. py file from our local directory into this app directory that's inside our container in the future so we're going to say add to our container everything from our app and now for the last one it's our Command which is going to be uicorn main colon app but instead of saying like-- reload we're going to Define our host to be 0. 0.
0. 0 so this binds the socket to all network interfaces which makes the server accessible from outside the container we then want to specify the port which is the listen on Port 80 inside the container so this needs to be like understood as best as possible and Docker has its own little uh like think of it as its like own little world and we need to be so specific about what we're telling Docker to do or it's going to try and customize some things for us automatically so this is why we want to specify the port and specify the host inside the container now just to kind of show you that we're not running this on our personal machine or inside our virtual environment I'm going to just go ahead and delete our V andv so what we can see here is we just have our app which has our init. py and our main.
py file and if we scroll all the way to top it's going to be like hey you don't have any of the dependencies running we have our Docker file and then we have a requirements.
Copyright © 2025. Made with ♥ in London by YTScribe.com