Docker Compose - Deploy Django with Nginx and MySql services

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so Hello friends I am Niraj and hope you are doing good so today let us complete our Docker compose session and in this session we are going to run our Django application that we built and inside the container and we are going to use having three services like nginx as a web server to serve our static content our taking the request over web and our Django application that would be a Django application and our database we are going to use MySQL right so I have already created a sample Django application this this is a very very base one so this is this in this and this nginx is going to have its own Docker file and our Django application is also having its own Docker file so in Django application we are using Docker ignore as well so that I mean when we is my development virtual environment so I do not want this when we to be to be uploaded inside container so that is why I have ignored it so Docker ignore is just like a gatek node and I am also going to use dot envi EnV file so that I I can export uh all the environment in the variables written over there so that would be needed by my database to connect the data my Django application to our database so so that it can be exported and here the in requirement.txt file we are having three dependencies like we need Django and in order to serve our application Django application we are going to use J unicorn and in order to connect to mySQL so we are going to use MySQL client adapter so that it can commit it can connect to mySQL database and this is its Docker file so its Docker file is we uh since we are going to use MySQL client so that is why we are going to use the my the python base image version 3.10 and not a slim version or not Alpine because it otherwise it we might need to install all the other underlying dependencies over there so this would be a Debian one and it would be having all sort of requirements right so our working directory is Django inside our container and this environment variable we are setting python unbuffered to make it a non empty value so that we can get our if our if there is any error in our application so it it gets to the terminal output and next we are upgrading our pip because MySQL client might have some issue with the ultra pip versions so it's better to upgrade the the beep as well and use the latest softwares and the next we are copying our requirement or txt file from our from this file to inside container and next we are installing all the dependencies of requirement or txt file and once everything is finished we are copying the file content from this host machine to container right so this is what our Django Docker file will look looks like and now come to our Docker compose so let's start a few things let's let's proceed one by one so in our in our Django we are we need to obviously we need to expose some ports so we will be using ports we will be our Django app would be running out let's say a port 8000 1 inside our host and inside container it will be listening on Port 8001 and the command we actually also need to provide a command you might recall in Docker file we can issue a CMD so command instruction so similarly we can also provide a command over here so command and we will be using G uni con and if you look at our Django application so the base folder is named as core so G unicorn core Dot wsgi file that would serve the our application and we will also bind ports so bind and we are binding to all the servers and on Port 8001 right and next we let's complete our database container as well so our database container named as we are going to use MySQL and container name is dbcon but here uh we think of it like think of it that when you install MySQL so one root user is automatically created and but we need to specify the the password of that user and in in our case we also want that whenever Django application would run so definitely it will it would connect to a database so we need to create the database automatically over there so in order to do that we can fortunately we can provide some environment variable and there is nothing to worry about so we can set them in environment variables so it's a key value pair so first we will start with MySQL and root password and in my case I am going to set it to neeraj my secret root password and next variable we are going to use as MySQL database and our database name would be test underscore DB so this way with this instruction we are telling our Django our Docker compose to create a database named testdb whenever a DB a DB Services service gets up so let's save it no and now now it looks to be okay and one more thing that if we go this way it might happen that our Django way Django application will start running but it may happen that our database operation that database service doesn't get up so in that case our Shango Django app will crash so in order to fix that ah to solve that that issue that that actually that might come that might happen so we can use another syntax depends on and depends on actually with actually with this syntax we are telling a Docker compose to start this service until the underlying dependent service gets up so in this case we are going to use DB so we want our Django service to up and after once that DB service gets up right so and similarly cos engine in nginx also we will need to our Django app to get up first I will show that to you so but first let me write it depends on and it depends on over Django underscore app maybe a type over here okay so with this we we want our nginx service service to get to get up uh once our Django app gets up right so I will tell you the reason for that so first let me create a file in nginx cause and the nginx container we need to specify ah like you might recall you might recall that we uh we can we were using same linking in the while setting up setting up our engine X away so we we so we we used to create same lengths so but so that our our Engineers could know uh how to serve the the applications application content right so we will create a file let's name it default.com and inside this this file so actually we uh we are going to replace the file the default configuration file with this with our own configuration file so that it can know how to serve the uh serve the Django Django Django application right so we will start with a stream and we will name it Django and we will Define our server as our service name so our service name is Django underscore app running on Port 8001 put a semicolon and next we will start with our server block so listen on Port 80 server name localhost and now we'll start with the location block so we want all the requests on on this server to be redirected to our Django application Django server so in order to do that we will proxy pass so we will proxy all the request on the root location to our http Django server Django Upstream right so this is it so this looks to be okay and now in the docker file we need to copy this file as well so let's copy it default Dot comf 2 we need to copy this file inside our container at this location Etc nginx conf dot d fall Dot conf right now it looks to be okay so let's check it out so right now there are no images no container running on my Docker desktop so let's start it so we will say Docker compose build it might take some time it will take the images right so it is up and right now let's see Docker compose up so it's running it might take some time to create the services for us and if there is any any errors so definitely we will see that and we will fix them so looks to be okay nginx is up go to container Docker desktop all the three services are running fine so here also in our database container if we try to my SQL minus p show data bases you can see like we are already having a test database over here right now inspect some something more our Django container is also running fine no issues so let's go to browser and try to access localhost server Django application is running fine so we are getting the content so this is a very base installation nothing fancy so our application is running fine so and all the and application is running fine in all the three concept three separate containers such as three separate services but there is a there is one more thing which we can extend that uh see the thing is like winners like container gets destroyed and candidate container gets recreated so we can also and uh like uh ways like our database Services is running inside a container so whenever this container gets lost so we will also get uh we will we will also lose all the data inside this container so in order to in order to fix this we need to have some ways so that our data stores on on the host machine so but so in order to do that we need to map volumes right from our host machine to the to the container so that we can do it very easily so in our in our local machine on on our rather on our host machine so we will create a folder data and inside data actually the purpose of this folder is that we we can have all the data of data data from all the containers inside this folder so that our data could be preserved causing it may happen that your your application might use multiple databases like MySQL postgres and redis so that is why it is a convention to name appropriately so data MySQL and DB and in inside the container container side the actual data of my cycle resides at the location that is were lib MySQL right so right now if we rebuild it so let's let's see that it that that in action okay Ctrl C to close our container up right now you can see there is one data folder data name folder is created so this is this is lying on our host machine so in case we recreate our container so our data won't be won't be lost so you can say our test DB is also here and the if we access our application it is running fine no issues and we can also cross check it here like one more thing which we can do it we can do so for example say go to Although our database has been created but there is no data inside it so MySQL minus p provide the password use test test DB suitables so there is no tables inside it but Django uses uh for a fresh application for a for a fresh Django installation we need to migrate some data so that we can do very easily here we are seeing so we can fire the command my python manage dot pi migrate what problem it is there is some problem in it okay okay okay give it a second I think like we didn't okay yes so actually we didn't export uh environment variables my fault so do you remember so you might remember that I showed you that we have that I have kept all the environment variables inside this file so we need to tell Django like whatever the environment variables are so EnV so our file name is Dot Django and Dot e and V save it and let's destroy our containers and talk compose down let's try to rebuild it now docker compose up so created looks to be okay our application is running fine yes it is running fine and now this time if we go to our Django container and we can fire a command python manage dot pi migrate so it is applying all the migrations that we can cross check it here inside the inside our data container go to terminal login to mySQL console enter password mirrors use test underscore DB showed tables so you can see like we are already we are having all these tables which which comes as a when we migrate our our uh when we when we migrate or the databases which are which come pre-installed with with the Django so they are they are they are they are already inside our database container that means like cover application is running fine and we have covered many things we have seen how we can instruct dependencies we have also used volumes we have also used environment files and many things which many new Concepts obviously yes so these are few new Concepts so but that is running fine so hope you liked hope you hope you enjoyed the session and if there is there is any issue or any confusion any query so please do let me know I will try to answer them as soon as possible so till then take care and happy coding
Info
Channel: Grow Your Skill
Views: 6,315
Rating: undefined out of 5
Keywords: Learn And Grow, docker, docker compose, docker compose tutorial, docker compose yml, docker container, docker tutorial, docker tutorial for beginners, learn docker, what is docker, Grow Your Skill, docker django, django tutorial, load balancer, django nginx docker, docker python tutorial, django nginx, docker mysql
Id: cxUyrB7ZZao
Channel Id: undefined
Length: 20min 59sec (1259 seconds)
Published: Sun Mar 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.