Developing with Docker - Docker in Practice || Docker Tutorial 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so in this video we are going to look at some practical example of how to use docker in a local development process so the prerequisites before you get started with this video is you have to understand some basic concepts of docker and you have to know some of the basic commands so if you don't you can check out my videos where I cover both the concepts and the comments and then you can come back to this video so what we're gonna do is a simple demo of a JavaScript and node.js application in the backend to simulate the local development process and then we're gonna connect it to a docker container with a MongoDB database in it so let's get started so in this video we're gonna see how to work with docker containers when developing applications so the first step will be is we're gonna develop a very simple UI backend application using Java Script very simple HTML structure and no J's in the backend and in order to integrate all of this in the database we are gonna use a docker container of a MongoDB database and also to make working with the MongoDB much easier so we don't have to execute commands and in the terminal we're gonna deploy a docker container of a mongrel UI which is called Express where we can see the database structure and all the updates that our application is making in the database so this development setup should give you an idea of how docker containers are actually used in development process so I've already prepared some very simple JavaScript application so in order to see the code basically we have this index.html that is very simple code and we have some JavaScript here and we're using node.js back-end that just serves that index.html file and listens on port 3000 so we have the server running here in the backend and we have the UI that looks like this so basically it's just the user profile page with some user information and user can edit their profile so if I for example change the name here and if I change the email address and do changes like this I can save my profile and I have my updates here however if i refresh the page of course the changes will be lost because it's just JavaScript no J's so there is no persistent component in this application so in order to have that which is actually how real-life applications work you'll know that you need to integrate the application with the database so using that example I will try to showcase you how you can actually use the docker containers to make the development process easier by just pulling one of the databases and attaching it or connecting it to the application so in this case we're gonna go with the MongoDB application and in addition to MongoDB container we're gonna also deploy a MongoDB UI which is its own container it's called Express where we can manage or see the database in insights and updates from our application much easier so now let's see how that all works so in order to get started let's go to docker hub and find our MongoDB image here let's go to Congo and we have MongoDB here and the Express which is another container that we're gonna use for the UI so first let's pull the MongoDB official image so I already have MongoDB latest so pulling doesn't take longer but on my laptop but you're gonna need a couple of seconds probably and the next one we're gonna pull is the docker Express which I also have I believe so let's see yes it's also fast so if I check luckily I have MongoDB and Express images so the next step is to run both and Express containers in order to make the MongoDB database available for our application and also to connect the Express with the DB container so let's do the connection between those two first in order to do that we have to understand another darker concept doctor network so how it works is that Dhaka creates its isolated token network where the containers are running in so when I deploy two containers in the same token Network in this case and Express they can talk to each other using just the container name without localhost port number etc just the container name because they are in the same network and the applications that run outside of docker like our node.js which just runs from node server is going to connect to them from outside or from the host using localhost and the port number so later when we package our application into its own docker image what we're gonna have is a game token network with MongoDB container Express container and we're gonna have a node.js application which we wrote including the index HTML and JavaScript for fronting in its own docker container and it's gonna connect to the MongoDB and the browser which is running on the house but outside the docker network is going to connect to our JavaScript application again using hostname and the port number so docker by default already provides some networks so if we say docker Network LS we can already see these auto-generated dog networks so we have four of them with different names and the drivers we're not gonna go into details here but what we're gonna do is create its own network for the MongoDB and Express and we're gonna call it mobile network so let's do this right right away I'm gonna say docker Network create and we're gonna call it Network so now if I do doctor Network LS again I see my token network has been created so now in order to make our MongoDB container in the Express container run in this Mangal Network we have to provide this network option when we run the container in the docker run command so let's start with the so we all know that docker run is the command to start a container from an image right so we have current which is the basic docker run command however in this case we want to specify a couple of things as you learned from the previous videos you have to specify something called port so we need to open a port of MongoDB the default port of MongoDB is 27 thousand seventeen so we'll take that port actually for both host and container so will run at this port inside of a container and we open the same port on the host so that will take care of the port then we will run it in a detached mode in addition to that there are a couple of things that we can specify when starting up the container and these are environmental variables of the MongoDB let's see in the official image description you actually have a couple of documentation about how to use the image which is very helpful to kind of understand what kind of configuration you can apply to it here you see some environmental variables so basically on startup you can define what the root username and the password will be which is very handy because we're gonna need those two for the experts to connect to the and you can also specify the init database we're just gonna provide the username and password because we can create the database from the Express you why later so let's do that and the way you can specify the environmental variables you can actually see here as well is by just let's copy this one so here you say environmental variable that's what they - e flag stands for and the root username will say it's admin and another variable which is the password will be just password so in this way we can actually override what the default username and password will be so two more things that we need to configure in this commend our container name because we're gonna need that container name to connect with the Hmong Express so we'll call this one dB let's say and another one we need is the network that we created which was called Network so in order to make this command a little bit more structured do it on multiple lines so let's see so it's more readable so basically all these options or all these flags that we set to go one more time through them it's gonna start in detached mode we are opening the port on the host username and password that we want a MongoDB to use in the startup process we're gonna rewrite or override the name of the container and this container is gonna run in a Network and this should actually start the container okay so if you want to see whether it was successful we can lock the container and see what's happening inside so as we see was started and everything actually looks good waiting for connections on port 20 7017 okay so now let's start Express we want Express to connect to the running MongoDB container on startup and here we have an example of how to run it and here we have a list of environmental variables that we can configure so let's quickly look at them username password we don't need them however we need the admin username and admin password of the MongoDB this is actually what we overrode with admin and password so we're gonna use them because Express will need some username password to authenticate with the MongoDB interconnected the port is by default the correct one so we don't need to change that and this is an important part this is the MongoDB server right so basically this is the container name that experts will use to connect to the docker and because they are running in the same network only because of that this configuration will work if I didn't if I hadn't specify the network then I could have I could specify the name correct name here of the container but it wouldn't work so with that said let's actually create the docker run command for express as well so let's clear the history and let's start so again we run in detached mode and let's see what parameters we need so first of all port let's say with what is the default port that the Express runs on that's 80 81 so we'll take that so basically it's gonna run on our laptop on port 80 81 the next option would be these two and remember environmental variables need to be specified with minus E and this is the username of MongoDB admin which is admin because we specified it when we started the MongoDB container this is the password let's set this one as well don't forget the network - - net Network we have the name we can also call it Express and let's see what else we might need here yes this is important one and our container name let's actually see it again dr. PS the one running it's called MongoDB that's the container name and this is what we need to specify here so I'm gonna write this here and finally the image is called Express so I'm just gonna copy this one here and that is it so basically with these commands talk Express should be able to connect to the MongoDB container so let's write it and just to make sure let's lock the container and see what's happening there waiting for MongoDB welcome to my express it looks like it connected successfully it says here database connected and the Express is available at port 8081 so let's check the manga Express out at the port 8081 so actually let's close these tabs you don't need them anymore and here if I say localhost 8080 one I should be able to see the manga Express so these are the databases that are already exist by default in or which are created on startup and using the UI we can create our own database as we saw previously we could have specified an environmental area variable in it TB on MongoDB startup and that would have created a new database however it doesn't matter we will just create a database name here so we will call it user account database so let's create one and now we can actually use it or connect to this database from nodejs so let's see how that works so now we have the MongoDB container and the manga Express container running so let's check that we have both of them we'll have to connect no J's with the database so the way to do it is usually to give a protocol of the database and the URI and the URI for a MongoDB database would be localhost and the port that it's accessible at so when I already went ahead and prepared the code for no J's so basically we are gonna use a client here which is a node module and using that client we are connecting to the MongoDB database so this is the protocol the host and the port that we just saw that the MongoDB is listening at and username and password of the root user of MongoDB of course usually you wouldn't put the password here or not use an admin or root username password to connect to a database but this is for just the demonstration purposes and these are username and password that we set as environmental variables when we created the docker MongoDB container so let's check that so this is the MongoDB container command and this is the username root and root password that we specified and this is what we are gonna use in the code as I said for demonstration purposes I will write the password directly here so then we connect to the database so I also went ahead and in the Express user account database and inside that I created collection which is like a table in my sequel world called users so here I connect to a user account database and I query the collection users and this is a get requests so I'm just fetching something from the database and this is update request same thing I connect to the database using the same URI and the database name and I update or insert something in the collection so let's see how all that works so let's head over to the UI so in the users collection the there is no data it's empty so we're gonna refresh it and edit the data so I'm gonna right here some and update it and refresh we see that a new insert was made so this is the update profile section here so all this was executed it connected to the MongoDB and now we have one entry which is email coding name that we changed so if I'm gonna refresh it now I fetched a newly inserted use the data in the UI and I displayed it here and also if you want to see what the MongoDB container actually logs during this process we can actually look at the locks so I'm gonna say docker PS and block using the container ID so let's say if I wanted to see just the last part of it because I wanted to see what the last activity was I can also let's clear this and I can also do tail so I can just display the last part of it or if I wanted I could also stream the logs so I'll clear this again and I'll say stream the logs so I don't I won't have to do doctor logs all the time so if I make a line here for example to mark the last logs I can refresh it let's make some other changes let's change it and save profile so I'm gonna see some activity here as well so these connections are new and it also says received client metadata and this is where the node.js request comes in with a noches and it's version and at the end of each communication there is an in connection because we end the database connection at the end so we see that also in the logs so for example something wasn't working properly you could always check them in a logs here so with that I have a fully functional JavaScript node.js application which has a persistence in the MongoDB database and we also have UI both of them running in a docker container so this would be somehow a realistic example of how local development using docker containers would look like thanks for watching the video I hope it was helpful and if it was don't forget to like it this is a video series so I will create a new one every week so if you want to be notified whenever a new video comes out then subscribe to my channel if you have any questions if something wasn't clear in the video please post them in a comment section below and I will try to answer them so thank you and see you in the next video
Info
Channel: TechWorld with Nana
Views: 26,220
Rating: undefined out of 5
Keywords: docker in real world, docker simple explanation, docker compose, developing with docker, docker in development, docker development workflow, docker node, docker nodejs, docker node js development environment, docker tutorial for beginners, techworld with nana, docker explained, docker demo, wright, raghav pal, docker tutorial, docker, developing with docker containers, develop application with docker, use docker for local development, use docker for development
Id: 6YisG2GcXaw
Channel Id: undefined
Length: 20min 50sec (1250 seconds)
Published: Sun Nov 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.