Django and PostgreSQL using Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everybody today we are going to build a Django application running in a Docker container and a postgres database running in a different container these two containers are going to run uh they're going to be connected to the same Docker Network right we're going to learn how we can one one Docker container can recognize another look container and they're connected to the same network right and uh first this right here is the demo of the application it's it's easy it's nothing much let's let's just see how how it looks like yeah it's just a task application which I've implemented some simple Cloud functionality some credit delete updates you know uh retrieve all the tasks in that it's nothing complex right so let's go and check the code we see how we've utilized Docker compose to run two different containers connect with the same network and the different containers in our case is a jungle application and the postgres database right so let's check out the code uh as you can see here the code there's nothing complex uh it's just a call project to the tasks application and I don't think we'll check much about the code because we're not that's not the main purpose of this video you see it's a view set where you can it does all the cloud functionalities and serializer nothing much nothing much and uh it asks more a task model yeah so let's start by um creating or setting up our virtual uh our uh our environment variables environment variables we set up our database that is right so we will use OS to set to create our environment variables so I'll just import OS and uh I'll set up the variables of the database nothing much so I'll just get rid of this I'll change this to post a scale and uh for the name I'm going to use OS dot uh [Music] don't get I'll give it a name okay scale name it could be any name the user SQL user he got password and we go hosts all right and we got whatever we have ports and I think that's it let me check yeah that that's it so we've set up our database um what next so after setting up a database what next you can do is um let's create a TMV file so that you can set up these environment variables right here EnV and let me split the screen yeah so we can set up SQL name you can give it a name like um what are you working on tasks say tasks BB is the name of the database battery that's the name of the database SQL name then we got SQL user we can say uh array SQL password [Music] we can say password okay host will be the name of the postgres container right I've noticed created the docker compose file yet so we don't know the name of the postgres container we will come and set this up later port by default is 54.32. and uh yeah I think that's it the EnV variable is set up so let's set up the docker file and the docker compose file I'll just close this and create a Docker file without an extension let's see and uh yeah now we're going to write some instructions which will be run when we want to build an image so we first by bringing in the base image which is going to be python I'm going to use python.11.4 Slim bullseye sorry about that once you get the image that's the base image then you're going to set up two environment variables I'm going to say the reason why for the environment variables one is Python and buffered and buff pad message to one and another one is python don't try to page code trade byte chord so in this first environment variable right here right here Python and buffered it will tell python that it should not buffer the standard output in the certain uh error so if there's an error something like that it should go direct to the log file it should be buffered but when I say buffered means like extrude somewhere right the memory something like that it should not be stored it should just be taken directly to the if maybe of um have configured logging in your application so you want that standard output to a standard error to go directly to the log it should not be buffered so that when let's say the container shuts down you can go to your log and see all the logs right there's nothing that was buffered and for the python don't try to byte code it tells python that um there's this uh dot poic files it tells python it prevents python from writing dot pyc files to the disk then um we're going to set up our work directory and I'm just going to call it slash app and then I'm going to copy all my all this content right here all these folders and files from the current directory to the to the currently working directly right and we have set up our working directly as is up you can also say take it directly to you today yeah absolute but put the absolute path which is okay and it can work and then after that you can let's say you enter on some updates you know a container that are going to spin up so I'll get update to the update all day all the packages which are installed in the OS level then um we can next run some dependencies install some dependencies Philippines so are requirements Dot txt and before that we'll copy the requirement.txt file copy okay the moment I see that I'm not treated the requirement the txt file but I'm going to create it in a moment copy you find the txt file to the current direction then uh-huh I think I think that's it yeah to run application we're going to create a script right now we'll get into that abash Scripts but yeah so let's create our requirements.txt file uh what you can do is pip freeze yeah so okay yeah so jungle jungle is framework those are dependencies at the moment and uh we're going to install another dependency it's uh it's a python SQL adapter right like it the independence is binary let me install it first let me talk about it second G2 binary you can see the name there right there so this is a this package right here it will help us connect to the postgres database right using python other languages have the other have their own uh package that they can use to connect to a database so this is a computer binary will help us connect to the postgres container postgres uh approach database okay and you can install you can eliminate this part here right this patch uh hyphen binary but when you element eliminate this part these uh um these are dependencies that you'll have to install in your local container in your local container that has your Django application right but for development purposes you can just use Circle G2 binary which means you won't you won't have to install other dependencies right there so yeah so let's update our recon.txt file over there yeah that seems and uh yeah it's right here everything is working fine our Docker file is working fine right so let me create that script this script you're going to run this script um when we want to build uh when you want to spin up uh a container that is running on Django application so start.sh and uh so it's basically I'm going to run this script is you're going to make the migrations and you're going to run great migrate poi make migrations and you're going to specify no input so we're just going to run server nothing complex and you're going to run it in a port a8000 right yeah nothing complex right here so this script I'm going to use it in my Docker composer you'll see you're creating that right now pause the tml uh-huh so in this Docker compose file why are we using a local composite because I can spin up a postgres container by just running a Docker Docker run just quickly like that something like that and I can spin up a postgres container it can spin up this Django container it is a jungle container running a Django application right but the importance of using Docker compose it will create a network by default so these two containers they'll be connected they'll be running the same uh Network they'll be connected to the same network so it will be easy for the two containers to communicate with each other right you're going to see that right now so we're going to start by specifying the version that you're going to use at the moment it's step by day and I'm going to specify the services stop by the web this is the name of this app you can use any name you can use any name so build say build context so this is just uh build this Docker file right here you're building this Docker file right here to have an image right which will use to spin up container you can specify ports this is Port binding you want our Django application to be recognized uh to be recognized outside this network right because we're going to test it in our machine right here our local our my laptop right not inside the container so we're doing some push binding and then we're going to have command and in this command let's say we're going to run uh this script right here start .sh I'm going to run this script right here so that you can start our jungle application can run right and we're going to specify volumes remember as I said this is for development purposes so you want to so basically this does here this does right here is if there's any change in the current directory right which means current directory if there's any change in the current directory it should be reflected to our to our app directory which is inside the docker container right so it means and if the change is recognized of course the server will reload and bring in the new changes again this is just for the development purposes for production there's a lot different things that will do like using the Unicorn and we won't use these volumes right here we won't use run server you see but this is just for the development purposes for production maybe I'll make another video then for the EMZ file I'm going to specify the EMV file that it's going to read all our dependencies and the name is root EMV and I cannotice okay it's already I've already created it and uh Nexus depends on so basically this does here is a like don't start this container before what I'm going to put right here before the name of the other container which I'm not yet created but I'm creating it right now so yeah so the other services postgresdb and you're going to put the image from Docker hub then it's supposed Grace what this will do is just grab the latest postgres image right you can specify the version if you want if it is 15 if it is 14 13 all that I'm just going to grab the latest which I assume it's 15 right now for the volumes I'm going to support squares data we should map it to V and r V first screw skip l later I'm going to create the volumes right here these I have to create a volumes called postgres data which I'm going to create down there down here let me just create it right now volumes postgres beta there you go what next you have you need to specify your environment variables yeah so for the postgres what you need to specify here is um there's a couple environment variables that you need to specify uh the one that is needed it's a must it's the postgres DB I think the post hdb the average they'll be used by default they'll be set up by default [Music] we're reading it from the environmental variables by the way so I'm just split this up SQL name and uh postgres user user postgres password change scale password yeah I'm just going to use those three environment variables for the port by default is 54.32 so yeah I think that's it if I'm not sure oh yeah for the host as said now here how do these two containers recognize each other so they recognize each other by using the names and then let me just load this and this I'll take a second yeah so they recognize each other by using the names the names of the services as you can see here we have two Services right the web and post qsdb so this contains two containers here recognize each other by the name of their of the service right so for the host you can specify the holes they're supposed to be hdb yeah I think now that's I think that should be it right we got our dog compose setup we got uh Docker file setup in Easter yeah I think I think that's it let's try to spin this up and see what the result is right but first I had a local container running so yeah I need to stop it [Music] come here this is the for the demo application so I'll just remove that okay so let's pin spin this up I want to run overcompose app build so build the flag build we just say like build the docker file right here this dog file right here like build it from afresh so guys uh in my application it is now running successfully uh as you can see it's running in the this is the link to the to my server yeah and just click all right I have to change this to localhost change this to localhost right third definitely wasn't my expectation what happened right there maybe we didn't register the urls okay we didn't introduce I didn't register my my URLs uh this part you know it's not supposed to be I must have I should have already worked on it but uh I can do it real quick it's nothing complex that should do it and as you can see I've made a change in this directory right here and it's reloading as always that's what I was talking about and I said adding this part right here this part right here this part right here these volumes right so when you make a change in this directory right here the current direction it will reflect to the up directory and the app directory is the working directly inside the the docker container that's running a jungle application right because it speaks like that in our Docker file right here walk Direction okay let's now let's check the application it should be working hopefully yeah it does work now English tasks uh-huh yeah we are connected to the postgres database right but let's confirm that right let's create a task then you go to the postgres database and query using uh SQL so that you can see if that's just visible from there uh what can I say uh I love listening to music you know it's not a task but yeah it can't work so we created a task and uh huh less query using SQL language from the from the posters container so it means you love to get inside the postgres container and run some queries open another terminal okay so let's check the running containers which continent they're running we have two running containers right here we have two running containers let me just close this this right here and this right here and as you can see this is a postgres container this is the other jungle continue running our jungle application so let's get into the postgres Container because you want to run SQL query what can we do use exec it then we specify the ID container ID of the postgres and we want to open a bus shell and we are in you can run LS and all that stuff so let's use PC equal to open a postgres shell or you can run the SQL queries so P SQL you can specify the user what it is just a user you know you gotta pass the authentication right user and uh I think also you have to specify the Dominion but uh I think it's Flag d if it's wrong we'll check will correct us so yeah okay we're connected yeah tasks DB according to the task DB and we can run um let's check all the tables and these are all the tables again this is most of them are by default and this is right this right here our tasks underscore tasks that's what you want to query and see if indeed is that task that we created from there from the from the web from the Chrome this this right here this task so we run our SQL query which gives us the select statement star that means all columns from the table tasks underscore task and semicolon yeah right there listen to songs let's add another task just to test it out again uh yeah uh watch football one it's not complete oops my mic push football yeah and now when we refresh we have two tasks created let's run again another query select and just go up right there see we are not connected to the postgres container jungle container is connected to the postgres container this has been made easy because of Docker compose we've leveraged Docker compose so we've utilized uh we can we have created services to Services a web service and a post race DB service and these tools these two Services which there are containers right they're connected to the same network so it's easy to one it's easy for one service to recognize the other using just the name or web right you can again do it without using logo compose but it's much easier using Docker home pause so yeah I think that's that's the purpose of this video so I hope you learned something if you did you can share it in the comment section and uh I love if you leave a like subscribe to my channel and we'll get more content about the web thanks for watching ciao
Info
Channel: CodeWithRay
Views: 2,707
Rating: undefined out of 5
Keywords:
Id: PYN7sxfZWIo
Channel Id: undefined
Length: 27min 42sec (1662 seconds)
Published: Tue Aug 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.