NestJS Dockerized | Simple NestJS Starter dockerized | NestJS X Docker 01

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome back in this video we are just very simply dockerizing the nest js starter app so for this we need vn pm we need note 3s we need nestle as of course and docker and what we basically do if you hit them with vanessa just cli you can hit nest new and then the app name so we can just do this here so we say nest new and then we can say for example necessary as dockerized and then this will generate here a new folder for us with the basic template for our project so we want to use npm and then you see it created finance jsocarized folder for us for all the files for running or starting a basic sjs application and we can just wait till this is finished so it's installing now the npm or all the node modules that are specified here in this package json file and if this is finished we can just say npm run start dev and this will start our nsjs application for us and you can see it here in our source in our controller or let's start with our main here you see our app is listening to port 3000 our controller is also listening here with a simple get request and is returning the hello world or get hello function from our app service which returns just the string hello world so now all the modules are there and we can just say npm run start depth so our so of course we have the cd into our folder and then we can hit this again and now you see we hit nest start with the watch and then we should now see after everything is compiled that our next class application is started you see it here and we have our app controller we have mapped the simple get route and so now we can make a very simple get request of our browser to port 3000 and we get to see hello world of course you can also do this with the postman with occur whatever you like and now we can finish this so you see if we make here a reload then this searches for an answer and should now return it for four patron or didn't you know answer so now we want to put this stuff here inside the docker container and then start the container and everything should be available as before and then on a different port so let's see how this works basically we have um then inside our container our docker container our nsjs application which is still listening to port 3000 and when we start our container we start with all the images that we need so for example with the node.js image inside our container and then with our s3s we build this when this is listening inside the port 3000 our container on the outside is listening for example to a different port so we could say that our container is listening to port 8080 on the outside and then it's mapping all requests against this port to the inside port and the container to port 3000 and then there it's listening application and is for example returning then we hello world so what we can very simply do we can follow this tutorial from the official node.js page there they want to have a simple node.js application to put inside a docker container and this is like mine just for development not for production we can do another video just for production purposes so they are creating us first part a very simple web application in notre s and then we build or they build a docker image and then they will initiate a container for that image um because we already have all our stuff so we have our necessity as application here which is basically node.js application we have our package json files and everything we can you know like skip this part here and they already or they also create a server to as fire and this is very simple just our main dot ts file which is starting on our yeah we're just starting our application so they first create a dockerfile we can also do this here we can just name this dockerfile and then we have not inside this my mistake then we also have this nice looking whale here for docker and the first thing that we want to do is we want to say which image do we need um inside our container or images so we need a node.js and here we say we want the latest long-term support version from node.js 12 but we could already also look on the official docker hub page for node.js and could reference any any image that we want so the next thing is we want to create an app directory where we hold the application code inside our image so we copy and paste this and this is in our container or in our image and so this is nothing to do with here this is just in our container so i can also use this whatever i would like then we can go on so this will be the working directory where we where we are doing all this stuff so this image here it already comes with node.js and npm so we need both and then we can copy and paste this code here where we copy our packet json and this ensures that we copy as well the package json as well as the package lock json and then we hit the command npm or run npm in style and then of course if you would build it for production you would need to do something else and here we are not copying then the complete package json but we are just cop the complete files but we are just copying the package json because then if we rebuild our container [Music] we are not getting all the modules again we are just doing this if something our packet creation changes so the long explanation can be looked up here but you can see it here the too long to read is like we don't rebuild our modules each time we rebuild the container only if something our packet json changes we will rebuild the module the modules so if you want you can read this also here but this is just for having a very short shopping that's working so let's go on and then we want to bundle our complete app source and then they are already exposing a port to to the outer world so for example if we later start this container then we could go to localhost in their case 8080 and of course it's not returning anything at the moment you see it here nothing there but this is the code that is exposed by the container and then they are saying what should be what is the entry file um into here to start the application and then they just say node run the server.js file but in our case it is the main js file and um we all so have to add something here because they have already compiled everything because we are just having the um package.json and the server.js file but we are still having here in the source our typescript file we have to compile this so what we can very simply do is we can run another command so we run our build command from s3s and then we have our compiled files and as you remember as we did here before and this will compile everything to our dist folder and then you see it here we are when our main tsc and our source folder is compiled to main js to main javascript file which can be worked with node.js here so let's go through this so our docker file should now look like this here and of course we have another step here and then we have again to think because we are copying everything here and then building then this will also generate inside our docker vdist folder so we cannot just say node run main file but we have to say node going to dist and then run the main.js file so this should be all i think and let's see they also have a docker ignore so we don't copy for example files from here into our container and then we could like have the wrong version or whatever so if we are here on this root we can just add a docker ignore and copy this name so we don't want to have the node modules or the npm debug files inside it and then we can build our image so this is very simple we can go one folder up we can say docker build and then we just say where do we want to go so we want to build the nest js stockerized and then we can give this also an attack or a name and we can say for example thomas oliver and say nest js dockerized and now you see it's executing all the steps in our docker file so here first it's getting the note 3s12 image let's make it a little bit bigger so ignore this here then we have our working directory inside our container in our image the thomas source app we are copying package json files then we hit npm run install and install all the dependencies here is just warning so that we should here that we are skipping optional dependency then it's running the next step npm run build inside our folder so it's first clearing the list folder then building everything inside our disk folder like here and then we have built in the docker image and then we can verify this by going to docker images and then we can see we have here our latest image has an id it has the latest stack it's created 19 seconds ago and has a size of 1.14 gigabytes gigabytes so the next thing would be to hit docker run and they do docker run then they map the outside to the inside port and this is just how we named our app so we can hit docker run then we map the outside port 8080 from our docker file here to our inside port 3000 from our main ts file here and then we just call it like we named it here so thomas oliver and sjs doctorized and then this should start our docker container so you see we have now starting the nest application it mapped all the routes in our container so i'm listening to port 3000 so you can see if we now hit localhost port 3000 there should be returned nothing you see this is narrow but if we go to localhost port 8080 this is the port our container is listening to and it's mapping it to the inside part of the container to port 3000 where an sjs is listening and is returning the hello world for the basic get request from our browser so this is the tutorial so this was just a very simple dockerfile for hitting the simplest sjs app possible of course we will do like another videos where we add for example a database into our docker image where we and so on or make a more complex app and work with this so yeah just stay tuned
Info
Channel: Thomas Oliver
Views: 10,007
Rating: undefined out of 5
Keywords: nestjs, docker, nestjs & docker, nestjs-dockerized
Id: BrlQthcUHGw
Channel Id: undefined
Length: 13min 41sec (821 seconds)
Published: Thu Oct 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.