Docker Compose | How to configure PostgreSQL or MySQL database with Python Django

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is xander welcome back to the docker compose and django tutorial series in this tutorial we're going to have a look at how to build and start a django project with a postgres or mysql database so you can follow from this tutorial you don't have to watch part one and two it would obviously be useful because i will presume that you do have some understanding of some of the basic commands that we've already covered apart from that obviously you're gonna need docker docker compose python and code editor if you want to follow along in this tutorial we're going to create a new docker compose file well we're going to just extend from the previous tutorial our docker compose file to include a new service that's going to be postgres or mysql we'll cover both of these installations in this tutorial so we will then go ahead and build a new docker image and container and then i also show you how to remote into a docker container so you can run commands in the container so i've gone ahead and downloaded the previous tutorials code and i've updated the commands text so part three here that's where we are we're going to be utilizing these commands and you can see they're very similar to the previous commands that we've been utilizing but this is just a record of all the commands that we've utilized so far so let's start off by configuring a postgres database for a django application so we are starting here with an application so let's just go ahead and delete the existing application and we don't need this database anymore and we won't need this manage file so we'll build this automatically through docker compose so i've got the command text a requirements text and the docker file on the docker compose file okay so first of all let's go into the requirements file so because we're going to be utilizing a postgres database we're going to need instructions or django's going to need instructions in order to connect to it and to work with a postgres database so we're going to need a new package so go ahead and add the new package here so like i said this is basically the package that's going to allow that connectivity or provide the instructions to connect to a postgres database in django or with django okay so now we've got that we can close the requirements um that's going to be installed when we run or build the image so now we can focus on the docker compose file so so far in the previous tutorial we set up one application which was django so the whole point here really with docker compose is that we can compose many applications so now we want to run a database so we can go ahead and now describe a new service all of the images come from the official docker hub service so here for example you'll find the postgres and mysql official image so that's what we're going to be pulling down and utilizing in this tutorial now with every image there are instructions and it provides other references to other commands or parameters that you can or variable sorry that you can define within your docker compose file so if you are looking for a more advanced setup here i'm just going to show you a basic get started docker compose configuration but by all means have a look in here and have a look at some of the more advanced options a minimal configuration would look like this for postgres the image postgres the volume so here we want to map across the data from the database over to our local machine so we're going to create a new folder on our local machine data db that's going to happen automatically and we're going to map across colon from the var lib postgres sql or postgres sql sorry data folder um over on our container so that's going to map it across all our data's going to reside here that means obviously when we close the container or remove the container our data will persist in our local storage right so next up is some environment variables so the postgres database name the user and the password so we need to match that across to our settings in a minute in django so just remember that and then i'm going to name the container postgresdb and that's pretty much it therefore kind of a basic setup for postgres so as i dropped in there let's just close everything here we don't need that um in a minute when we build this we are going to need to configure django for postgres database but the first thing we can do now is obviously build our image start our container and then our application will be available for us to then configure it with the database settings now you may already have a django application so you can work in that way if you want to so if you have a django application you can follow this guide of course that's not a problem at all you'll just need to now change that settings and i'll show you how to do that in a second so now we can build an image so if you head over to the commands if you're not familiar let's do docker compose build so we're going to build the image so let that do its thing if you are using the docker desktop you'll be able to see the image being created and let's just get rid of all the other images etc as i have um let's just remove that so here we're building the the app image so just gone ahead and we've built that yep that's already built now so that was called app so now we can go ahead and run this command here so remember from the previous tutorial docker compose run here we're going to remove the container that gets um started and because we're going to run this from docker compose up so here i'm just purpose purposefully going through as many commands as i can to make you aware of the different commands that are available so there are differences here and later on in the tutorial series we're probably cut down on commands we utilize because we can perform similar actions with different commands so here we're going to run the run and then we're going to remove the container that is generated so we define the app we want to utilize or sorry the image sorry we want to utilize and here we are going to run a one-time command um which is going to be django admin start project core so that's going to create a new project our new folder is going to appear here or linked across to our container for both the database and our application so let's give that a go so that's going to take a couple of seconds and there we go so yours is going to take a little bit longer i've already have the postgres image you can see it's 300 meg in size so that happens almost instantly so here you can see i've got the core now one thing that didn't that wasn't made um creating network b default was a default driver okay so one thing that didn't happen there i was expecting the volume volume to appear now let's have a look at the reason why that is so you can see that i run this file and i'm missing a command so the missing command so this is going to be depends on so you can see there that this didn't run so if i now add the command depends on and describe what this depends on db now this is going to be also connected and set up so depends on is a command in actual fact um that really just defines the running order the start up and close down or order so now we've got that in place let's do this again so i'm going to go through the process again we don't have a container so let's get rid of this image so we don't need to do this i just want to show you from scratch again so we're all working on the same page so let's go ahead and docker compose build so let's build our image again so that shouldn't take long so let's run this command again so this time with that additional command you can now see we have the data folder and the core folder so this is our database data and our normal django application so i did receive an error here that's because i've already created i'm trying to run the command again for the same to build the same app it already exists so that's not a problem at all so that's why i'm being shown that message there you might not be so now we have that in place what we can now do is go into settings you may already be familiar with this go down to the database configuration and of course we don't need that anymore that's the old database configuration for sqlite and now we need to add our postgres configuration so remembering the settings from the docker compose the name username and password is progress the host is db so that just basically describes the service here db and then the port number the default port number for postgres database right so with that in place that now connects or attaches our django project to the database so we can go ahead and run compose up so going back to our commands here so we're going to run doc compose up so that's going to start the container with our configurations you can see that our database has started and then now django has then started so you can now see the running order the database is started first and then django so you can see it now tells us that we need to run our unapplied migrations and that's going to be our real test wherever this is working because if we can apply the migrations it means we're connected to our database because this is the only database that's described in our settings right so now let's make a new terminal window so let's run the exec command so basically docker exec that's a command that's basically saying run a command in one of your containers exec so what we're going to do is we're going to create this interactive terminal mode using the flags int and then we need to describe the app that we want sorry the container that we want to move into so we called this natural fact django container so let's go into the django container and we're going to run a command so that command is going to be bin bash so we're going to start a bash shell and that essentially is going to allow us then to interact with it here within the terminal so we'll be able to use the command line of that container here so let's have a look there we go so now we're inside the container so we can run our normal command so let's have a look at what version of python is running 3.8.8 there we go so ls for listing all the files so i'm currently in my project folder that's the slash django folder there so i can move to the default or the the root directory and so on so this is just linux if you're familiar with linux so now i can go ahead and run my manage file um and then i can migrate so that's obviously going to test you can see that all the databases tables are created and it looks like i'm connected so from this point i can carry on and continue working on my project so to confirm you can test this yourself it's always good to do that if you were to close down your containers and your image and start this again all the data will be persisting here within your local storage remember this project in my case is on my desktop on my local hard drive so now let's take a look at changing this into a mysql database right so first of all we'll just comment out or the postgres so we're inside the docker compose first so we're going to need a configuration now for mysql so let's just paste one in and have a look so here i'm running an older version of mysql because by default a latest version there's a few errors that needs resolving by default so here i'm just running just to show you get to get this working the mysql 5.7 so i'm defining here the database um name and there are other settings as you might imagine mysql allow empty passwords true so by default like i i haven't said before by default um the mysql database has a username a root and a password which is empty so we can apply this setting if you want to allow empty passwords if we want or we could create some more passwords and usernames so again have a look at the docker hub image the mysql image and then it will do detail all of the different environment variables that you can set so again we're just setting up our volume to mirror across our data so this time i'm going to make a new folder called mysql and db and that's going to copy across or it's going to mirror the folder on my container var lib mysql so again this is going to make sure that all the data about my database that's needed if i were to restart the container is contained on my local storage database right so that's the docker compose file so as you might imagine now we're going to need to change our database configuration in our django setting so let's go ahead and do that so it's a very similar setup as you might imagine so here we've got mysql backend now the name the database that's a mirrored across from the dock compose we've got the username password of nothing host db port 3306 so that's that done so now in no particular order of course let's have a look at the requirements so here we're going to need to have again a way of connecting to the database so where this is the instructions to character postgres we're now going to include mysql client which again is the package that we're going to need to connect django or allow django to communicate with our database and vice versa right so that's in that in place so we've got now our docker compose file sorted settings in our django and our requirements so the last step is to go to the docker file so here we've been using the slim version of python 3.8 image the the official python image so this isn't going to work so we aren't going to be able to install by default mysql client directly on to the slim blaster so we're just going to be using 3.8 and that is a very large image it's around about 800 to nine hundred meg so that's the change i need to make there so now i can go ahead let's just close that down let's open up a new terminal right so let's just go ahead and just stop all the containers so we can do this with the desktop tool or of course we can do it by commands so i'm just going to remove the image and let's go back right so let's go ahead now and docker compose build just do that first for no particular reason i won't just to show you from the very beginning so that's now built now we already have a project here so i'll just run the full command again so what we're going to see is a new folder up here in the data area let's go ahead and run that so now you can see we've got the mysql folder again i've got this arrow because obviously my project already exists so it's just me being a little bit a little bit lazy there for you okay so now we can compose up so let's get this going to build our containers all the services are running and it looks like we're going to need to migrate so let's open up our let's open up our let's get rid of that okay so let's now open up a new terminal we can obviously now move into our container like we've done before now that's the wrong it's a wrong container so it should be django container and now we're in the terminal let's do exactly the same thing again manage pi and migrate and there we go so we're now migrating the default tables from the installed apps and it looks like we're now connected to a mysql database so same situation here all data is now being mirrored across to this folder so if we were to close the container restart the image for example or completely uninstall docker and install it again everything is persisting here on our local storage so there we have a baseline installation for postgres and mysql now we don't need to run django for example this is very flexible we don't need to run django in docker container for example you may want to develop your django application within your local machine like you normally do but hopefully you're seeing the benefit now of using docker for installing other services such as postgres your database or any other message broker for example that you might want to attach to your application maybe you're using node for example so we don't need to install it anymore on our operating systems which for some software can be a real pain to set up particularly in windows so it's very flexible later on in your project you might then migrate your django application to docker before you then deploy it of course um obviously there's multiple options here and as time goes by hopefully hopefully you become more familiar with the commands it just become natural for you to utilize docker maybe in your workflow so thank you very much again hopefully this was useful if you do like it then please like and subscribe and hopefully i'll see you in the next tutorial
Info
Channel: Very Academy
Views: 7,082
Rating: undefined out of 5
Keywords: docker postgres, docker, docker mysql, docker compose database, docker django
Id: q75wgk9jVjA
Channel Id: undefined
Length: 18min 40sec (1120 seconds)
Published: Fri Apr 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.