Using Docker for Local Development in NodeJS projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we will see how we can use docker and docker compose for our local development so in a previous previous video we showed how we can use uh docker for our nodejs applications here was our docker file and in this application we will see how we can use docker for our local development as well so usually for our local development what we do is we uh inside our package.json file we have some command let's say something like this and we just run it let's say under dev and our application will be run uh it's connected successfully and if we see if we hit you'll see that our application is returning this message hello world right so it works but there are some problems with this approach the problem is let's say your application uses some special features that is provided by node.js 18 but you this project is being worked on by by uh five developers it's not guaranteed that all of those developers have nodejs 18 installed on their machine even if that is installed it's a very painful thing to switch between node versions uh from project project so you can solve this problem by using docker uh what will you do is uh if you already have a docker file until this point you have used it to run this in a production environment but now we will use this file along with a docker compose file for our local development as well so to do that create a new file called docker dash compose.yaml and uh open that up and let's see what's in inside this particular file first we have our version uh then we have our service so docker compose the purpose of docker compose is to uh include many services uh we can use database and our application in a same docker compose file it will be even more useful but for now we will not do that so inside services we have only one single service which is express typescript docker service ah as environment we have passed this variable inside the environment we have to pass it manually so we have set the node environment as development ah for building ah for building we are using uh this context i am in our current directory and as a dockerfile you are using dockerfile.tab so you will notice that i have created a new file called dockerfile.temp which will look a little different from our original docker file so what's different there if you go inside docker file you can see that after we copy our application code we expose the port build the project and run the project but this time these three commands will be taken care of by our compose file that's why i have created a separate docker file dockerfile.dev which is almost exactly like this setup except these three lines you will see that i am this is our base image our work directory i am copying everything i am installing everything and copying the application code that's it so if we open up our docker compose.tml file we are using this dockerfile.dev not the dockerfile okay volumes is the uh original directory or your file storage which will be used by docker and you can use it as default something like this uh we can you can name your container so what's your container name it's express typescript docker uh we will be exposing a particular port so in inside docker file notice that we have exposed report but this time we are doing it inside the compose file that's why we didn't need these three files so we are exposing a port we are mapping the ports so when we run a particular docker image we map the ports like this right i i hope you remember that we map this application port 3000 with our container board 3000 and inside our docker compass file we are doing the same thing our application port mapping our application port with our container port and the final command that will be done is npm run dev remember we are doing the local development with docker composite so that's why we will run npm run dev and this is a pretty basic setup it will work with most of your applications most of your uh not just applications and now let's see uh if it works or not so what you have to do is docker compose so you will notice that our application this time is not running on our machine it's running inside a docker container okay and it's connected successfully on port 3000 let's try to hit it and see if it returns us hello world yes it returns us hello world but in in local development we also want to change and update our code and see the changes instantly right so let's update our code so instead of hello world it should now return hello world updated and you will notice that our application is rebuilt restarted and it is connected again so this time when we will update it or hit the end point it should return as hello world update yes and sure enough you can see that we are getting hello world updated now which means our code change was uh reflected directly without stopping or starting anything uh inside this uh docker container okay so ah that's the main thing you can stop it by running ctrl c and it will just stop everything and if it stop everything will not get any response and yeah that's ah that's about it so i hope you now have a better understanding of why you should use docker for your local development and how you can use docker compose the power of docker compass for your local development have a great day thank you i have a blog i have written a blog for this so you will get that blog in the description and also the project that i am showing you is also available on github i'll link it in the description as well thank you so much
Info
Channel: Mohammad Faisal
Views: 7,094
Rating: undefined out of 5
Keywords: Nodejs, Docker, Dockerfile, Docker Development, ExpressJS, NestJS, Typescript, Docker Compose, Local Development, Javascript, Backend Development, Containers
Id: nawJwaPW1yI
Channel Id: undefined
Length: 6min 8sec (368 seconds)
Published: Tue Aug 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.