Creating Django App on Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back agents my name is Devin Vivian in this video we'll see how to create a django application on docker so we have talked about docker then we have seen how to install it but it's important for us to understand how to build an application using docker see there are two ways of having your application running on docker first we can create a project separately and then we can dock arise it or we can say we can continue eyes the application so that it will run on docker the other way is you can create the application inside the container itself I mean you can install the dog or you can install the Django framework on docker and then you can write a code so by default you are going with that way of writing your application on talker so the moment you done this application it will directly run on docker container so we are going for the second way in this video of course we have to also understand how do we continue eyes the existing applications which we are going to see in the next video so in this video we are trying to create a django application on da course of course on this machine you need a docker installed so it is verified do we have dock install so if I go to terminal and if I search for darker version and yes there's a dock installed I just need to create a Django project here and of course we are going to write a simple hello world application we're not going to do something a big here because see if you and if you know how django works of course once you have seen once you see this video you understand how to get a big application on docker or you can just google it out how to add data based on different stuff but here the important thing is how can you build the application now if you're not sure how to write a django application and what is django i have a series on django available so in the description area you will find a link for the django series django is basically a web framework for python so of course you can build web application using multiple languages we have java we have a dotnet framework we have PHP we have Python and if you want to build your web application using Python one of the best framework is is Django so let's let's start with this now first of all let me create a folder and yes we are going to use an IDE here which is or the editor here which is vs code so you need vs / as well of course you can use any idea which you prefer but if you want to follow along vs put is preferred one so that you will not get you will not get stuck anywhere so let me create a folder here so I will say mkdir and it will said this is projects and let me just switch to projects switch to projects here and in this project I want to create a Django application but then I want to put that in a separate folder so I will have that folder named as the Isco of course I can have a name doesn't matter and let me switch to the disco here and let me create a project but then even before creating a Django project you need to do some setup on docker remember we have we are building this Django application on docker so we have to write some configuration files for docker now on docker we have different options for configuration one of the best configuration files for docker is docker file itself so what so what is docker file so docker file is a configuration file which helps your doc engine create a containers for you so based on the image so as we have discussed before we have images and with those images we create containers now in this image what features you need so what are the layers you need we mentioned that in the docker file but what happens you know when you are working on the big application when you work on the enterprise application you don't have one container running you might be having multiple containers and to configure all these containers you know at what time you want which container to get started you can use a special feature of docker which is talk of compose so we are going to use docker compose here as well I mean if you want to use this application if you also go on to work with database then docker compose makes much more sense so we will start with that so let's use docker compose button if you want to configure double compose you need to create a file so I will go to my vs code and the folder which we have created let's open that here so I will go back to be asked what I will say open folder so I'm in my projects folder where we have then his Co let's select that and click on ok so I am in this folder where we have that is Co let me just close this and here so this is my folder where I have to create a darker composed configuration file now what format it supports so normally we can go with Jason or you can go with DML so let's go with the a meal which is sophisticated jason so let's do that so the file name has to be darker - compose and the extension we are going to use is wireman of course you can also go for the for later extension which is why am L but the environment makes sense now once you've got this Vimal file you have to write a quantification here but then it will write the document post confusion later initially let's go with this single container configuration which is talk of file let me also create one more confusion file this is for docker specifically so which is docker file and remember to have this name as the capital that's important D capital and it's a one word well F is small there is no extension for this click on enter so we got two files here which is docker compose and docker file now in this drop of file we have tried some steps now you can just go to google and search for how docker file works and how to do that so in this video I will give you some steps of creating this application so my vs code says the docker extension is recommended for this type so yes it is helpful for you to have this exchange ins in your machine so 1/4 doc docx extensions as well so it it makes you a project easy and manage and deeper containers application so of course it will give you suggestions and it will let you know what went wrong anywhere okay so here what I will do here is first of all I have to mention see but when you talk about images in docker it is created with the help of layers so one image will be having multiple layers so we have to start with the base layer so this project is built on what so this project is built on Python so what I will do I will say from so we have to mention the base image and the base image for me is bitin and the version we are going for is three then we have to mention the environment variables so let's say that first Python unbuffered then we have to create a folder so normally when you run your application the container it's it's good to have all your files in that particular folder so let me create a folder here and then the command I want to execute is mkdir in the container and for that we have to use this run command and the internal command the the UNIX command you're going to use is mkdir and remember I am doing this practical on Linux based system so the container which we are going to use is layers based of course so I want to create this inside an app folders of course you can have any name so let me just have it app and then I have to switch I have to mention my working directory so where I want all these files to be I want it to be app now the next thing would be so let's say we are using docker to learn Django but then we have to install Django on the on the server right we don't have Django as of now so we have to install Django how do we do that now if you have seen my Django series we have to install the Django admin on the system so here as well in this container we have to install Django admin but how do we do that so the one way you can do that is by creating a separate file and the file we have to create is called requirements dot txt of course you can have a name so it may have this file name as requirements dot txt this is where I will mention what requirements I have say in your project you might be having multiple requirements maybe you have a requirement for my skill you'll be baby or you come in for Django so here we have to mention all these things here so we have to say Django we need Django and the version we are going to use is 2.2 so we can use double equal to or you can also specify relative operators which is less than or greater than if you want to specify the version which you're going to use so that's one we only need Django and to include that here we have to inform our Django application hey we have to so if you have please instruct our Darko that we have to install and the way you can do that is by using copy so we have to copy this requirement file in our project so I will say requirement dot txt we have to push that in our folder which is app ok that's done now once you have seen the main thing is in your container you'll be having this app folder now this app will have your source code we are requirement dot txt I know this will not have your source but this will have a requirement dot txt now once you have requirements dot txt inside your app you need to install the requirements so you have to again run a command but this time you have to execute the installment commands right so if you want to install something we use PHP IP maybe you if you don't have P I opinion container most it will have P IP based on which type of Linux you are using if you if you are specifically focusing on Alpine I'm not sure do we Alpine has B IP but I guess by defy it will use Ubuntu so ap IP will be there if you get errors with this VIP install make sure that you also install p IP okay so in your in one of the command one you have to also install p IP but i mean assuming that it will have VIP service api p install all and then i have to mention the file name which is requirement dot txt now this will install all the requirements which is mentioned in the requirements dot txt now once you have that you have to copy the entire project and tap folder which you are creating in your in your base code into the app the app which we have mentioned here so basically this app will have the entire project in the container that's a tag that should of a file or we got an error here that should be capital okay so the ones we have reached till this point it's time to move to our docker compose so yes we have done with the commands dot txt we have talked about docker file let's move to dr. compose now in docker compose we have to mentioned different steps maybe you want to work with database maybe you want to run these on this server so you have to mention all those configuration here so this will simply follow the steps you know this is the main file which will start your container which will link your containers and so forth so first of all let me mention the version number for your docker compose we'll say 3 and then we have to mention all these services which you want to learn so do you want to run a web service do you want to run a database service now in this example I just want to focus on the web service so what I will do is I will mention all the services so I just want to run one web service I will say web : and in this web I have to first mention what to build so I will build the current folder so when you say dot it will build the current folder if you have this in some other folder you have to mention the entire path now once you have your build ready of course you want to run your Django application right now when you run your Django application you have to mention the host port numbers see what happens is when you I mean when you request this from the browser browser is external to your container right so browser will talk to the host machine and host machine will have its own network system your container will have its own network system so you have to mention that you want to accept the requests from all the hosts from anywhere for that I will say command and I will say Python you have to manny you have to run your server remember when you work with django this is a command you have to pass python manage dot py runs out but with this you have to also mention the IP and the port number well from where you want to access so I want to access from everywhere so it says 0 0 0 I mean 0.0.0.0 : 8000 now that's the port number I am using you can use any port number doesn't matter the next thing you have to mention is volumes I mean when you when you when you run your application on docker it will have a container now inside your container you can write data the only problem is continue will lose data once you destroy the container so what if you want to store some data from for a long time and that's where we will use volumes so you can also mention volumes of course we are not going to use volumes in this code but maybe if you want so you can mention volumes so the volumes I want to put inside app now which port I want to expose I want to use port numbers 8000 : 8000 and that's it once you have mentioned this your web service is ready to use but will this really work that's a question so until this point nowhere we have actually created a django application we are simply doing all the configuration now once you have done with the configuration it's time to create your Django project but how do we do that it's actually very simple you just need to move to your command line or Vaser terminal so we go to terminal I will say new terminal and this is where I will create a django application now for that you will say sudo docker compose you will use docker compose to create your django application because that's yeah so that's why we have a webserver Bleyer so here we will say it one the service name which is web so this is the web service you have to mention web and then the command the actual command the actual command which we are going to use is Django admin remember this is how you get a Django application and then you have to mention start project and then you have to mention the project name so I have my the main project name as the disco and the Django application which I want to create here is let's say what app I should create I will say my site I know that's that's weird well it's my site and I will say dot this is in the current folder I want all this code I will say enter it will ask for the password let me just do that now it will take some time to create this because it will download all the required layers for you as you can see here so depend up depend upon you to SP depend upon your machine speed it really takes some time so as you can see we have downloaded all the images here and we have the talker applications ready so if you can see on the left hand side we have a my site app and here we have settings taught you we have setting start by we have your got PI this is where the magic happens right but is it really working now first of all even before running the application ok it says we have to install the exchange and for python let's do that extensions are actually very important so this is where the magic happens but then even before running the application we have to understand the permissions here so if I do the least things you can see these are the projects we have so we Darko composed or yeah Mel these are the files we have then we have the most important file which is managed rpy and then we have our app here we have requirements dot txt but then if you see here we have the permissions issue here if you can see this two things have a root permission now we don't want root permission v we want the user to have the access there and for that I would say sudo see it own - I'll I will use user user now using this I can change my permission okay so using this I can change my permission so if I said least so now I have all the permissions here now once I got the permission I can do anything with my files I can delete my files I can create new files I can I did the existing files now once I got that it's time to run this project but how do we learn this now of course I wanted this on this application on the container so this Django application will be running inside the container which is running on the dock or engine and which is running on this OS so we have to go in a dock away and to do that we have to say docker of course you can build this image I can run it otherwise you can simply say docker compose and you can simply say up so if you want to start the container set up if you want to finish it you can say down let's say up it may take some time and it's up this okay it was quick I hope there's no error there is no error let me just try this so go back to my browser so that's my browser it's time to say 127 0 dot 0 dot 1 : 8000 I hope it will work and it's working you can see that we have our Django homepage up and running so yes we have not done any coding as of now and that's why it is giving you a default page but if you got this page that means your system or your your Django is actually running on the container example if I just stop this if I say ctrl C to stop the container and if I refuse this you can see we caught an error so yes it was running because of the container now this is working but then if we don't want a simple application like this to be running on the container right we want to have something maybe I will just want to print hello world if we can do that we can do lot of different stuff so how do we do that now if you want to create a project how will you do that so it's very simple first let's keep this up and running and let's open a new terminal now of course you can use a terminal obvious code all I can use external terminal let me just use external terminal here which we were using from a long time and here I want to use that docker image so first of all let me just see how many persons we are running so you can see we have this process already running here it's going in two lines but let me just explain that so this is the Dutch of container ID that's your image name which is the new scope web that's the Commodities passing to run this application and the important thing gives the name the name of your container is the disco web one now using this container I want to create a new project or the new app so in the existing project we are we want to get a new app that's how Jango works so if you want to have a separate thing you want to do you have to be the app and let's let's get an app here now how do we create an app it's very simple so you have to use the existing image existing container now if you want to execute something on the existing container you will say darker exe C which is execute and you have to mention the name of the image and about the container that is the disco - web - one and then you have to pass the command the command to create a new app is Python manage dot py start app and the app name of course I want to create a app as grid so this will greet a user let's enter is it yeah so if you can see if you go to vs code we have our app ready so it's so simple might just use the existing container and create the app now once you have your app ready it's time to make it run so what exactly I want to create is if I go back to this home page and if I say slash hello it should print hello world that's it as of now it is giving you error which is four not four of course we don't have any pages or the app built for that URL now to make it work we need to go to our vs code and that's where you have to do the coding as of now let me just close all this files to reduce the complexity of the application now how do we do that so whenever I had one whenever I say hello it should print hello boy now for that if you want to do something on this trend you need to use you need to use views dot py that's that's where you define what response you want to send to the user so first of all if you want to send a response you need to import Django dot HTTP and you have to import what I study response now as I mentioned before if you're not familiar with Django I would I would recommend you to explore the Django series on this platform oh I don't have enough permission retry a super user I need a permission here so maybe I just need to do that once again do I have the permission for great yes I have a permission now will it save you it work so of course you have once you create a happy up to mention that you need a permission there now once you've got a permission I will go back here and I was able to save this application now I want to create a function which will accept my request so I was a def and the function name can be let's say hello I want to get a function for hello requests so it's a request and this is where I will simply return the response I mean I'm just writing a way simple code you can make it complex by adding some new features and I just want to return hello boy but will this work of course not right it's not that simple so even if you code it any few you when you field on this application by saying up it it will not work so if I go back to the browser if i refresh if I start afresh nothing is working so what went wrong see when you send a request it will use the we need to have a URL dot py file inside this great there is no way of specifying when to call this function the way you do that is by creating a new file which is you but it'll start B Y this is where you mention your the path so to do that you have to save from Django dot G URLs you need to import path again I'm not explaining this thing in detail in this quote there is a separate video on this you will find that in description area and from I want to import from all I want to import views now once you've got this - you need to specify the URL pattern so you have to say URL patterns and that's what you have to mention multiple parts in your application you might be having or in your app you will be having multiple URLs so we are doing it for hello maybe you want to do it for the payment you are doing for the some other request so you have to mention all this thing in this part in this UL patents I have to say path I am requesting for hello and I will say we use start home so views door hello that's the name is it the yeah so function name is hello so if you save use dot hello that's the function and we are mentioning and then you have to also mention the name of the application name of the app so we'll say name of the pattern but okay so this is done from this side but will it work now even if you save and if you go back to the browser and if you say refresh oh it's not working so much not working it still says hello 4.4 so thing is the main application for your project is my site and in this if you can observe we have you others dot P by this is a main thing so you have to configure this one as well how do we configure this so I have to say path and this is where you have to mention whenever you get a request with hello you need to send request for okay how do we do that so you have to say include and this you have to mention the app name what's wrong yeah so in this you have to mention the URLs of green and the way you can do that is by saying great dot URLs so whenever you get a request for hello this is where it has to map oh that means we don't need to mention hello here okay let's try I hope this will work I just I can just hope now let me just say up again oh we got an error it says what include is not defined oh my bad include has to be inputted here how can I miss that so I will say up again and it worked this you can see there is no error I hope this will work on the browser as well let's refresh the browser oh it working so that we got a lowboy it's that simple that's all we can create a django application on on taco so basically you are talking at this on the container so you can just pack this image deploy it on the or push that on the docker hub and anyone can use it without any installation of course they have to install dock others this will not work so just to give you a quick recap what we have done first of all in your machine you need taco and you also need vs code or any data type for that matter now once you have darker you need to create a Django application the way you can do that if you can just follow this comma steps here that's a new steps I guess we have done on the terminal now there's a first step is you have to create this folder which is the risk of course you can create any folder and in that you need to create three files which are very important one is the daka file which where you have mentioned what is your base image and what requirements you have and you have mentioned all the requirements in this special file which is requirements dot txt this is where you have recommends dot txt when you have mentioned we are using Django and in this Jiang and then we have gone for the cocoa compose where you have mentioned what to do when you start your when you start your the start your container you need to run this server and when you run the server Django was working but then we wanted to have our own own app there and the app which we have created is great we have written some command like this although we have the command to execute that you will find this command in the description and for using the idea created this app and in this app we have to configure the views dot py you have to configure us dot py so that we can give the path and this in the main path in the main yours or py as well you have to mention the path and that's it if you want to add new features create new app create new you are I mean create right some URL path create some functions and this will work so I hope you enjoyed in the next video we'll see how do you container is the existing application as I mentioned there are two ways you can start the application you can start building the application on docker which we have done now or you can build the application and then can't rise it so that's it from this video I hope you enjoy let me in the comment section and do subscribe for further videos bye
Info
Channel: Telusko
Views: 39,306
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: XDaQAmkDFX4
Channel Id: undefined
Length: 26min 58sec (1618 seconds)
Published: Mon Apr 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.