How to Run MongoDB in Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks today I'm going to show you how you can run a database inside of a Docker container I'm a big fan of running databases inside of a Docker container mostly because if you work on a team you can make sure that any of your database changes don't break the app before you push it to Devon test which will then affect other team members and it's even better if you're solo developer because there's no cost associated with running it on your local computer first I'll show you how to run it using Docker run and then I'll show you a cool way of using vs C to interact with your database and then finally I'll show you how to use Docker compos to start your database so let's jump in and get started first I'll show you how to run this using Docker run go ahead and open up a terminal and you can be in any folder that you want and the command for this is a little bit long but I'll break it down into pieces first we'll say Docker and then run and then- d means detached mode then I'll give it a name I'll just call this demo then we're going to pass in a couple different environment variables and these environment variables are for the root username and the root password and because this is going to get kind of long I'm going to break this into different lines and the trick to do that is if you hit the backslash and then to move your cursor to the next line without actually executing the command is shift enter or Alt Enter and then - e and a space and I'm going to just copy this in because it is a pretty long name I don't want to mess it up so this is the environment variable for the root username and it's all caps and it's mongoor init dbor root username and then you say equals and then you can give it whatever name you want I'm going to call it admin then again I'm going to do a new line just because this is going to get long and then another- e for another environment variable and this one is for the password and it's the same as before andit DB root and then password and then equal sign you can give it whatever password you want mine will be like And subscribe next I'll Define the port mappings so dasp and I'm just going to use the default ports for which is 277 and then lastly I'll give it the image name which is and you can leave it as if you want that will pull down the latest image or you can define a version here if you want so if you say version 7.0 that will always use the version 7.0 now you can go ahead and run this and when you run that it will go ahead and print out this long identifier this is the identifier for your container and mine did start right away because I already have the image on my computer if you run a Docker run and you don't have the image for example if you don't already have this 7.0 image it will pull that down first and then it will run it for you so you don't have to always pull the image first if you don't want to you can just say Docker run and it'll pull it down and now if I say Docker PS that will print out my running containers and you can see the demo is now running now that that container is up and running you can connect to it and you can use that database however there's one little thing I want to add and that's a volume if to delete this container that's running right now any data that's inside of it would just be deleted and that's because the files that is using to store that data is in the container and if you delete the container all the files inside of it get deleted as well and that's not ideal if you want to make sure that your data is there every time you delete and restart your container I do want to specify though that the data will stay there if you stop the container and restart it it only goes away if you delete the container and in order for that data to persist it's actually not that hard we just need to add a volume and a volume is just a way of saying put the data on our local computer but let Docker have access to it this is the command that we just ran and in order to add a volume there's only one thing we have to add and right after we Define the ports I'm going to define the volume so if you go up say- V there's a couple different ways you can do volumes I like to use named volumes mostly just because it's the easiest way and to do that you just give it a name I'm going to call mine demo and then a colon and then you give it the path to where stores its database information and that is data/ DB this data/ DB folder is in the Docker documentation for I'll put a link to that down in the description below and it'll also put a link down below to how uh volumes work in Docker and that's all we have to do so I'm going to go ahead and run this I'm going to go into Docker desktop and show you what this actually did this is Docker desktop and I'm in the container section and you can see that the mongod Das demo container is up and running and if I go over here to volumes there's a new one called demo that was just created and adding volumes isn't a requirement it's just something I like to do because if I do delete the container and I recreate it as long as you use that same volume name the data that you have in your database will still be there and I want to point out why I always map these ports I had somebody asked me this question in a previous video where I showed how to run SQL server on a Mac if you run this command like it is here you're going to run version 7.0 on this port 27017 and let's say for example that I want to test running something in version six of but I want to make sure I don't lose all the stuff I had in version s of well you can do that so if you change the version to six and you change your host port to something else you can now run both of those images at the same time and you can connect them at the same time from different applications of course without them messing with each other so if you connect on Port 27018 that will give you the version six of and if you run the command we ran before which was on Port 27017 that will give you version 7 of so there's a little bonus tip for you let's move on now to how you would connect to this database in vs code like I mentioned before if you're in an API you can connect to this database just like you would but sometimes you just need to get in there and ADD test data or just query what's in there and see what it looks like I really like using vs code for that there's an extension in vs code that lets you do this once you're in VSS code if you open up extensions and you search for mongodb it's called mongodb for VSS code and it's from the mongod DB team and I already have it installed but if you don't have it installed you'll just click the install button right here and that will add a new icon for you this little leaf if you click that mongodb leaf it will allow you to connect your database go ahead and click add connection and you can connect with the connection string or you can use a little form that they have I'm going to do open with form and the connection type is Standalone the host name is Local Host when you run your database in Docker it's going to always be Local Host as the host name since we mapped our ports to Ports 27017 that's going to be the same here and then authentication username and password go ahead and enter the username and password that you used and that should be all you have to do so go ahead and click connect and you can see it says successfully connected to local host on Port 27017 and up here in connections you can see it's connected so if you expand it you can see the different databases that are inside of it and what I really like about this is it actually gives you a way to basely run JavaScript code against that database to do that if you right click and say add database it'll open up this Javascript file for you and from this file you can interact with your database in the same way that you would if you're running this inside of a normal JavaScript application and they give you a nice little template go ahead and replace the values in here with what you want to use I'll just name the database demo collection name I'll just call it users and really quick I'm going to change collection to be collection name and then down here I'm going to say const collection equals DB dogit collection with collection name I have an array of test data I'm just going to go ahead and paste that in here it's just creating an array of users that each have a name and an email and I'm calling that constant users and then since db. collection gives me a reference to that collection now I can just use that collection to add those users so I can say collection. insert many of user then lastly just to make sure that they're all in there I'll just do a find and get all of them back okay now if I come up here and I run this with this little play button right here I'm going to go ahead and close this first but I'll go ahead and say yes and this little playground result tab opens up and this will show you the last command that was in your Javascript file since my last command was finded which will return all of the users it returns an array with all the users inside of it and this is just a really cool way to interact with your database on your local computer and so hopefully some people find that helpful now that you're a ble to run your database and interact with it I want to show you how to use Docker compos to run your database instead of using the docker run command and if you're not familiar with Docker compose it's a way of running one or more containers at the same time and the reason I really like it is because you put all of your configuration into a yaml file and that configuration includes all the stuff that we put in our Docker run command which was the image name the environment variables the volumes all of that into one file and then instead of running Docker run with all that stuff in it your command is just Docker compose up so let me let me jump in and I'll show you how that works and all you need in order for this to work is a Docker compose file and normally you'd want to put this Docker compos file in a project next to where you might be running an API for example for this demo I just created a folder and I only have this Docker composed file in here but the name is docker-compose DOL and inside of here we're going to Define all the stuff we need for our database first thing you want to do is Define the version and this is the version for Docker compose I'm going to go ahead and use version 3.8 the next thing we declare is Services anything inside this Services section will be run as a container when you run Docker compose up so I'm going to go to the next line and I will declare as my next service and this name can be whatever you want and this will make up part of the name of the container when it's running and I'll show you what that looks like when we run this in this section for we're going to Define all of the properties for our container these don't have to be in any particular order so the first thing I'm going to Define is the image and we used and that was version 7 and that was version 7.0 and next we'll Define the environment variables the environment variables that we use here are the same two environment variables we used when we ran Docker run and I'm going to paste these in so I don't screw them up so the the first one will be the username and we used admin and the second one will be the password and I use like And subscribe and next we'll Define the ports this will be the same as before as well so 27017 map to 27017 lastly we need to define the volumes and lastly we need to define the volume that we want to use so we don't lose our data and volumes have to be set up in Docker compos in a very special way so I'm going to actually set the volume up first and then I'm going to tell this image how to use it so I'm going to go back to the top level and I'll say volumes and then this you give your volume a name so I'm going to call it mongod data and then you go to a new line and I'm just going to say driver is local and that's all we have to do to declare that volume and so lastly we need to tell our image to use it so if you go back up after ports and you say volumes and then a dash and then you give it the name that you used so this name has to mat match this name right here and then you give it the path that it's supposed to map to inside your container which was data/ DB and that's it that's our Docker compos file so I'm going to go ahead and save that in order to run that Docker composed file we need to be in the exact same folder of where that file is so I'm going to CD into my code samples folder and if I do an LS here you can see there's my Docker compose file and now all you have to do is say Docker compose up and that's it this will run that database the exact same way that we did before using Docker run I am going to add- D just for detach mode so it doesn't lock up my terminal so I'll run that and you can see now that it's created that database I switched over to Docker desktop I want to show you just a couple small things that are different in how it runs these containers when you use Docker compose this container here is the container that we ran earlier when we ran Docker run but you can see now that there's a green one down here called code samples and the reason it has a different name is because code samples is the name of the folder in where I ran that Docker compos up command from and because Docker compos can run multiple images for example if I had an API that was connecting to this database I could run my mongod DB container and the API container from the same Docker compos file if I expand code samples you'll see inside of it is our database that's running it's been up for 2 minutes it also does a similar thing for volumes if I go over to volumes you'll see here instead of the name being mongod demo which we used in our Docker run command this one here is code samples unor mongod data so it's going to prepend the folder name to your volume name I think those are the main things you need to know in order to run a database on your computer using Docker now you know how to run that database using Docker run how to connect to it and interact with it using vs code and how to use Docker compost to start it up if you have any questions let me know in the comments down below thanks for watching and I'll catch you later
Info
Channel: ScriptBytes
Views: 14,289
Rating: undefined out of 5
Keywords: how to run mongodb in docker, mongodb docker, mongodb docker setup, mongodb docker volume, mongodb docker compose, docker mongodb
Id: gFjpv-nZO0U
Channel Id: undefined
Length: 11min 44sec (704 seconds)
Published: Mon Oct 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.