How to Build Node.js Apps with Docker | Dockerize Node.js and Express Apps

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey what's going on guys welcome back to  another video so in this video we will see   how we can dockerize our node.js application  we will see how we install the docker desktop   and build and run our images on Docker  container we will also see some of the   frequently used Docker commands while working  with our node.js application so if this sounds   interesting then stick around also don't forget  to subscribe the channel and press the Bell icon   so that you don't miss the videos like  this one so let's get started [Music] Alright guys so before we jump into the main  topic of this video I would like to share my   experience of using this amazing platform mnWho  so a man who platform connects the brands with   value driven Nano and micro content creators for  various Niche facilitating targeted and impactful   campaigns with the focus on various Industries and  Men Who streamlines the process of collaboration   between the content creators and the brands so  this is the mnWho platform where you can join   as a brand or as a Creator so what you need to  do is you just need to click on the sign up then   you will see that you can connect your social  media accounts and you can click on this join   as a Creator so if I click on it you will see that  we get a create a sign of form so you just need to   fill in your details once you click on the sign  up you will land to the dashboard where you need   to complete your profiles so first thing you need  to do is you need to verify your email address so   once you verify your email address you can go to  the channels and then you can click on connect   channel so here you can add your channel name  I will just click on the add Channel and then   it's connecting so my channel is already added  so you can connect to your channel here and once   you connect your channel you will see that you  will have all the information related to your   channel now once your channel is active you can  start creating your services so if you click on   Services then here you need to add your channel  name you will add some title you will have some   description related to your service your category  and then here you can create multiple services and   once you have your services done then you can  go for the campaigns where you will see all the   campaigns listed here so you can apply to any of  the campaign so let's say if I want to apply it to   this campaign I'm going to click on it and then  you will see some details about the campaign so   you just have to apply here and then you just have  to wait for the response from the brand so this is   a very good platform to get the collaborations  with the brand and make some money while working   with the top brands so I highly recommend that if  you are a content creator then you can join the   MN who and then you can collaborate with the top  brands and make some real money alright guys so   the first thing we are going to do is we are going  to create a basic Express app and then we will see   how we can build image of the express app and then  run it in the docker container so we are going to   start with I'm going to create a directory and I'm  going to have the node.js docker web app and now   let's go to the directory and now what we will do  is I'm going to have the npm in it so that I can   initialise my node JS app and here I'm going to  give the application name as node.js Docker web   app all right we are going to have the version as  1.0.0 we will have the description as node.js app   on Docker container we will have our entry point  as server.js no test command no git repository   no keywords I'm going to give author as the page  malvia and then we will have to type yes and then   it will going to create our packet.json file now  let's open that in the folder so I'm going to go   here and I'm going to open my application here so  I'm going to click open and this is going to open   our packet.json file okay I'm going to go and open  the terminal again and then what we are going to   install here is I'm going to install the express  so let me have the npm install Express I will have   the dot EnV all right I'm going to hit enter and  this is going to install our dependencies I will   also install the node mon so I'm going to have  the npm install node mon hyphen hyphen there   have hyphen save all right so the node mod is  installed but I made a mistake it should be hyphen   hyphen save and then hyphen Dev so it should  be like hyphen hyphen save hyphen Dev and then   it should have a Dev dependency so now if we go  and check then we see that we have Node 1 as Dev   dependency so now what we have to do is we need  to create the server.js file so I will have the   server.js so here I'm going to have the constant  Express that will be equals to require and I'm   going to have the express all right I'm going to  convert this to an es6 so I will go and convert   this to es6 all right and for that what I have to  do is I have to go to my packet Json file and I   just have to add the type as module so I'm going  to add the type and the type will be module so   that it will take the import Express from Express  app now the next thing I will do is I'm going to   create a constant port and this port will be equal  to process dot EnV dot port and if we don't have   the EnV file then I'm going to have the port 8080  all right now let's create the express app so I'm   going to have the app which will be equals to  the express and let's create a simple route so   I will have the app dot get I will have the Slash  and this will have the request response all right   this will be an arrow function and I'm going to  send a Json response and this Json response will   be message and our message will be app is running  on Docker container all right so now we have the   route and now we just need to listen to the port  so I'm going to have the app Dot listen and we are   going to pass the port here all right this is  going to be an arrow function and I'm going to   Simply have the console.log and we will have the  app running on and let's give the port all right   so I'm going to have the port here so now we have  our application now let's go to the package.json   file and in the packet Json what I'm going to  do is I'm going to add a script here so let's   add a script for the start so I will have the  start oops start and here I'm going to have the   nodemon server.js all right I will save it and  now let's give a try so what I will do is I'm   going to have the npm start so this is going to  start my application on local environment so I'm   going to hit enter and we see that the app is  running on 8080. so what I'm going to do is I   will just go to the thunder client and just make a  simple request so I will have the HTTP colon slash   localhost so localhost 8080 and when I hit enter  I should get a Json response which is the app is   running on Docker container so now let's assume  that this is the simple node.js application now   what we need to do is we don't want to run this  application on our local environment but we want   to dockerize this application and run it on the  docker container so usually when you want to run   something on a Docker container you need to First  create a Docker image and after that you can run   that image on a container so what we need to  do is we have to install docker on our machine   so I already have installed Docker in my machine  but what you can do is you can just follow this   guide so you will see that docs.docker.com engine  install which is the official documentation of   installation of Docker desktop on the machine from  here what you can do is you can go to the docker   desktop and you can have the docker installed  so I'm using Mac so I install the Mac you can   go for Windows or Linux so now once you install  the docker on your machine you will have a Docker   desktop which will look something like this so  you will see this is a Docker desktop and here   you will see that containers so at the present  I have no containers which is running any image   if I go to images then you will see that we have  a local images and we have remote repository so   we're going to create the image on the local so  you will see that I already have one image for   the mongodb which is on my local and the image is  not running then here you will see the volumes and   then you can have some more Dev environments and  extension so now let's go and let's create the   image of our node.js application education so this  was our node.js application what I'm going to do   is I'm first going to kill this and then before  we create the image we need to have some more   files so what I will do is I'm going to have the  docker ignore so that I don't want uh my files to   go into the image so what I will do is I'm going  to add the node modules so let me have the node   modules all right I will also have the npm debug  log and I will also have the dot EnV I'm going   to copy this and I will also add these into my  get ignore because I'm going to put this project   into my GitHub repository so I will have the dot  get ignore all right and I'm going to add it here   as well and now what we need to do is in order to  dockerize any application we need to have a Docker   file so let's create a Docker file so I'm going  to go here I will have the docker file so in the   docker file what we are going to do is whenever we  want to dockerize any application first we need to   have a base image so from which base image we  need to have the this Docker image so we are   going to have a from here and this will say that  you need to have a base image so now let's type   the base image name so we are having the node.js  so I'm going to have the node 18 hyphen Alpine   so now you must be wondering that from where we  got this node 18 Alpine as a base image so what   we can do is we can go to the docker desktop and  we can make a search here so if I search for the   node then here you will see that this is the quick  reference and this is maintained by node.js Docker   team and here you will see that Alpine different  different versions which are available which you   can reference in your Docker file so now let's  go back and here you will see that we are having   the base image now and then from the base image  what we have to do is first we have to create a   app directory so let's have the app directory so  inside the docker container where this application   is going to run so this application is going to  run into a working directory which is a work dir   and I'm going to give the work dir as the app  all right so this will be my working directory   now what we have to do is we need to install all  the dependencies into this working directory so   I'm going to have the install app dependencies  and here I'm going to have copy my packet Json   file from the current directory so the current  directory is if I go here and if I type PWD what   is my current working directory then from this  application I want to copy this packet Json file   into my app directory so I'm going to have my  package Json file and I want to copy it into   dot slash so when I say dot slash it is the slash  app directory now we have copied and once we copy   the packages and we need to run the npm install  so let's run the npm install so I'm going to   have the Run npm install all right so here I'm  going to have the Run command and we can give   the command as npm install so this is going to  install all the dependencies now once you have   all the dependencies installed you need to copy  all the source code into the app directory so   now let's copy the bundle up source so I'm going  to have the bundle app source and yes we need to   copy so I'm going to have the call copy from my  current directory to my current working directory   so this is going to copy and then we can expose  the port which we want to expose so we are using   in the server.js as 8080 so let's expose that port  8080 and then we can have some executable commands   so if we want to run our application what commands  we use usually if we go to package.json we use npm   start so let's add that command so I'm going to  have the npm here all right and then we're going   to have the start all right I will save it now  you will see that we have all the commands in   our Docker file now let's give a try to build  a Docker image out of this so what I will do is   I'm going to have a Docker command here so you can  have the docker and you can have the docker hyphen   H which is going to give you all the commands  for the docker all right so these are all the   commands and we are going to use the build command  so let's have the build command first so I'm going   to go down and here what I will do is I'm going to  have the docker build so this is going to build my   image and for the image I also want to have a tag  to that image so I'm going to put a flag as T and   I want to run this on the current directly which  is the dot and I'm going to give the name as the   malvia Slash and let's have the node web app and  I'm going to give the version as one because we   are going to create multiple versions of this  image so I'm going to have this and I'm going   to hit enter all right oops so we made a mistake  so it should be first we should have the dot so   it will build in the current directory and then  we can give the tag to our image so I'm going to   hit enter and you will see that it is first going  to install all the dependencies which are from our   base image so first time it takes a little bit  time because it's installing the dependencies   first time but the next time when you build the  image from this code it will be much faster so   we're going to wait for it all right so now you  can see that the docker image is built in and if   we scroll up and see then what it actually do  is have the First Command which is from Docker   which is our this from node.js the base image so  from the base image it is downloading all the the   dependencies in the next command it is executing  is the working directory then copy the packet Json   npm install copy all the source code and then  it is having the image all right so this is our   image so now if we go and if we see in our images  then we should be able to see that we have the new   image which is the node web app which is on the  local all right if we click on the details then   here you will see that we have all the details  related to the package all right so now if we go   here and if we want to see with the command then  what we can do is we can have the docker images   and this is going to show us the image which we  have now what we are going to do is we are going   to run this image so either you can run this image  from the desktop itself you can just click on it   and it will start but what we will do is we are  going to run it via the command line so I'm going   to clear the console to run it what we will do  is we are going to use the docker run command   and then we are going to run it in the detach mode  that means it will run in the background and we   will still have the console to use but if we use  it in an interactive mode so in that we will have   the hyphen ID and then it is going to execute all  the container stuff in the same command line so   we will not be able to use the same terminal so  I'm going to have it in a detached mode and then   we are going to give the port as a 8080 so this  port is from the container Port so inside the   container I want to run it as let's say 801 and  the application Port is the 8080 so this is the   Now map and then let's have our image so I'm going  to have the the malvia I'm going to have the node   web app oops web app and we're going to have the  version one and let's hit enter so now you will   see that the container is running so if we go and  verify in the container then we will see that we   have the container which is running so you will  see that there is a name called awesome Darwin we   can also give a container custom name all right  our application is running on the port 801 and   now let's give a try so I'm going to go here and  I'm going to have the localhost 8001 and when I   hit enter then you will see the app is running  on the docker container so this is how we have   created an image of the node.js application and  then run it on a Docker container now we're going   to see some more commands which are very useful  when we work with the docker containers so now   if I want to list down all the containers which  are running then I'm going to do a Docker PS and   this is going to give us the container information  the next thing what we can do is if we want to see   all the docker containers which are created we can  simply use hyphen a and then that is going to give   us the list of all the docker containers now if  you wish to see the logs let me first clear the   console so I'm going to clear the console we're  going to have the docker logs and before that we   will also need the docker ID so I'm going to have  this and then I'm going to have the docker logs   all right and let's take the ID so this is the  ID I'm going to copy and then I'm going to add   it here you will see that these are our actual  application logs which is the node mon server.js   app is running on eight zero eight zero now if  we want to stop the container we can simply use   the docker stop and then we have the I ID so this  is going to stop our container all right you will   see that the container is now stopped and if we go  here and then if we want to see then we will see   that the container is status is exited it's not  running anymore and you can just start from here   or you can start it with Docker start the next  thing I want to show you is if you want to log   into the Container then what you can do is you can  have the docker execute the it then we are going   to give the docker container and then we are going  to have the slash bin slash best so let's see our   Docker container is top but still we can go into  the box or not so I'm going to hit enter and we   see that this container is not running so let's  start the container first so I'm going to have   the start here all right I will have the start  and now the docker container is running and what   I will do is I'm going to just clear the console  and now I'm going to execute this all right so   it says that our Docker container doesn't have a  bin bash so the alternative what we can do is we   can also use the other shell which is this one and  let's hit and enter and then you will see that we   are in the slash app directory so I'm going to do  an LS and let's go One Directory up all right and   then if I do LS then you will see that we have our  container we have already inside our container and   then if we go to our app so let's go to see the  app and let's do LS iPhone LTR so this is going   to give us all our application stops so this is  our source code which is in our Docker container   all right so what I will do is to exit it I'm just  going to have the exit and now we are exited from   the box the docker container is running let's  use a curl command and let's see so I can have   the curl hyphen I let's have the localhost colon  5000 oops not five thousand eight zero zero one   and then it's going to give us the response and  this is our response app is running on the docker   container now the one thing I want to show you is  that uh if I go and make a change in our file so   app is running on Docker container with version  2. all right I save it and then if I try to run   this again call command then you will see that the  content is still the old one so it's not getting   updated it so for that what we can actually do is  we can have a Docker Mount volume to our running   container so we can mount this to a running image  in our container so how we can do that is let's   clear this first and now we are going to mount our  volume to The Container so what I'm going to do   is I'm going to have a Docker run and then we're  going to have it in a detached mode I'm going to   give a port so we're going to have the container  Port as 8001 and our application port at 8000 and   we're going to mount it so we are going to have  the hyphen V and then I want to mount my current   working directory so in Mac if I want to mount  my current working directory I'm going to have   the PWD and this I have to mount with Slash app  so I'm going to have the slash app here and then   let's give the name so I'm going to have the name  to my container so this time the name is uh node   web app container so let's have the container  and then we are going to give the image so I'm   going to have the image here as node hyphen web  hyphen app and V1 so you will see that now the   application is running and now if we want to see  then what I will do is I'm going to have the curl   command so let's have the curl and let's make an  request so if I hit it then you will see that the   app is running on the docker container version  2. now let's make it as a version or let's remove   this and let's change this as we have mounted the  volume to running container all right I will save   it and now if I go and execute the request again  then you will see that we have mounted the volume   we don't have to create a new image every time  whenever me are making the changes all right   so this way you can actually mount it and if you  want to pass some environment variables so what   you can do is uh I'm going to go here and let's  pass an environment variable so I'm going to go   and I will have an environment variable here so  so what I can do is I can have an hyphen e which   is my environment variable and I can give the port  as 5001 all right since I'm giving the file 5001   Port I have to change the port here as well all  right so I'm going to change this 5001 all right   I'm going to hit enter and you will see that error  from the demon conflict the name is already using   so okay let's give a different name so this time  I'm going to have a V2 as container name and I'm   going to hit enter and we are also having this  port as a complex so let's have a change the   port as well here so I'm going to change this  to so let's change this to eight zero zero two   all right I'm going to hit enter so now we have  changed this to version 3 and then now we will   see that we have a port as eight zero zero two  and five zero zero one so we are not going to   use the port which is defined as eight zero  zero inside our application we are passing an   environment variable so now let's go and let's  try so I'm going to go to my desktop and here   you will see that we have the same application  version but those application version are running   on different ports so we can just delete this we  don't want this all right I'm going to delete it   and now you'll see that we have two different  applications One is using the port from the   environment variable and one is using the port  from the application code itself so we have the   8001 which is this one and now we will also have  the 8002 so let's have the Local Host eight zero   zero two all right so this way you can also pass  the environment variable uh in the docker command   now we can also have the environment variable  file if we want to pass it so what I'm going to   do is let's create an environment variable file so  I'm going to have the dot EnV all right I'm going   to give a port here so this time the port will be  5010 and what I want is my application this should   use the port as 5010 which is from the environment  file so we have also installed the dot ENB package   so let's make a use of this so what I'm going to  do is I'm going to have the import dot EnV and   then I will have the from and this will be from  dot EnV I'm going to have the dot env.config all   right so that it will use the process dot env dot  port and now let's run this or we can also create   a new image so what I will do is let's create a  new image first so I'm going to have the oops I   will just clear the console first so let's have  it clear and let's build it so I'm going to have   a Docker build from current directory let's give  a tag I'm going to have the name as the malvia   node web app call V2 so this is going to build  the second version of the app and now we can   see all the images so I'm going to have the docker  image oops it should be Docker images and you will   see that we have the version 2 app now if we want  to run that version to app but we will also need   to have this environment file to be included all  right so what we are going to do is we are going   to have the same Docker run hyphen D we are going  to have the port mapping so let's map the port to   5010 so I'm going to come here all right first  I will have the version 2 to be running then I'm   going to create a new container name so I'm going  to have the node web app EnV hyphen container all   right and then what we will do is we are going to  change this and and we are going to have here as   environment files so I'm going to have the hyphen  hyphen e and V hyphen file and then we just need   to do a reference so I'm going to have the dot  slash dot EnV so take the en.env file from the   current directory and have it as an environment  file all right we need to change the port so let's   have the 5010 and then I'm going to run this time  as on three all right I'm going to hit enter and   you will see that the container is now running so  if we go and refresh then we should have the other   container which is the node web app EnV container  so all the three containers are running and let's   give a try so it should have a 8003 so I'm going  to go here then I'm going to go here I'm going to   have the localhost 8003 and we see that we have  the mounted all right we have all the commands   now if we want to list down all the containers  then we can have the docker PS hyphen a and if   we want to remove it we can use the docker remove  and then we'll give the container ID so I'm going   to copy this country dinner ID and then I'm going  to add it here but you cannot remove a container   directly but you have to stop it or you have to  kill it so what I will do is I'm going to have   the docker kill and this is going to kill the  container and now if I want to remove it I can   remove that all right so now if we go and see then  we will have only two containers for running all   right so similarly we can also remove the images  so if we want to remove the images what I will do   is I'm going to have the docker images and if we  want to remove it what we will do is we're going   to have Docker remove image let's have the image  ID so before deleting any image it should not be   on a running container so we have exited this  too and what we will do is first let's delete   them so I'm going to delete this containers all  right and the contains are deleted now you can   go to the images and you can delete the images or  you can just use the docker RMI and then you give   the ID of the images so let's have the ID so I'm  going to have the docker images I'm going to have   the docker remove image and then let's give the  ID so I'm going to have the ID here and now you   will see that that is untag and it's deleted we  can do similarly for the second one so I'm going   to copy and I have it here and it's deleted so  now if we go to images then we will see only   one images left so that's how you can actually  manage your node.js application with Docker or   you can dockerize your node.js application we  have also seen how we do the uh how we add the   environment variables or environment variable  files and some different commands which are   very handy to use Docker we can also do all  those operations with the docker desktop and   it's really the preference of the developer how  how he has to do either via the docker commands   or the docker desktop so that's all I have in  this video I hope you liked the video a thumbs   up is appreciated you can also connect with me  via Facebook or Instagram and you can follow me   on Twitter for latest updates but before you go  don't forget to subscribe the channel and press   the Bell icon so that you don't miss the videos  like this one thank you thanks for watching
Info
Channel: Dipesh Malvia
Views: 11,879
Rating: undefined out of 5
Keywords: nodejs, docker, docker tutorial, learn nodejs, nodejs tutorial, node js, nodejs docker tutorial, nodejs docker container, running nodejs docker, docker nodejs, docker node server, docker node, docker express, how to create a docker file, dockerizing a node.js web app, dockerize node app, dockerizing genetic node app, nodejs app docker container, docker node js, dockerize node, nodejs docker beginners tutorial
Id: nH47lsxvY9c
Channel Id: undefined
Length: 25min 47sec (1547 seconds)
Published: Thu Jul 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.