Waiting..
Auto Scroll
Sync
Top
Bottom
Select text to annotate, Click play in YouTube to begin
00:00:00
hi this is tom das tech i'm tom and in this video i'm going to show you the easiest node.js deployment you've ever seen so we're going to do this deployment with caddy docker and docker compose and we're going to deploy
00:00:12
the application onto a digital ocean droplet but you could use this method to deploy an application onto aws ec2 instance or even a raspberry pi any vpc will do so this is what we're going to build so
00:00:26
we're going to have our vpc which is in this case is going to be a digital ocean droplet and then we're going to have docker on that droplet and then we're going to have a caddy server which uses a reverse proxy to make requests to our node.js
00:00:39
application and all these requests are going to be made over tls so why are we using caddy well there's two main reasons why you would use caddy and the first one is automatic tls or
00:00:50
ssl or https whatever you want to call it the correct term is tls and easy configuration and when i say easy configuration it is by far the easiest configuration you've ever seen
00:01:03
so caddy is an alternative to nginx or apache after you watch this tutorial i doubt you'll ever reach for nginx or apache ever again for a side project first we'll walk through what we're going to be doing
00:01:16
so we're going to be setting up a dockerfile and a docker compose file and then we're going to create the caddy file and then we're going to create a deploy script next we'll create a digital ocean
00:01:29
droplet and we'll point our domain at the droplet and then we're going to ssh into the server pull our repository and run the deploy script and then finally we're going to verify
00:01:41
that our application has tls enabled with this ssl labs ssl test so if we have a look at what i have here i have a small node application that just runs a http
00:01:54
server that's served on port 4000 and the host is 0.0.0.0 this is important because you don't want to use localhost here like that docker will not like that and
00:02:08
you will have lots of problems so you need to use 0.0.0.0 this application has a couple of dev dependencies we have the node types package and we have typescript and we have one
00:02:21
script and that's a build script so if you quickly run this application and check what it does you can see that it serves one end point on port 4000 and if we curl that end point we can see that it will just return hello world
00:02:33
and we have hello world here so the first thing we'll do is create a docker file and this dockerfile is going to be responsible for building our application so we'll use the node 14 alpine image
00:02:46
so this is node version 14 and the alpine image is just a little bit smaller we're going to add the package.json to a temporary folder we're going to run a command to remove our build folder we're going to go into our temporary
00:02:59
folder where our package.json is and we're going to run npm install we're going to add the source directory and we're going to remove the node modules from source directory and we're going to copy the node modules into this
00:03:11
temporary folder next we're going to set the work directory as the source directory because this is a typescript application we need to run a build script and finally we need to start our application with node and you might be wondering why we aren't
00:03:24
exposing a port here so we would expose 4000 it's because we're going to use cadi to proxy into this application so we don't need to expose this application the next thing we're going to do is to create a docker compose file
00:03:44
we're going to use version 3.7 we're going to create services so this should be docker compose.ynl we'll get better syntax highlighting them we're going to call our application nodap
00:03:58
the container name is going to be node app i'm going to set one environment variable and that's the node m is going to be production we'll add build and then context so this is our node
00:04:11
application image ready to go the next thing we're going to do is to create our padding image so it's going to pull the image from caddy alpine again this is just a smaller version the container name is going to be
00:04:23
padding service we want it to restart unless it's stopped we also want to restart our node application on this stopped we're going to expose port 80 and port 443 so port 80 is for http request and port
00:04:36
443 is for https requests paddy requires that we add some volumes here so this one here is going to be our caddy file it's going to be moved into this etc caddy caddy file finally we need to
00:04:49
add our volumes next thing we need to do is to create a caddy file so this is a lot like a docker file but for caddy so my application is going to be paddy dot snipped
00:05:00
dot io and i'm going to reverse proxy to our node app on port 4000 and i'm going to add this header here strict transport security with a max age of this number here
00:05:14
so you if you don't want to add that header you can remove this line here but we do want to add this in this case the last thing we want to do is add a deploy script so add deploy dot sh
00:05:25
this deploy script is going to be used to run some commands for us so we don't need to type them into the terminal every time we want to deploy so this one here is just going to echo out pulling and then it's going to run and get cool the next one is going to echo our
00:05:38
building application and it's going to run docker compose in detach mode and it's going to run build so let's push these changes to github and we are ready to create our digital ocean droplets
00:05:52
we're on our digitalocean dashboard if you don't have a digitalocean account already you can sign up with my referral link that will be in the description below and you'll receive a hundred dollars of credit and i'll receive 25 dollars of credit
00:06:04
if you don't want to use that link that's okay as well so click create and click droplet and we want to create our droplet from a marketplace image select the docker image this image will
00:06:19
have docker installed for us already otherwise you'll have to install docker manually if you click regular intel ssd you can select the five dollar a month claim select a region that's closest to your
00:06:32
customers select your ssh key if you don't have one you should set one up or you can use a password but i highly recommend setting up your ssh key you can name your droplet anything you
00:06:48
like so i'm going to pull my caddy and then click create droplet once your droplets finish creating click the little copy button next to the ip address come over to your domain name registrar mine is namecheap
00:07:03
and click add new record my host is going to be a subdomain so i'm calling it paddy and it needs to point to your digitalocean droplet ip address if you don't want to use a subdomain
00:07:16
you'll need to use this at symbol and point that at your ip address click ok now that our digital ocean droplet has finished building and we have our domain pointing at
00:07:28
our droplet we can ssh into the droplet run apt-get update to update ubuntu's repository run apt-get install kit to install git cd into slash bar own your git
00:07:41
repository cd into the git repository on chmod on the deploy script so we can execute it
00:07:49
and execute the deploy script and that is your application deployed and running we can see the docket containers running with docker container ls and we can see how we have our node
00:08:10
application and the caddy server so the last thing we need to do is go to our domain to make sure caddy is corresponding and we can see hello world so if we inspect the network request we can see that the response
00:08:22
includes this strict transport security which means it's coming from caddy the last thing we need to do is run this ssl test to make sure that our website is actually hosted through a secure connection
00:08:38
you can see that ssl labs has given us an a plus if you find this video helpful please drop a like and a subscribe thank you for watching
End of transcript