Docker-izing a NodeJS ExpressJS API - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Do we need docker ?

👍︎︎ 2 👤︎︎ u/vfhd 📅︎︎ Mar 01 2019 🗫︎ replies

Thanks for this.

👍︎︎ 1 👤︎︎ u/Turdsonahook 📅︎︎ Mar 01 2019 🗫︎ replies
Captions
[Music] hi everyone its Elliott from tutorial age dotnet and in this tutorial we're gonna be looking at how you can darker eyes an existing nodejs application and ultimately leverage the benefits of docker as always the full text version of this tutorial can be found by website and I'll leave a link to this in the description below so with the Nesta torial we're gonna be looking at how you can create a docker image that will dynamically pack up changes to a new GS application and automatically recompile these changes and rerun our application without us having to rebuild and we run our docker image so why are we doing this well doctor offers a number of massive advantages and it can drastically reduce the fraction of deploying your application to multiple platforms with minimal fuss new developers can easily peel them and authorize the applications and run them with just a couple simple commands on their local machine without having to configure things like environment variables and saying the correct version of the underlying runtimes application developers can explicitly state within a docker fail everything that application needs in order to run so let's jump into our text editor of choice now for this tutorial I'm gonna be using visual studio code first thing we're gonna want to do is to create a new file called app GS never then that's Abdo GS we're gonna specify a really simple no GS Express GS REST API so Const Express equals require Express like so next we're going to want to set up our applications so Const a p-- equals express and finally we're going to want to define the route api endpoint which will simply return a hello world whenever we hit that endpoint so res dot send hello world nice and simple and in order to start our server we're gonna want to do app but lesson specifying port 3000 and then within the body of this we want to do console.log my REST API running on port 3000 and that's all there is to it now in order for us to initialize our project we're going to want to do NPM and it within the root directory we're then going to want to step through all of the questions that it prompts us with and give it a name like REST API docker will stay with version 1.0 leave the description blank the entry point will be a PS we're not gonna have a test command for now and step through these and family will say yes this is ok this will create the package Jason and we can now install the Express GS library by doing NPM save Express and you'll see this adds the Express to our dependencies less like so now in order to get this running we're gonna have to add a start command to our script section within here we're gonna say start and we want to do node app dot GS like so within our console will then want to test that's worked by calling NPM run start and you can see a REST API is no successfully lessoning on localhost port 3000 and if we had that in the browser you'll see success hello world has returned with a response body so now that we've got our very simple node.js Express REST API how do we go a bit dollar rising mass well the first step we'll need to take is to add a new docker fail like so to the root of our applications directory with the nest we're gonna want to specify the base image that we wish to base our docker image from so in this example I'm gonna use the Nords image and I'm going to use the 9 - slam tag next I'm going to specify the work there which well essentially dictate exactly where all of our applications and any subsequent commands we run within a docker fail will run within this directory next we want to copy our package JSON into our app directory that we've just created then once we've got this package JSON we want to run the NPM install command now the reason I'm not copying all of my applications source fails into the application directory right now is so that we can cache these steps so docker is quite intelligent and the way that it handles the building of our images now if we do it like this we can then cache these steps so that every time we make a change and rebuild our application and our docker file we don't have to run through these steps again now that we've got our applications dependencies all installed we then need to copy over our applications source files to the app directory and finally to kick off our application we want to call NPM and start perfect and we've got slight typo here so just delete these periods and then we want to build our docker container our docker image so docker belt we use the dash T like to specify the name of our docker image and we'll call it nude docker tutorial and we'll specify the path of our docker file which is just and the same directory that's we'll then go away and run through a series of steps that we've defined up here and it'll build our complete docker image now from that docker image we can then start to spin up containers and the way we do that is by calling docker run - IT to give it an interactive terminal and then we want specify the port that everyone's on so Wells our application may be running on port 3000 this will be port 3000 within our docker container so we need to map that to a free port on our local machine so one of the free ports in my machine is pour 9000 we map this to the internal port of 3000 and then we specify the docker image we Wester wesh2 run north docker tutorial like so and as you can see our docker container successfully started running on localhost port 9000 but not to the internal container port of 3000 so if we go away into our browser and we had 3000 we should see that were unable to connect but if we change this to 9000 which is the port we've mapped it to we should be seeing our hello world application perfect now if we wanted to run this docker container in the background we should come out of here by control seeing and we could do docker run Manistee to run a detached mode and we could again specify the port 9000 to 3000 mapping specifying our docker image and then pressing enter this will go away and it will start up the container in the background now if we want to see the list of running containers we type the docker PS command and as you can see we've got our nodes docker tutorial image running it was created four seconds ago and it's been up for nine seconds and again we can see things like the port mapping that we've specified so we've been able to successfully docker eyes that's node.js application however if we wanted to make changes to our source code then we would have to shut down or running container and then rebuilt the image and then subsequently start up that image again so let's look at how we can optimize this process by using things like Nordmann and how we can use things like v volume flags so let's first see how we can automatically pack up changes and rerun those changes now to do this we're going to be using nord Mon now if you come into your package JSON and change your start command to nude Mon GS and then try to run npm run start within your console you should see the node 1 process kicks off and then starts watching any of our fails for any changes so if i come in here and make a simple change so hello youtube save that i will see these changes and then check off our server once the and if you come into our browser have a look at port 3000 because we're running this locally you see that hello YouTube has no been returned so well stats nude man change might work locally it won't yet work within our docker container now the first thing we need to do is effects that is to do NPM install - save node Mon this will explicitly address to the list of dependencies within our package JSON so that our docker container on belt will then have access to that node module once we verified that node one has explicitly defined within our dependencies lest we can then rebuild our docker image so docker bailed Manistee node docker tutorial and again specify the path run that and you should see that because the underlying package dot jason has changed it will then start to redo this particular task okay no that's done our docker image should now have node one within it so let's go ahead and try and run a stalker image alongside our existing docker image that we created not long ago so docker run we're gonna want to do this and interactive mood just so we can see the changes getting picked up we don't want to specify the port and we've already allocated the previous container to 9000 so let's change this to 9001 mapping to 3000 and we're then going to specify the volume flag and we'll need to do dollar sign print working directory and slash app like so finally Norwood docker tutorial it's best fitting our image name and press Enter that should go away no let's volume to our container so that it picks up any changes and if we come in here we can validate less by changing us back to hello it like so and as you can see our docker container picks up these changes and if we then navigate to localhost 9001 you'll see hello Elia it's getting returned now just to validate let's go back to the localhost 9000 and you see our unchanged docker container from our previous section of this tutorial and that's double returning HelloWorld so we now have two distinct versions of our application running within two separate docker containers on the same machine excellent so in this tutorial we've learned how to implement a talker container that perfectly wraps are in our node.js application this docker container is now deployable anywhere that can run docker which is a massive advantage no more worrying about the fact that it works on my machine so that's what we're gonna cover in this tutorial if you enjoyed it then please feel free to leave a like and subscribe to my channel for more programming paste content Cheers
Info
Channel: TutorialEdge
Views: 158,106
Rating: undefined out of 5
Keywords: docker, nodejs, expressjs, docker tutorial, basic docker, dockerizing nodejs, running nodejs docker
Id: CsWoMpK3EtE
Channel Id: undefined
Length: 12min 17sec (737 seconds)
Published: Sun Jun 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.