The Easiest Way To Deploy Nodejs With Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
at some point in your career you're going to end up working with node.js and when it comes time to deploy that you're probably going to be looking towards Docker for your deployment so in this video I'm going to show you how to dockerize a node.js application start off I want to point out this file that I created dot Docker ignore it works just like a git ignore file and I added the node modules folder to it the reason I did that is whenever we bring these files into our Docker context I don't want node modules because that's specific to my operating system which is different than the runtime that I'm targeting in node so we can just save ourselves some problems and some Heartache by excluding that and saving a lot of space and overhead too because node modules tends to get pretty big next thing we need to do is create a Docker file itself and it's just that it's a file named Docker file make sure you capitalize it with a capital D and let's start building this out we are going to start using the official node image node version 18 with the bullseye variant meaning it's running on Debian Bullseye which is the latest release of Debian as it of the recording of this video last thing that's worth noting on this line is I'm calling this as Builder so this actually has a name of Builder and I'll show you why that's important towards the end of the video here now if you look at our code here on the left you can see this application is written in typescript so we need to transpile that from typescript to Common JS so node can read that normally in a development application or in development environment when you're working with it locally you'll use something like nodemon to transpile it on the fly but in our production application we we don't need to transpile it on the Fly and we don't want to transpile it on the Fly because that adds extra overhead and impacts the performance of our application so we're going to transpile it once as we're building our image and be done with it we need to copy over our package.json file that's going to let us install our node dependencies that our application depends on and then we're going to go ahead and copy over the rest of the files from our application into our Docker context here then we're going to run the npm Run compile command so this is telling npm to run the script name compile and we can get an idea what that looks like in our package.json file because if we look in the scripts we'll find the script named compile and that actually runs this Command right here so let's break it down and see what it's doing shx is a common Unix emulation utility I guess for lack of a better word basically it allows you to run unix commands anywhere regardless of your operating system and in this instance we're going to run the RM command with the dash RF flags and point that at the lib folder so basically it's going to remove this lib folder and then we're going to run our TSC command which is going to transpile our typescript into common JS and it's going to spit that out into our lib folder so basically we nuke the lib folder in case it's already got something in there so that when TSC runs we know we get the latest and greatest version switching back to our Docker file here we're now starting a new Docker image here again again we're targeting the node version 18 Bullseye image and we're changing our working directory over to home slash node now whenever you use the official node.js images there's automatically a user called node in there for you and we're going to leverage that to implement some security here in a minute the next thing I'm doing is I'm copying over my sqlite database and I want to point out and make this really really clear this is specific to this video only in a production environment you would not be putting your database inside of your Docker container you would have a database that's hosted somewhere else externally and you would provide the connection settings to your application to it the reason that you don't put your database inside of your Docker image in case you don't know is because whenever we launch this container we run it we'll be able to read and write to this database but the minute that I terminate that Docker container all the data that we had in that database is lost and gone forever which is bad for a production environment the only reason I'm doing it for this video is to just show you the working application at the end we're going to copy over next our config and public folders from our source over here config contains the configuration for our app application and public contains the index.html file for presenting the UI for an application then we're going to copy over something else and you'll notice this has the dash dash from equal Builder line in it and what that's referring to is this Docker image that we created up here as part of our multi-stage build we named this Builder and so now whenever we copy this we can say hey from that image you created named Builder grab the home node lib folder and copy it into this image as the folder lib what that's going to do is it's going to grab all of the common JS that we created in that previous image and just grab that but then everything else that existed in this Builder image we can leave behind which gives us cleaner more streamlined production images now we're going to switch over to our user node I mentioned that using the official node.js images comes automatically with user node we're going to switch over to that user now and then everything that we do below this is going to be executed as that user node everything up here above it was being executed as a user root and now we're going to be using the user node the reason that we do that is because at some point your application is going to be hacked and we don't want to give that attacker root privilege root privileges where root means the Unix user with the ID of zero named root who has complete unrestricted access to everything using it this way when you do get hacked the only thing your attacker is going to be able to do is whatever permissions our user node has which are very very minimal permissions so it's still going to sucked when you get hacked but it's going to suck a lot less than if you get hacked as your root user again we're going to copy over our package.json run npm install to grab our node dependencies then we're going to expose Port 3030 and finally set our Command to run when the docker image starts or the docker container starts net command is going to be npm run start so if we take a look at our package Json one more time we find our start command here it runs the node command and then gives it a path of our lib folder which is the common JS that we created with typescript compile command now we can actually build a Docker image from our Docker file so to do that we'll do the docker build command Dash T we'll tag that as well button slash feathers Dash chat and give it the latest tag and then I always forget this but don't forget your trailing dot at the end to let the docker build command know that you're referring to this current directory that you're in so once that builds we can actually push that up to Docker so we'll do Docker push and then Supply the name of our Docker image and let's log into Docker and then we'll actually try that again this should work out a lot better for us and now that that's pushed up there we can actually jump over here to Docker Hub we can refresh this page and we should see our image and there it is right there so now anyone can use this image we'll actually just do that we'll do Docker run and imprivide that next we need to expose our ports here so we'll do Dash p 30 30 30 30. so that's up and running and if we go to localhost on Port 3030 there's our application we'll log in with hello at feathers.js.com with a password of a super secret don't tell anyone my password and here's our application so let's see we should be able to type a message here and there we go so that's how you build a Docker image for a node.js application with a multi-stage build if you liked the video please be sure and give it a like And subscribe to the channel and let me know in the comments what you liked about it what you didn't like and what else you would like to see next thanks and I'll see you all in the next video [Applause]
Info
Channel: DevOps For Developers
Views: 4,106
Rating: undefined out of 5
Keywords: docker, containers, docker tutorial, learn docker, node js
Id: vI7p3SQDAMA
Channel Id: undefined
Length: 9min 13sec (553 seconds)
Published: Mon Mar 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.