Django & Postgres with Docker Best Practices

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so how do you configure your django or python applications with docker do you search on google and copy and paste some code and put it on production well certainly this is one way of doing it or i can give you some tips and save you from future headaches welcome to this tutorial in this video we are going to configure django and postgres with darker but the tips and tricks are useful if you have any python projects my name is ali company and in this channel we are talking about full stack programming and sometimes uiux as well if you like this kind of stuff please watch to the end of the video and make sure to subscribe first we'll start by configuring django in the development environment obviously you need to have docker and docker compose on your system if you don't have them i put some links in the description you can follow them and install docker and docker compost if you already have docker on your system let's jump right into it this video is brought to you by webcard studio if you need a landing page crm or any web application please contact us at hi sign webcard.xyz first we need to set up our virtual environment i'm using ppm for that after that let's install django and create our django project because we should never put passports or secret keys inside docker's configurations let's create an emv file and put these variables inside it in order to manage our environment variables i'm using the django environment package first we need to install it after that let's edit this settings.p settings.py file to read our environment variables the next step is to create our custom docker image we need to decide which python-based image we are going to use there are some people who suggest using alpine but because using alpine could make our builds too slow and create some performance issues in the future we are not using it i put a link down in the description which explains in detail why it's better not to use alpine we are using python version 3.8 as slimbuster because it's a small and stable enough compared to other base images after that we are setting the work directory and let's set these environment variables the first one prevents python from writing pyc files to disk and the second one prevents python from buffering std out and sdd error after that let's install our python package from ppm we also install flake 8 which analyze our code for prep 8 best practices and finally we copy our code to the docker container docker caches the steps in docker file to speed up builds then a change is made to one step all steps following will be redone so it's better to start your docker file with commands that are less likely to change and putting commas that are more likely to change for example like copy as late as possible after that we run flake 8 to see linters suggestions next we are creating a docker compose file we are creating a service inside it for our django application we called it web let's add a volume and make port 8000 on the host machine connected to port 8000 on our docker container now let's test to see we didn't make any mistakes as you can see everything is working if we want django to use postgres we need to install cycle pg which is a postgres adapter don't install cycle pg binary on production in order to use postgres we need to edit our docker image so we head to our docker file and add these lines to it basically cyclopg needs these dependencies to work properly combine run steps that are related in order to prevent caching since each run step will create a new layer and using unnecessary disk space after that we need to add postgres service inside the docker compose file we call it db to make sure that our database data doesn't remove each time we stop our container let's create a volume for it now let's add this environment variables to emv file okay now we need to add a new environment variable for our database after that let's edit the settings.py file and tell django we are using postgres now now let's edit our docker compose file and make sure every time we run it first we migrate changes and let's build our containers again awesome everything is working in order to use docker on production we need to make some changes to our configurations first we need to install g unicorn package after that let's create a new docker file and name it docker file dot prod here we use a docker multi-stage build to reduce the final image size essentially we create a temporary image first we call it builder this image is used for building the python wheels the wheels are then copied over to final production image and the builder image is discarded the builder image is similar to the development image which we created earlier but for installing python packages we first extract packages from ppm.log and create requirements.txt file then we are building python wheels after that let's create our final production image it's best practice to run our django app with a non-root user so let's create a new user and call it app next we create necessary directories and set the work directory as well after that we copy python packages from the builder image and copy our project inside the docker image after that let's change ownership of all the files to the app user then change to the app user and finally we need to create a new docker compose file and update the web service to build with docker file dot prod image and use g unicorn for running the server also since in production we don't need a volume let's remove it now let's run our production container to see if it's working we add dash flag to tell docker that we are using a new docker compose file awesome it's working you can also create an nginx service for this setup but usually i don't do this remember docker is another layer of complexity and it may cause more harm than benefit especially in production servers we don't want that i also put a link of an article in the description which explains why it's better not to use nginx on a container it's also advisable to not configure the database with vdocker there are many fully managed database services like rds or cloud sql and rather than managing your own postgres instance within a container you should use them i put github link of this project down in the description if you want to use it as a starter for your django projects you're more than welcome to use it if you enjoyed this video and find it useful please consider to subscribe to our channel if you have any suggestions or questions feel free to ask them in the comments section until the next video goodbye and have a nice day
Info
Channel: Webkar Academy
Views: 782
Rating: undefined out of 5
Keywords: Ali Kompany
Id: 4wdNx2j1j-w
Channel Id: undefined
Length: 11min 10sec (670 seconds)
Published: Wed Jul 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.