🐍 Django with Docker 🐳 | How to Dockerize Django Apps + basic Docker commands

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to cover the basics of Docker in order to dockerize ajango application and we will talk about some of the most important Docker comments that you absolutely need to know so first of all let's start with a few definitions Docker in simple terms is a tool that lets us pack distribute and run the software in isolated environments which we call containers and these containers run independently but they share the host system kernel and to create a container we need a Docker image so we are going to create this Docker image very soon and this Docker image basically is a lightweight Standalone and executable package that includes everything that is needed in order to um run some piece of software including the code runtime um libraries Etc and a container you can say is an instance of a Docker image which is used to run the images and basically this approach leads to many benefits including improved resource utilization faster and easier deployment as well as simplified scalability after the brief Theory introduction we are now ready to dockerize our D Jango application and for this I started a brand new Django project which I named the back end so inside of the backend directory I have the core which is our main project folder here I also have the DB SQL SQL light 3 so this means I ran the migrations and I have the manage py file so to sum up I've started a brand new Jango project and I ran the migrations this is basically it what I have done um as you can see I also included the Tod do MD file and this is a cheat sheet for this tutorial Series so today we are starting with the back end which is about dockerizing um a simplified version of ajango app probably we will need part two for this but after today's video you will be able to dockerize Jango application and run the basic Docker comments so um for the next videos I have planned the front end and this would be either nextjs or reactjs app postgress PG admin Prometheus grafana traffic engine X saler Redd is painer not necessarily in this order but this is the list which I would love to cover um if there won't be any engagement uh you won't like these videos I will simply shorten this list we'll see how it goes so for now um let's not worry about about this 2md uh I'm going to close it and before we create our Docker file let's cover the installation so in order to work with Docker we will be using the docker desktop head over to doer. products Docker hyen desktop and over here we will need to download the proper version of the docker desktop according to the operating system so for me this would be download for mac Apple chip but there's also for Windows and for Linux as the next step make sure to open up the docker desktop finally if you are using visual studio code go to the extensions find Docker and install this particular extension so now we are ready to create a Docker file which is used to build Docker images so um I have my development server running and I just just want to show you that this is what we are going to dockerize so I haven't done absolutely anything in the Jango app and we want to run this what you see over here from the docker container this is the goal for today's video as well as of course learning all the necessary basic Docker comments so back in Visual Studio code I'm going to add a new file and I'm going to name it Docker file like this so as the first step we need to specify our base image on top of which we are going to create our custom one and for this particular example I'm going to use the Alpine Linux uh which is a lightweight distribution and it will minimize our image size so let's write from and then python let specify the version I'm going to use 3.10 and then Alpine at the end note that this is not the best version for running D Jango apps with data science libraries so I experienced it a few months ago and I won't recommend it it's very painful to work with alpine I switched to a normal version which by default is the Debian based image so this is just a a note a remark which might be helpful for building your own applications so again I'm going to use the Alpine and as the next step we are going to run APK update so first of all run um is used to execute commands during the image building process and then we have the APK and which is a package manager in the Alpine Linux distributions so we can run APK update and then what I'm going to do is to add Nano install Nano and I will need Nano at the very end of this particular tutorial to fix one issue manually from inside the container so we will access the interactive mode we will get inside of the container and then we will switch something in the settings py file to fix one issue okay next we need to prevent writing pyc files to disk so let's write envy and this is a key value we can write Python and then don't write bite code and set it to one and then let's prevent buffering the output which could lead to problems with logs when running the app in the container so again envy and then python on B one like this hopefully I didn't do any spelling mistakes next let's create and set the working directory in the image file system and all the commments will be executed from this particular location so for this let's write work here and then I'm going to set it to app and I will will show you how this works in practice how this looks in practice once we access the container in the interactive mode so so um now we can actually add Docker ignore because we will copy all our files but I want to exclude the docker file and the 2-o MD so we can add a Docker ignore file so let's write Docker ignore and here I'm going to specify the docker file and then the two do MD very similar to um G ignore so now I'm going to save this save this and close this and yeah we have our work deer set so at this stage I think we can puse the work in Docker file and simply create our requirements txt file and then we will cover the concept of layers so what I'm going to do is to go to the terminal quit quit the development server from running and then I'm going to create the requirements EXT file pip freeze greater than requirements requirments dxt all right so now we have our requirements txt over here perfect so it is important to understand that layers are instructions that are caged or the instructions are um layers that are caged so each instruction is in fact a layer so we can do some work with the requirements txt file in an early stage and we do it in order to leverage the cing mechanism and make the build process a little bit faster of course this is only for the case where the requirements txt file hasn't been changed so again I will show you how this works in practice so first of all let's copy the requirments requirments XT to app okay so we are ignoring for now all the other files we are copying the requirements txt and then once we have the requirements txt we can run pip install D- no C deer and then R and then requirements EXT so this will install all the packages um that we have in the requirements cxt file okay so as the next step we can copy the rest of the files so we can write copy dot so everything that we have over here to the app right and again if this is not understandable I'm going to show you how this works uh once we get to the comments so first of all we need to finish our Docker file and we are basically almost done and once we have our Docker file we will start with some basic comments and I will uh explain why we why we did this like this so why we copied the requirements txt first okay so as the next step we can expose the port that jungle will run on however this is not mandatory this is just a good practice so in our case it's going to be P 8,000 and finally the last instruction is going to be to run the development server we do it with command and then python of course in a list as you can see so each word of this particular comment is a separate element in the array python manage py run server and then 0000 0 8,000 so we are starting the development server and we use this 000000 to run our app in the container and being it being accessible uh to the outside world from the container Okay so let's save this and yeah let's actually build our image based off this Docker file so as you can see I have a blank window of terminal I'm currently in the backend directory so inside I have my Docker file and we can actually start by listing the docker images so in my case I cleared all the images before recording this video so the list should be empty and and in fact it is empty so Docker images is the comment to list all the images and right now we are going to build our image from the uh Docker file okay so let's write Docker build and then we are going to give it a name with this hyph T and I'm just going to call it my image and then I'm going to add a DOT at the end since I'm in the same place in the same path as my Docker file if you would be in a different place you would have to provide the path to this Docker file so let's press enter and as you can see we are building okay we are now up we have a uh so we have an error I misspelled the requirements txt let me fix this very quickly require I missed an E I think so let's save this and I'm going to clear it and rerun it again okay we are running the instructions and now we are installing the packages as you can see so this takes a little bit you can say and and for me it takes probably a little bit longer since my internet connection in the place where I'm recording videos is very weak so this is why um it takes that long all right guys so it took quite a while because my internet connection over here is really really weak so um I made a p and now I'm back once we've built the image so now we can write Docker images and over here we have our image that is created so now if we rerun this again it will take pretty uh less am pretty it will take less amount of time as you can see it's almost instant it's all from CED and it's done in just a few seconds so yeah as the next step I'm going to show you that we are going to return to this particular step because um I'm going to install some package um it can be for example Jango framework so pip install Jango rest framework okay and then uh I'm just going to update the requirements txt file requirements dxt there we go and then I'm just going to rebuild the image so as you can see we are installing everything again from this particular step all right so it took a while for me to rebuild the image and the option in the docker file no cage deer didn't help because this option prevents from cing the downloaded packages so that's why if you combine it with my weak internet connection it just took a little bit uh too long sorry about this but yeah we can move on and we can list out the docker images again and over here we have the newly created image and the old image which I'm now going to remove with the use of Docker and then image RM and I'm going to provide the image ID so now we have a confirmation that we've deleted an image and we can write Docker images again and we see that the image the old image has been deleted all right so as the next step we can add actually run the container so we are going to run a container based of the image that we have created in order to do this we need to write Docker run and I'm going to run it in detach mode this way I will have my terminal available otherwise you would have to open up a new terminal to run some additional instructions and yeah we will simply specify the image name so in our case it's going to be my image but we are also going to map the ports so what we want to do is to map Port 8,000 on the host to the port 8,000 in the container so 8,000 8,000 like this and yeah let's see if this will work okay now we can list out the containers with the use of Docker PS and over here we have our Docker container so this Docker container I'm just going to minimize it for just a second I know it's going to be difficult to see it has a random name sleepy Shannon and a container ID so we will refer in the future to either the name or the ID to do some additional comments um for now let me just make it bigger and yeah let's see if we can go to see our app so I'm going to Simply open a new window and there we go so we have this allowed host because we actually would need to provide this particular address in the allowed hosts so in the settings py we would have to include it over here and this is something that we will fix a little bit later by going into the interactive mode by logging into the container and manually adding something to the allowed host but this is something that I will show a little bit later for now uh we have some kind of a confirmation that our Django app is working however we have an error uh so yeah as the next step let's stop the container to stop the container we can refer either to the name or to the ID so I'm going to grab the name and then Docker stop and I'm going to pass in the name so now if I run Docker PS I have an empty list but if I run Docker PSA as you can see over here I have my container with the status exited so it exited around 11 seconds ago now to remove the container I can use a similar comment Docker RM and then I need to again provide the name or the ID this time I'm going to use the ID and there we go so now if I run Docker PSA I will get an empty list so we can simplify this process by adding the RM tag when we are running the container so I will show you how this works in a short way so Docker let me make this window a little bit bigger Docker run and then dash dash RM and then I'm going to use p and I'm going to map 8,000 to 8,000 and um I also want to run it in detach mode and then I need to specify the image name which is my image okay so now if we run Docker PS we see our Docker container um but if if we now grab the ID for example and write Docker stop it will be automatically removed so now if we write Docker Esa the container doesn't exist after stopping it has been automatically removed all right the final comment that I want to show you today is the interactive mode so I want to go inside of my container so I'm going to run run it again and then I'm going to run Docker PS and here we have a random name so maybe I'm going to show you one more thing I'm going to show you how you can um add your own name for the container so let's write Docker stop and then let's write Docker run detach and maybe somewhere over here I'm going to write Das Das name and I'm going to set it to I don't know my [Music] awesome or just let's keep it a little bit shorter awesome DJ something like this okay now let's write Docker PS and here is the name awesome DJ so let's access the interactive mode so for this we need to write Docker exec iPhone it awesome DJ or the container ID sh and now as you can see we are in the app in our work directory that we have specified inside of the docker file over here and we can run LS and as you can see we have the requirements txt manage py the SQL light core but we don't have the files included in the docker ignore okay so what we want to do is to fix this issue because if we refresh the page it it still exists so um let's go to core and then with the use of Nano let's open up the settings ey and now I'm going to go to the allowed hosts and I'm just going to set the allowed host to every everything okay just to keep it simple then I'm going to press crl X and then I'm going to press enter to confirm uh sorry first I need to press Y and enter and there we go so let's refresh the page and there we go we have our Jango app running from the container and over here in this interactive mode you can run for example migrations python manage py migrate but of course we need to go to the Parent Directory so um inside of cor we of course didn't have the manage py file but in the app directory we do so we can write again python manage py migrate and there are no migrations to apply but this is basically how you can work inside of the container using the interactive mode which which you can activate with the use of Docker exec iPhone it the name or the uh ID container sh so I think we can finish this video over here in the next one I think we will tackle um volumes maybe mention a few uh words about networks and we will work with Docker compose so yeah I hope you guys enjoyed this video and hope to see you guys in the next one
Info
Channel: Pyplane
Views: 1,709
Rating: undefined out of 5
Keywords: django 4, django with docker, dockerize django app, django tutorial 2024, docker containers, Containerization, docker basics, docker basic commands, docker build, docker exec, django ci cd, django devops, docker commands, django web development, python docker, python dockerfile, docker image build, docker tutorial 2024, python with docker
Id: FYZ2wytNX4U
Channel Id: undefined
Length: 25min 54sec (1554 seconds)
Published: Fri Feb 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.