Docker Compose | Build and start a Django project with Docker Compose & work in a Docker Container

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is xander welcome to a docker compose and django tutorial this one's a little bit tricky to explain i've named it how to build and start a django project with docker compose so just bear with me while i try and explain this one so by the end of this tutorial we're going to have this type of setup so we're going to describe in a django compose file how to start a django project we'll then build an image and then go ahead and run the docker compose file and that's going to then create a new container and then start a new django application and the twist is here as well we're going to link the docker container volume so the um the folder where django is installed we're going to mirror that across to a to our operating system and then have that opened in visual studio code so we're going to be working with django as we normally do in visual studio code but anytime we make changes in visual studio code it will also update the docker container so that way we're going to serve the django application within the docker container so you might be thinking what is the point of that i'll explain that in a short while so just before we start the tutorial if you are new to this series don't forget you're going to need to have docker and docker compose installed typically they come as one package you install it as one and then of course python a code editor here we're using visual studio code and then ideally you've already seen part one of this docker django series so just quickly then take you through the steps here we're going to create a new requirements list now again you can just skip steps if you want to the video timeline is in the video description then we're going to create a new docker file so you should be familiar with that from the first tutorial then we're going to build create describer docker compose file and then build a docker image and then we're going to go ahead and run the docker compose files that's really the steps that we're going to take so once we've done that we'll spend some time at the end going through the stages of starting and stopping the django application building multiple django applications from the same image for example as well to get familiarized and comfortable working in this way so you know where this is heading in the next tutorial we use the same setup but define multiple services so here we're just going to focus on the single service using docker compose just to familiarize ourselves with this setup so going back to that question what is the point of doing this well let's remember that here we're going to be running a container a container is independent from our operating system it's contained all the services software is all contained within the docker container and that will then prevent any conflicts or compatibility issues with our main operating system that we might have when we've had to install a database or redis or other software within our main operating system so here what we're doing essentially is we're describing what services we want to run in our docker compose file and then we're going to run those set them up and run those in our container now because our container is running linux it's a consistent code base we know that any time we run any of these services in our container it's just going to work we're not going to have compatibility issues with windows and so on right so i've gone ahead and downloaded the previous code from the previous tutorial in some respects it doesn't really matter at this point um if you've done that or not let me just show you first so remember we're not going to have a django application to begin with right so this is the previous code from the previous tutorial so i'm just going to get rid of the core folder with all the django files in it don't need that i'm going to get rid of the the database don't need that and the manage pi file so all i'm left with here is a command file um which i'm just building for you so you can reference back to the commands that we've utilized that's nothing to do with the application at all and then we've got the docker file uh from the previous tutorial and the docker ignore file so we can actually get rid of the docker ignore file now we don't need that so that ends up with us just needing the requirements text file which i'll just remove that and in addition to that the dockerfile that we previously had so that's all we're going to need to start off with so this is where we left off in the previous tutorial so the docker file allows us to build say one service so a single server so the idea here with docker compose is that we're composing multiple service we're describing and configuring multiple services so we are going to use docker file to set up our image so we're going to need an operating system a working directory and copy across like we've done before the requirements text and install it but we're not going to need to copy anything over again so we don't need to worry about that and then we're going to configure all of these other settings in our docker compose file so let's get rid of the command down we can squeeze all this up and that's pretty much it that we're going to need there so there is one additional setting here and that's the python number equals one so we're going to set this to a non-empty value so basically this is going to ensure that any python output is sent straight to the terminal so any um error logs for example is going to be sent straight to the terminal which is in this case our container log so what that essentially means is whenever for example let's give you an example of an error when there's an error that error will then go to the command line so you can see the error that information will get piped to the the command line so you can see so or the terminal sorry so what might happen is that data might first get buffered and when there's enough of that data it then will get forward forwarded to your terminal so the idea here is that we're going to set this unbuffered to one and we're going to say any data is just going to be sent directly to the terminal without being um without being buffered without being stored maybe to a certain size limit before sending across so it just allows us to work in in real time essentially we get the messages straight away okay so now we need to build a docker compose file and this is essentially just a yaml file which is if you've not used yaml before it's just a it's gonna look like a text file um a human readable text file um yaml is a data serialization language um so all we really need to know is that we need a yaml file so we're gonna need a dot yml extension to this and we're just going to describe in in clear text really what's going on so um by all means have a look at yaml files or the yaml syntax if you want to learn more about that right so let's crack out a new file here docker dash compose yml there we go so welcome to docker compose this is where we're going to start here now the first thing to understand about dock and compose there's different compose specifications and that's pretty much all we need to know just make sure that you potentially using the latest specifications so here loosely speaking in different specifications there may be different ways of describing or performing different actions so you can see we started from 1.0 here looks like we're output to 3.8 so the first item in our docker compose file normally consists of what version that we're working in so here we could just say versions three for example if we want to or be more specific 3.8 so back when i described what docker compose is for just to reiterate again here we're building or defining services that we want to run in our container so you would imagine at this point this command will make sense we're now going to describe our services that we want to run so here in this tutorial we are going to only be running one service uh in the next tutorial we'll run multiple databases i wanted to make that into a separate tutorial because i wanted to show you multiple setups so this makes sense hopefully now we've got a service that we want to describe so we're going to be we want to build an application we could call this django i'm just going to call this app so that's the name of our application so i'm now going to indent further and start defining this application and how i want to build it so next up is build so this is a powerful tool um which we can utilize to configure other options that are applied at build time so next up we're going to describe the build so here we could define multiple options or configuration options that we would then want to apply at build time here we're just going to specify the dot so the dot here refers to this folder here and you can see that we've got our docker file inside of this folder so now we could just describe this as we're just telling the build that the docker file um exists and we can utilize that to support the the build so next up we're going to define a volume so before we do that let's go back into the dockerfile let's make uh our working directory called django so i'm just going to change the the working directory to django hopefully that just gives us a little bit more context to understand this so a volume here in this context is describing a folder that resides on our operating system within the container right so what we want to do here is by utilizing volumes we're going to set up using dot uh so dot referring to this folder here our project folder and then we're going to match that we're going to copy that mirror that or make a link between the containers folder and that's the slash django folder that's what i've just set up here so when we we're going to install everything inside of this django folder on the in the container remember the container is running an operating system so we're going to build a new folder called django and then put django application within this folder so we're going to basically just copy this folder so dot is referring to my machine here this folder here if i wanted a different folder or make a new folder i could change the structure here to that um so if i had a folder asd over here and i could mirror it across to there so colon and then the folder that's on the container in the container in this case slash dango so that's going to build my link between the folders and my folder that's going to be on the computer now remember what's going to happen here is that initially when the application is built a new folder will appear i can go ahead and do whatever i want in this folder that is saved on my computer that's the important thing is that it's saved to my computer when i close or delete the container everything is saved still on my computer and i can just then build a new container update the data before i build a new container in a natural fact we're going to show you this and then it'll just work as normal again so that builds that link so we're going to need some ports so if you remember from the first tutorial we're going to map across the ports so we can access the container so port 8000 on our computer to port 8000 in the container here so that's going to expose the 8000 port in the container so that we can link into our django application using the 8000 port and now we just go ahead and we can name the image or give the name a tag so we say image so the image is going to be called app because we've got app here so we want to rename the or tag this image so we're going to just tag this image as django and we can then run the the image with the tag which is going to be django so you can change that if you want to so i've just called that django and then we can then go ahead and actually um name the container so the container that's made from this we can name this so i'm just going to call this django container so you can see that in place and then the final command so we want to run a command now so where previously we run a command so in contrast here we can do this without all the double quotes so just run the command python and then manage dot pi we're just going to start django run server and then like we did previously 0.0.0.0 to explo to expose on the container or ip addresses and then the port number 8000 so this is tricky to explain here y zero zero zero not one two seven etcetera so like i said zero zero zero zero networking terms uh zero represents something a little bit special and here we're saying to represent all ip address um from this service um and expose all of them over to port 8000 so definitely head over to the docker docs and select the command line references and then the docker compose reference over here here you can find all the the main commands that you can utilize in docker compose now to build an image here if you remember docker we just use docker build and then maybe the we can tag it like we did in the first tutorial so here we've also got docker compose build now to build an image in docker compose there is a few commands that we could use so we could use build um or we could in actual fact um use run to build a an image now i'll start off just using the docker build so or docking compose build so let's go back in and run docker compose build so here we're just going to create an image and prepare it for them to run our docker compose file so if we go back into our docker desktop here you can see that we've got now a new image that's created we've got the tag as we've described in our docker compose file so that's all in place now we are using a slim version of linux um that's probably worth noting so there's no container running at the moment so we've just got our image so what we can now do from this if we wanted to is run a second command so next up then we're going to run docker compose run so like i said the run can actually build for us as well and just do the whole thing um but i just wanted to show you the fact that you could docker compose build if you wanted to do that and have a look at the docks and the comparison between the build and the run command because they do slightly different things with the run there's more parameters that we can provide and we go through that right now or at least some of them so docker compose and then run right so i'm going to use the the rm so docker run compose run is going to build a container for us but i don't want it to do that initially because i want to start the container separately because i want to potentially perform some other actions before i run the container so i'm going to remove the container once everything's run and then basically i'm going to select what i want to uh the image that i want to use so that's going to be the the app image so that's my app image and what i want to do here is i want to run a command so using compose run it's going to allow us to run a one-time command so if you remember what's happening here in our compose file at the end it's going to run this server now at the moment we don't have a django application we don't have any volumes notice we built the image but we don't have a volume yet at all and this is only going to appear and start once we've actually built the container so these are commands for essentially the container so here we're going to select the app image that we built and now we're going to define um a command that we want to run before we then go ahead and actually start to run the server and that's obviously to build a django application so django admin start project and then the project is going to be called core and like i normally do a dot at the end to specify i want to run it from the folder and not a folder within a folder right so let's go ahead and do that so that might take a couple of seconds okay so what's the result of that well it looks like absolutely nothing at the moment because i've stopped a container um but if for example now i go back into visual studio code here you can see that this has run because you can now see i've got a folder called core i've generated a new django application and here i've got my manage file and i've got my core folder so everything is started so what i can now do is to show you the next command and that is docker compose up so that's basically going to start the container you can see that it's now running now we are locked in here to the container so these are this is the terminal what you're looking at here is the the commands that have been captured from our container terminal and they're showing them here so you may be familiar with this our unapplied migrations because this is a new project we haven't migrated yet so it looks like it's running okay so obviously the last check we can do is go into our our server and there we go so let's not forget we bound we binded the port 8000 to our django container so now what you're looking at here is django running inside of our container so if we go over to docker desktop go into our containers you can see the f is running and you can see the django container is called django container because that's what we specified so where does this f come from well f here is the name of my folder that's holding all of these files that's why it's called f so what you built now in actual fact is a based image this image here is a base image you can now use to run multiple django applications if you want to so let's do that right now so let's just open up a new window it's going to be quite larger to apologize uh so let's just give this slim this down a little bit so we're just going to go ahead and create a new project so i'll build a new folder and i'll start that up and i'll see you in a second okay so i've made a new folder i've just copied and pasted the django compose file that we built earlier in the dockerfile over to my new project folder called g and that's pretty much what i need here so i've got my image already made so there's one change that i need to make i can't run a container on the same port as another container for obvious reasons i can only connect to one i can only connect to one point at a time with one application so i've changed that to 8001 this time nothing else has been changed so let's go ahead now and run the same command again so docker compose run rm app django admin start project core right so let's go ahead and well actually yeah let's go ahead and do this first because what we want to do is we want to make a new project called core and get a new folder so let's go ahead and do this so you can see that's run okay i've got my new django project right here and now i can use compose so docker compose up and then start my new container okay so this seems to be a bit of conflict um you have removed oh so looks like we've got a naming problem so we've already got a container called django underscore container so let's call this number two there we go so now it's working okay let's go back into here so we don't we didn't make a new image we're using the same image we go into the containers i've got two containers now one called g one called f and inside of here um there we go we've got these different services so let's go back into here we're gonna go to 8001 now we're gonna see that we've got another django application so now we've got everything set up i'm just going to get rid of this g for now so let's go back to the f the first build so now we've got that i'm just going to go ahead and delete so i'm in apologies you can't see this i can't move this down any smaller so i'm just going to stop on the right hand side i'm going to select the container on the right hand side i'm going to delete the container on both all the services in the container so that gets rid of the containers and then i'm going to keep the image there i don't want to move that so what i want to do now i've got my application here i can run my application right here if i wanted to like i normally do so i can work within a container or not so i run that and it's working exactly the same and i can view exactly the same right because the docker compose file in the dockerfile isn't affecting my application running it doesn't isn't dependent upon my application running here locally so what i can also do now is i'm just going to grab the files from the first tutorial and i'm going to drop them in here and update them about me one second so i'm going to go ahead and just update the urls um so that was from the first tutorial apologies let's get rid of this so i created import views and i created a new path for the home and then i'm creating a template again this is just the code from the first tutorial so that's what i said hello and then i also had a view a simple view that connected up my template so obviously at this point i've made some changes to my broad project right that's what i wanted to show so all i need to do now is docker compose up again okay so um you can see that everything has been retained um but now when i go over to my project this is 8001. sorry zero uh it says template does not exist okay bear on me a second that is because i need to just register the core app within the settings that's something i need to do manually okay and then go back to and refresh and there we go so we're now serving again our project from docker we've mirrored across all the data to docker and you can see it's working so we now have django being served again from docker so if you go into docker desktop for example and select your container on the right hand side here if you drop this down sorry uh actually we can do it from here so if you drop this down you can see our container we can select the cli so that would take us directly into our container now remember our container is running an operating system a small version or slim version of sorry linux so we can run linux commands so ls for list so this is the working directory that we're working in currently and you can see all the all the files here so if i type in cd then slash that will take me back to the root directory and press ls to list all the items so you can see our folder called django and then i can cd into that folder again and then i'm inside of that folder again so here i can actually inspect all the files so if i just go into change directory and then core and then list them you can see here we've got our views that's been copied across and mirrored across from our django application that we have stored on our computer that we're working in with visual studio code so you can see that yeah is that actually happened it's actually mirrored across all the data over to the template over to the container okay hopefully i don't need to embellish any further you've probably got it at this point what's going on so have a go have a play and see how he gets on and like i said in the next tutorial we will extend the the services in our docker compose file to include databases so i do want to install maybe a few different databases to give you some understanding of how to set up multiple databases in docker compose with django and there we have it thank you very much for listening hopefully that was useful if you do like that please like and subscribe and do those things and hopefully i'll see you in the next tutorial
Info
Channel: Very Academy
Views: 8,449
Rating: undefined out of 5
Keywords: docker, docker compose, docker volumes, django docker, django tut, learn django, django beginners, beginners django, docker container, docker windows, learn docker, docker tutorial
Id: aMqs_y6dZw4
Channel Id: undefined
Length: 25min 55sec (1555 seconds)
Published: Thu Apr 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.