Building a Django Docker Container

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the Django salary Mastery course just a quick reminder if you like this course and would like to access the source code and more you can access this course on udemy the link to the course which should provide the best price is in the video description we're going to start off by preparing a docker working environment for Django so building a Django container now if you are already familiar with Docker and you feel comfortable doing this then do please skip this step you will find the code base in pretty much each section of this course so just go ahead copy and paste the setup or create your own setup of course and then move to the next section so as a quick reminder before we start this you are going to need to have python installed and dock a desktop now this should be quite straightforward process I have left some guides at the start of this course for you to follow again just to reiterate if you are familiar with Docker and Django the scripts for what we are developing in this tutorial is available in this course sections resource I am making the Assumption you know what Python and Docker desktop is although I will take you through some of the preliminaries as if you are new to docker right so let's make sure that Docker has been installed Docker desktop this is what you should look like or something similar it may look slightly different depending on what version you have if it's a much later version than when I recorded then it probably going to look slightly different but the Dynamics and the features are probably going to be the same in this course I'm going to be utilizing Visual Studio code which I'm kind of making the Assumption again that you're familiar with now again I probably have provided a setup guide for this at the start of the course as well as giving you a little bit of an overview on how to start utilizing it so please utilize that if you are new to visual studio code and you want to follow along step by step so my first thing is to make a project folder and then open up file open folder I won't go through that process but open up a new folder go to the folder where you are going to be saving your project open that folder you should be presented with this screen right so next thing you need to know is how to open up the terminal I'm going to be utilizing that a lot so you can just up the top here which you cannot see I realize that if you go over to view the view option in the list here you can then drop it down to the terminal item here you can see there is a shortcut on Mac here but there will be a shortcut on Windows as well so you can use the shortcut rather than go into there to open and close the terminal so I can just use the shortcut right so that's the terminal so now we need to create the virtual environment again I have created some guides for you if you're not familiar with that so let's go ahead again I'm working on the Mac to apologize if you are on a different operating system so whether you're on Mac or Windows you can probably type in Python 3 use the m flag and vmvmv so that's going to create a new virtual environment like I said there should be a guide for your operating system at the start of this course and then on the Mac I'm going to type in Source vemv bin activate that's going to then activate and then allow me to go into the virtual environment so I can start building now if you're on Windows you're going to just type in venv slash and then I think scripts slash and then activate you can use tab so here I just type in a then press tab it will then complete the command right so now you can see VMV I'm in the virtual environment so I press Ctrl K to clear if you're on Windows you can type CLS to clear the screen apologies to clear the terminal not the screen the terminal okay so let's go ahead and install Django so pip install or install Django so that's going to install Django from the python package index so I'm going to press command and K again to clear that right so with that done let's first of all set up pip requirements requirements um yeah we do it we do no we're not going to do it there so let's create our Django project first and then we'll create the PIP requirement so uh Django admin let's start a new project let's call this project D salary so Django celery so that's what we're going to call a project you can call it whatever you like this is just a demo project if you'd like for us to start to explore some of these salary functions and features so let's start a new project notice I didn't use the dot at the end to to note where I wanted to save the project so that has created a new folder called D salary and inside of that we have the project folder the salary with the settings so that's my setup there so I'm going to CD into that yeah I'm going to CD into that change directory um the salary if you're not familiar with that so move into there and I'm going to pip install and pip sorry pip freeze the requirements create requirements text so there we have the requirements which are going to be needed when we create a new Docker container or Docker image now what we're going to do here is to install some other dependencies that are going to be needed so we're going to pip install a celery and then in addition to that we'll also go ahead and pip install redis there we go so we just run the pressing up on the keyboard just going to run the requirements again just to update the requirements and that should include now all the dependencies that are required to run Django redis and celery I will also be making a commands file here which you can access through you can access by downloading the code in this course so you'll be able to check out some of the commands I've utilized I will start to copy and paste some of those commands I've been utilizing in case you forget or you want to make some notes to save you having to kind of rewind and find when I use the commands so I will be saving this to Source control so I will go ahead and create a git ignore file and inside of here we just want to ignore the vamp folder and then in addition to that we probably also want to actually I've got a list here it's family list there we go so those are just a few things I'm going to ignore for Source control Reasons I'm not going to save that in Source control if you're not using Source control just ignore that don't worry okay so we're going to be utilizing Docker so let's go back to Docker here now just very briefly images and containers so very briefly then if you're not familiar with images and containers in the context of Docker an image is a lightweight or Standalone and executable package that encapsulates everything that we need to run a piece of software this includes the code the runtime environment the system tools and Library so very much like an operating system if you like in that we can grab everything that we need to then run a container let's think of a container as a a unit an individual unit on our computer that we can then run applications and other things like we do with our main operating system but of course we're sharing the resources of our computer with the container now this image that we're going to be downloading serves as the templates or blueprints for creating our Docker containers so what we're going to need then is this image to create the blueprint for creating our container and then we're going to provide the instructions in a Docker file which is going to specify the configuration and dependencies of the software application that's going to be needed to run in the container that doesn't make sense hopefully it will do in a second so what we're going to need to do here then is we're going to need to describe or build a Docker compose so we're going to be utilizing Docker compose because we're going to be building a few different services so Docker compose files it's going to allow us to describe multiple services that we're going to build for our working environment so we can start to use salary so inside of here first of all we're going to describe um we're going to describe like I said the services that we're going to create so here at the top we can describe the version code that we're going to be utilizing here in Docker so that's going to be 3.8 now all of this is available for you to view if you're interested in Docker and you're new to Docker have a look at docking pose documentation that's going to give you a much deeper view if you're interested in the code that we're using but I'll provide you a general overview we'll we will run this line by line I'm not entirely sure why I called it MD apologies I was thinking about something else at the time it's going to be a yaml file okay notice that the how the docker icon appears on the left hand side of it to indicate the fact it is a Docker compose file right so we need to be careful here because we need to make sure that we provide that we're providing the correct um spaces um between the code so services so I have um gone ahead and indented this by one so service so first step is that we're going to be building then is going to be Django so we can call this web I'm going to call this Django this service and then I'm going to go into indent again sorry and then I'm going to give this container a name and that's going to make it easy for us to actually then navigate into it you'll see that shortly we can we can hook into our container and view the terminal use the terminal inside of the container so that we can provide additional instructions like we would do for example if we were running Django on our computer so I'm going to name the container Django so we can easily access it and then I'm going to describe the build right so we're going to need to provide like I said some additional instructions so that we can build I'm going to context so that we can build our Django application inside of our container so here I'm going to call this D celery so this is referring to this directory folders top level directory folder here dot this folder directory and then slash so slash D salary so that's going to be this folder here so inside of this folder I'm expecting it to be able to find the docker file so I'm going to add a Docker file here I'm going to add Docker file there so that's my Docker file this is going to describe shortly actually how to set up the Django environment right so we'll do that slide in a second now apologies not Destroyer put it made a capital there okay so that's lowercase right so that's the docker file now we can specify that here so we could say for example Docker file and then the name of the docker file because we don't have to call it Docker file but because we have called it Docker file Docker will look for that file first so we don't need to do that so that's the build um that's the build settings let's bring this down so we can see what we're doing okay so we'll described that we'll sort that out in a minute so if we work for the workflow at the moment what we've got is we've told Docker where to find the instructions to kind of load up everything that's needed to run Django in the container so once that's done we then create a new command so then we can then start we can then start Django in the container so this is a command that we're going to use in the container run server and then that's going to be 0.0.0.0 and then colon 8000 so that's going to run Django in the container with me so far if you are new and then what we need to do right so this is kind of a tricky bit we are skipping many different lessons on Docker here but what we're going to need to do is let's remember I try to visualize this eventually we're going to have a container here and that's going to be running a live version of our Django application so let's imagine that a container is an operating system a computer on that computer there's an operating system and that's running Django so we're configuring that to be available we're configuring that to run Django now what we're going to need to do is we're going to need to make sure the code in that container is up to date and is running the current code that we want to run correlating to our Django application and we want to manage that code within our Visual Studio code platform here so what we're going to do is we're going to use a volume which is going to mount this local directory where our app directory is inside the container so we're going to essentially create a copy of this code inside of the container we're going to link it up so that whenever we make changes here in our local machine it will update the code inside of the container remember the container is like a computer running our instance of Django let's not forget that so here we're just going to in our local machine we're just going to edit the code we're going to manage starting and stopping the server on the container so what we need to do here is just tell docker about the volumes the fact that we want to mount this local directory into the Container so let's call this volumes and then we need to specify so here we can use the dash so I have tabda Dash d slash I'm sorry dot slash now we just need to remember that this is a doc composed file this is where it currently resides so dot slash and then we just need to describe what folder we want to uh what folder we want to mount to the container and it's this folder here this decelerate folder that says everything in related to Django so D salary and then we needed to then describe on the container where we want to mount it to so in this case it's going to be user slash if you're familiar with Linux this potentially is going to make sense this is the folder structure inside of the image that we're going to be utilizing so inside of here we have a folder called app so that's where we're going to store our Django application in the container so we're going to copy our folder here over to this folder there right so we are going to need to be able to access our containers I'm going to set up the ports here I'm going to set Port 8001 on my local machine so I'm going to be able to access my container from my local machine from my browser so I need to type in 8001 port and I'm going to map that across to Port 8000 on the container and that of course is where Django is running so I'm just mapping that across so that when I run this in my browser it's going to map across to 8000 in the container and the show me Django right so we're also going to need some environment variables so I'm going to set up some environment variables for our application um this isn't necessary but while you're here I might as well Adam so debug equals one um now you will find I will probably added some comments if you were to download this to give you a better idea of what we're doing here so secret key equals and then we just need to generate a secret key for Django whatever you like for now we could also obviously make Django do that but that's going to be fine for now so we're going to set the allowed uh host that's going to equal localhost maybe and then 127.0.0.1 so that's what those are going to be allowed to connect um okay uh so let's go for okay I think that's actually I think that's it I think that's all we need to do there so we've set up some environment variables so that's pretty much it for our Django container in terms of uh setting up so that we can run the Django application inside of the container we've mapped across the folder so that we can work here in Visual Studio code but update the code in the container and then we set some environment variables so we are going to need to of course Define these environment variables within our application so let's go into the main settings here let's go to the top I'm just going to remove some of this code here these comment sorry for now so we need to get rid of the secret key and then debug and then we're going to set the allowed house okay okay so we're gonna need to access these environment variables in the container so we are going to set them in the container so we need to access them through OS Environ get so that would be the same on all of these here so we can just copy and paste that down you see that we're going to need to import OS so that gives us access to the operating system file system for example and other OS features so here we're going to need to describe what we want to access so in this case the secret key that's going to be the secret key inside of we're just describing what we've got here so secret key we've got debug so this is going to be debug and then the allowed host I'll just copy and paste that in there so that's what we want to access so that's going to be accessed from the environment variables now we have specified multiple environment variables for the allowed house so in actual fact here we're going to need to Define split just to split the configuration based upon the commas there so we're just simply just taken out extracting all of these different options there so that we can apply them correctly to the allowed house okay so that's Django Now set up so let's go back into the we're going to need the docker file now so let's go back into Docker file we need to just specify some of those settings so at the moment we've got some general instructions for the container so that we can run command in the container once everything's been built or when the container started we know to map across our folder here we know to access the container and we know to set some environment variables so now we need to actually get our project over to The Container so if we go into the docker file here that we created inside of the outer or inside of our main decelery folder here let's go ahead now and just Define what we need here so first of all we need an image now remember what I said the image is like a blueprint of everything that's needed to then run our application so here we're going to use 3.11 now this might change obviously depending on how old this tutorial becomes so as a recording 3.11.4 Alpine so this is a Docker image a pre-created Docker image which has a copy of Alpine or should I say Alpine Linux so this is a lightweight and minimalistic Linux distribution that is very commonly used as a based image for Docker containers so Alpine Linux is known for its small size and focus on security and it makes it simply an ideal choice for containerized environments so to be sure that um you select Alpine if you want to follow along step by step because these different versions which you can utilize will have different Linux versions We have different features so some may work some may not based upon the settings that we apply going through this project so do be careful if you do want to follow along step by step you've utilized the same versions now I can't guarantee that I'll be able to keep this tutorial up to date so it is also important to have a look at the requirements as well and just to make sure you are utilizing the same here and then if you do want to explore later versions then please just update them individually I cannot always guarantee of course that they the features and what we set here in this course will work on the newer features of any of these different packages okay so that's our Linux version if you like we're going to be utilizing alpon Alpine so let's go for our work directory so we're going to set our work directory here remember that's the working directory inside of the container so that's going to be the app here that's what we defined earlier and then we're going to need to set up some we don't have to but typically we set up two different parameters and I'm just going to bring them in because I've got a setup file here that typically use so here first of all we're going to set python don't write bytecode one which is going to prevent python from writing any pyc files and then we're just going to ensure the output is sent directly to the terminal without buffering so we get all the output to the terminal setting python unbuffered one right so those are two kind of common settings that are normally set so what we need to do now is now pretend what we're doing now is we're building the image and we're setting everything inside of that image before we then build our container so what we want to do is we want to pip install we can do an upgrade let's just make sure that we're using the latest version of pip and then we're going to go ahead and copy the requirements file so dot slash requirements.txt now let's just remember where this is so remember dot referred to as directory slash and then the name of the file so we're here in this Docker fault so that's where we find the requirements file so that's right it's text and then we want to what we want to do is we want to copy it over to our working directory so SRC slash app so once that's over there okay so once that's over there it's so simple to spell that wrong once that's over there we're going to then go ahead and run that so we're going to run pip install and then use the r flag because we want to run from requirements.tech so we're going to run all of that we're going to run the requirements file and install all the dependencies that are described in our requirements text file alright so that's next step and then what we want to do then is to copy right so we're going to create an entry point file this entry point file is going to be run every time we run the container so these are typical tasks that we might want to run when the container starts for example in our case in Django we want to maybe collect static files maybe or maybe run a migrate so I'll just quickly show you this because this can be a fairly good thing to know when working with Django and Docker right so let's go ahead and add this file in and create this new file I'm going to call this entry point dot is going to be a sh file okay and then at the top here we're going to have the shebang all right there we go now this is import okay so if you weren't running uh apologies let me zoom out if we weren't running Alpine potentially we would have bash The Bash shell available in that particular version of Linux that we're running in our container now because we're using Alpine we need to remember that that runs the ash shell so we need to specify the ash shell here of course if you're not familiar to this format here this is known as the shebang or hash bang line in the unix-like operating systems Linux for example so this is a special comment placed at the beginning of the script to indicate that The Interpreter or sorry to indicate The Interpreter that should be used to execute the script I got it out there okay so we've got that in place now we just need to specify what we want to run so here just for an example um let's go ahead and we can Echo out something that's a crap like apply database migrations um if we wanted that for example in the terminal maybe and then we can run python manage dot pi and then migrate so that's just going to run like I said every time the container starts and then we can execute the provided commands or empty point so we can type in ex and then dollar dollar come on can't find the keys there we go so here in this shell script that we're developing this command here is used to execute the command or entry point provided to the script so it's common practice to include this line at the end of a script that is meant to serve as an M as an entry point or execute a specific command so this is going to be run on our container so we need to specify that so we're going to need to copy that across first so let's do that now why we are copying this across because from memory this will not be copied across because in a minute we're going to copy the whole project across to our server but I believe that would not copy this shell script it would simply not include it so we have to do this manually now there is something else that we need to potentially think about here which might not work and that's to think about again on Linux or Mac if you're following on Linux on Mac um this could be the case so what we might need to do in addition um but we might need to do is to ensure that this file the entry point file has been set to the right permissions so it's just well worth going into your terminal here I'm going to type in chmod again here on the Mac you probably won't need to do this on Windows I'm just going to set that to plus X here and that's going to ensure that we can then access it on the server so you can just go ahead and run that if you're on Mac or potentially Linux now do make sure that you're in the right directory to do that of course I was in the directory where the entry point file is currently being stored okay so the last thing that we need to do then is go ahead and now just copy the whole uh copy the whole project over to our working directory so again we'll use copy Dot and then so dot referring to this folder where the doc file is stored okay so we're going to copy everything inside of D salary that's what we're saying and then that's going to copy be copied over to slash user slash SRC slash app slash okay so that's where everything is going to be copied across too and then at the end here we can just then specify the entry our entry point I'm going to send and then capitals entry point and so we then just need to specify slash user again this is the directory SRC where we're going to find this in app and then we're going to find this because we've copied it across we're going to find Andrew point.sh and there we go so that's going to be run every time the container starts so to summarize we've now created our first service our Django service we're going to Define and create a new container so we specify the container name where we're going to find the docker file to actually build what we want to be included inside of our container as well as a copy of Linux and then we'll Define some other parameters to get the server started as well as be able to access the server and pass it in some environment variables for a little bit of security Now obviously when we're building the image we're going to need to specify how to set up Django so we're going to copy across our project and then we're going to well actually we're going to first little PIV install pip or just upgrade pip sorry and then we can install the dependencies then we're going to run the dependencies to install them sorry and then we're going to go ahead and make sure that the project files are over and ready to run as well as run any additional setup commands so once that's done we then can start the server and then set up the volumes and port and so on and everything should be running nicely so let's now give this a go right so first of all actually let me just update this oh no that did not work so the command I used earlier to cmod so add that in there so the next command we're going to be utilizing assuming you have installed Docker of course is going to be Docker compose so we're going to run Docker compose and then we're going to specify um how to actually start or build our new image and container so we're going to use up okay so that is basically going to read now our Docker compose file and set everything up now I'm going to set everything up to kind of work in the background so what's going to happen is it's going to start running Django and that's going to be running in the background um of our new container so I specify D and then dash dash to build so that's going to build so what we'll do now is we're specifying the fact that we're going to build everything to build a new image build a new container bring it up when it's all built and then run Django in the background right so that's build and I think we are done so let's give that a go right so that did not work at all so let's have a look additional property service is not allowed so that is indicating the fact that are the indentations are incorrectly described or defined so that's our doc profile yeah yeah so root something about apologies trying to close the terminal there we go so something about root so service let's just have a look my apologies so it should be Services um so services so let's give that a go again and this time we're ready to go so what's happening now then is that we are downloading a copy of that image that we've described the copy of Linux that we have indicated in our Docker file here Alpine and it's going to go ahead set up all these environment variables you can see that the container has started and so we should now be able to go back into docker and you can now see that we have our image that we created and the container now notice I called my container Django that's what you can see here so the name of my container is Django so you can see the ports it's mapped to so what I can now do is open up my browser in my local machine well in actual fact I can't do that because you can see here that it says it's exited okay so it's tried to start the container but there's an error so all I need to do is click on the container this is important because you will no doubt have an error at some point so if I click on the container we're then provided logs of what happened you can see down here we're told that the entry point line six potentially there is a problem now if we go back into the entry point code here the problem is here that we didn't add a space so we are going to need a spacer that's it that's all that the problem was so we'll just go ahead now we can run the same command again Docker compose up build so that should rebuild everything and we should now be able to go into Dock and you can see it's all green it's running the status here is running so everything is good to go so let's go back into our browser and let's uh refresh you can now see that we have successfully installed and we're running Django from the docker container don't forget that we are using Port 8001 so make sure you type that in you probably cannot see that so let me see if I can just uh bring that down slightly I don't know if you can see that but this is the the domain if you're not familiar with that 127.0.0.1 8001 if you're not too sure you can simply just go into the docker container here and it will tell you where to find in this case it doesn't really give you much information but it tells you the port number that's running in the container so just make sure that you are typing in 127.0.0.1 Colon 8001. so that is Django setup in Docker at this point if you aren't familiar with Docker and Django then do just have a play around with those commands just very briefly if you are having any problems sometimes it's good just to start again now there is a command where you can just delete everything but what you might want to do is just go ahead and delete all the containers we will do it many times and then all the images and then just try again okay so sometimes that's the best solution when retrying some different code options just to delete everything and start again and run the command now if you are having any problems at all then please leave a comment in the Q a and we will no doubt get back to you and help you if we don't get back over to someone else potentially will also help you if there are any problems or any issues with the code base then please let me know I would definitely want to make those changes to make it easy for everyone to be able to follow along and then also don't forget all the commands that we've utilized so far and from this point onwards we'll be in the command file here and you will find this document as well as all the other code here in the course lesson resources
Info
Channel: Very Academy
Views: 22,082
Rating: undefined out of 5
Keywords: django, django celery, python celery
Id: PkynrL1aU9o
Channel Id: undefined
Length: 34min 10sec (2050 seconds)
Published: Tue Aug 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.