Exploring Docker
Lesson 2 of 3 · Exploring Docker [2] – Docker Compose With Node & MongoDB · Programming
Exploring Docker [2] – Docker Compose With Node & MongoDB
[Music] this video is sponsored by dev Mountain if you're interested in learning web development iOS or UX design dev
Mountain is a 12-week design and development boot camp intended to get you a full-time position in the industry to learn more visit dev mountain comm or click the link in the description below
hey what's going on guys welcome to exploring docker part 2 in the first video we just look at docker as a whole what it is what containers are what images are we looked at a bunch of
different commands to run containers in both interactive and detached mode we looked at docker hub and pulling image we looked at a very simple docker file using nginx so just kind of an
introduction now I want to do something that's a little more practical I want to take an existing node j/s application that uses MongoDB it's a very simple application and I want to docker eyes it
by creating a docker file and also I want to create a docker compose file because I want MongoDB to run in a separate container and I want them to be able to basically talk to each other and
we want to be able to easily containerize this application and put it anywhere and we're actually going to put it on a digital ocean droplet alright so let me close that up this is the this is
the app on github now by the time you guys see this you're gonna see the docker file and the docker compose and all that stuff if you want to follow along with me you can clone this
repository and just delete anything that says docker and then you'll basically be where I am now ok and I do have my note my dependencies installed here so I already ran npm
install now first thing we're going to do is just take a look at the application so if we look at our package Jason let's see our dependencies so body parser so we can parse what was
something and whatever comes in from our form it's a very simple just form where we can add an item with a name and then it just lists the items on the page very simple we're using ejs embedded
JavaScript as our template engine expresses our framework for our routes and then Mongo to talk to MongoDB and if we look at our index J s we're just bringing in our
dependencies setting the view engine here we're connecting to MongoDB now notice I'm not using localhost here if you were doing this in a local environment you would use most likely
use localhost Mongo is going to be the name of our service that's going to that's it's gonna be our Mongo container okay so that's why this is not localhost and once we connect we're just bringing
in the model which is very simple take a look real quick just a name and date the date is put automatically so all we have to worry about is the name and we have two routes one is the home page which
just finds the items in the database renders the index template or the index view with the items and then the post is when we submit the form it'll create a new item it'll get the name from the
form and then save to MongoDB and then redirect and then notice the port is three thousand that's very important because in our docker file we need to we need to know which port that we're gonna
be exposing okay we're also gonna need it for the docker compose file so very simple application if we look at our view this is all it is is a simple form where we can add an item and then it
just loops through the items and shows each one okay very very simple and I wanted the app to be really simple because it's not about the application it's about docker so let's go ahead and
create a docker file ok capital d docker file no dot at the beginning or anything like that and we're gonna be using the nodejs image ok so let's say from node now we could do latest ok we could do
that but if the if node gets updated or if this image gets updated it could mess our application up if there's something in the newer version that that isn't compatible so it's usually a good idea
to use a solid version so right now version 10 is out so I can just simply say no 10 and it'll give us the latest version of all right so I'm going to do that and
then we want to create a working directory okay so basically where our app will live in the container and I'm going to use user slash source slash app and then we want to move the
package.json file and the package lock file while we're at it into our container because we need to install any dependencies if you guys don't know how nodejs works it looks at the
dependencies in this file and then you need to run npm install to actually install them and then it creates this node modules folder so we need to move the packages on JSON file so let's say
copy and I'm going to say package dot jason and we want to copy it to the to the working directory so we can just go like that now if I want to move to move the lock
file as well you can see package – lock Jason I can simply put an asterisk here and that will account for both of these because that's a wild card alright and then once we do that we need to run NPM
install so we can simply use the run command and then say NPM install that will install our dependencies next we want to copy everything else over so I'm going to just do copy dot 2 dot so copy
whatever is here into our container and then we want to expose port 3000 because that's the port that it runs on and then finally we need our command which is in an array form so we want to do NPM and
start because if we look at package Jason I have a an NPM script under start that just runs the app it calls node and then the name of the the main file which is index JSON p.m. start we'll run our
app and that's what this command is going to do right here so this is a very common and very simple docker file for an ojs app so I'm going to save this now we could go ahead and build this but
it's not going to work because it's gonna look for MongoDB and it's not going to be able to find it so we could do things separate like we could manually create a Mongo
container but we don't want to do that because you want this to easily port to another system and docker compose allows us to to do that now in the gist that I have in the description which is this
github gist page here with all these commands if you go down to the bottom there's an example of a very simple docker compose file and this is from Brett Fisher's course and this is
actually for a Jekyll site so he has an image on docker hub called Brett Fisher slash Jekyll serve and this is an example of him running the container he did docker run but we were doing docker
container run on he put opens the ports and he put he creates a volume for the current directory to map to the site folder in the container now you can do this same thing within a composer file
and that's what he's doing here I don't know why I put the version up here a version should be down in the file here but you can see that we have services and he only has one service which is
Jekyll and he defines the image so image same thing he's doing here the volumes so the current directory this dis dot is gonna be mapped to slash site which he's doing right here as well and then ports
he's mapping 80 on his local machine to 4,000 in the container which is the port that Jekyll is running on which he's doing here so this command can be done with just putting this stuff in this
file and simply running docker compose up so that's the idea of what we want to do except ours will be a little more in-depth because we're also using MongoDB so we want a Mongo service as
well so let's go ahead and close up our docker file and let's create did I put that in the models folder I did don't do that guys let's move that out to the root that shouldn't be in models so now
we're going to create a file called docker – compose and it's a yamo file so it's gonna be dot yml and we want to do the first thing we want to do is the version so you could use version two
I'm actually gonna use version 3 which should be the latest and then let's say services ok indentation is very important here so under services you want to make sure that your tabbed over
and then we're gonna put app as the name of this service this doesn't actually matter you'll see web a lot of times or you'll see like node in this case or something like that but we're just going
to use app and then we're gonna specify a container name which I'm gonna call docker – no – Mongo then we want to define restart as always so basically if it fails then we want it to just restart
that's all this is doing now as far as the image it's we want to build from our docker file so we want to just put in build and then dot and that's going to simply look in the current directory for
a docker file and it's going to use that to build the image ok and then let's go ahead and map our ports so when we go on a separate line like this we use a hyphen and then a space and then we'll
put in our port so I'm going to use 80 on our local machine and map to 3,000 in our container because that's what our app runs on is 3000 okay so that's our app service now we want our Mongo
service and you could call this Mongo like I am you could also call it DB you'll see that a lot if it's a database just make sure that these are on the same level okay all your services should
be on the same level and then in here let's define a container name which I'm going to just call Mongo and we want to define an image because we're not building from a docker file for this
particular service like we did here we're actually going to pull the image from docker hub so we're just going to define Mongo and that'll pull it from docker hub let's see next we want the
ports so MongoDB runs on two seven two seven zero one seven so we're just going to map those two seven zero one seven and then we also want to link our app service to our Mongo service ok our app
container to our Mongo container so we're going to define right here link and we're going to say – and just say Mongo is it linked or links I think it's linked let me see if I can find its
links I'm sorry not linked so links to our Mongo container alright good so I think we should be good let's let's save this and then the last file I'm gonna create is a docker ignore file it's
actually dot docker ignore and it's just like a git ignore okay so we want to ignore no – modules and also if there's any if there's any debug logs I'm going to just ignore NPM – debug – log put
those in there and close that up alright so we should be ready to start our application in containers okay so we should have our main docker node Mongo container and our Mongo container and
they should be able to work together and in our index J s that's why I have Mongo right here so this should map to the service name or the container name okay so let's try this out let's go to our
terminal here and let's run docker – compose up okay so it's gonna pull Mongo from docker hub because we don't have that image currently I deleted all my images all my containers it's going to
get node 10 as you can see right here it's going to get that image now okay so that's the first thing it's going to do is just get those images and then it's going to set the working
directory it's gonna copy the package Jason don't worry about that that's fine that's just that's just an audit from NPM Expo copying all the files exposing port 3,000 let's see successfully built
already exists image for service that's fine creating creating Mongo creating docker node Mongo so it's creating our containers and then it's doing a bunch
of MongoDB stuff and then down here it just says MongoDB connected because in our application once it connects I just have a console log out so it's now running now if we go to localhost we
should see our application because we chose port 80 so there it is my node app and if we add an item we already have some stuff here let's just say eggs I'll add it and you can see we got a little
log for Mongo down here choose milk add and there we go fur reloads still gonna stay there because it's inside the date inside the MongoDB database which is running in the
container alright so I'm running this and it doesn't matter if I have node or Mongo installed on this Mac that I'm on I don't have Mongo installed actually I do have node but I don't have Mongo
installed it's running in the container and that's the beauty of this I could port this to anywhere and we're gonna actually bring it to a digitalocean droplet now to tear this down we could
simply first of all stop it we can do a control see it's gonna stop both containers and then and by the way if you wanted to run it in the background you could do docker compose up – Dee
which will run it detached but now I'm going to do docker – compose down and you can see that it's going to remove both the containers it also removes the network that's
something we haven't talked about is docker networks okay you can actually create your own networks of containers and when you do a docker compose you have those you know multiple
services it actually puts them on their own network so we had a network called docker node Mongo default but I just kind of just trashed all everything and I can bring it right back up by doing
docker compose up but I don't want to do it here I actually want to do it on digital ocean so let's go ahead and do that I'm going to close this stuff up now I mean there's there's a few ways we
could get our stuff onto digital ocean I'm gonna use github so I'm going to push all of this stuff to this repository and then I'm going to clone this on github okay so I'm sorry I'm
gonna clone this on our digital ocean server so let's just go ahead and do a git and I'll say get at all I already have a git repository obviously and then we're going to do get commit and let's
say added docker to app okay and then I'm gonna do a git push and if we reload our repository here now we have all of our docker stuff it's awesome alright so now I want to get this application on a
digitalocean droplet so let's go and by the way this isn't going to work anymore obviously we destroyed the containers with the with the compose down but let's go to digitalocean
and i'm gonna put a link in the description for you guys to get $10 free and if you never heard a digital ocean it's it's an incredible cloud hosting service where you create what it called
droplets and they're just like little VPS is little virtual Sur servers and you can do whatever you want install whatever you want on them and then you have an option which I'm going to show
you when you create a droplet to actually have docker pre-installed so once you sign up you know you put your credit card information in when you sign up but it doesn't charge you until you
create a droplet so right now I don't have any droplet so I'm gonna say get started with a droplet and I'm gonna choose Ubuntu and then let's see one-click apps you'll see
that you can actually choose docker to be pre-installed it's easy to install you can just use your package manager or whatever but I'm just going to choose it as a pre installation and then choose a
size I'm going to choose the cheapest one which is 5 bucks a month and go down here I don't care about any of this and I'm gonna add an SSH key just so I don't have to go and to my email and get the
password and all that stuff I'm not gonna really go through like how to create SSH keys I have a crash course called SSH crash course that you can look up where we actually do this we
create SSH keys we add them to digitalocean to github and so on so you can check that out but I'm just going to grab mine by doing a cat command to my home directory dot SSH actually I don't
even know what I have in there let me see let's see so I have an ID RSA do dot pub so that's what I want so I'm going to do cat and let's do dot SSH / ID RSA underscore DOI specifically created that
key for digitalocean but and we want the public okay so I'm just gonna grab from this to this copy that and go and say new SSH key paste that in I'll just say iMac all right and should be good that
you can change the host name of your droplet if you want and I'm going to create it so since I use docker it just kind of named it did put docker in the name of the droplet and this just takes
a couple seconds and it doesn't have to be a digital ocean server you could take this app anywhere on any machine as long as it has docker installed and simply run
docker compose up and it'll set it all up in the containers well I feel like this used to take quicker there we go okay so now we have our IP address here so now what I want to do is login to
this server so I'm going to copy the IP address and let's clear this up unless ssh into that so i'm going to save routes at and then the IP address and if my key works i should be able to login
and it's denied so it got denied i just need to run ssh – add and i need to add the private key so i'm going to do as this h add dot slash home wait what am i doing I want to do home slash dot SSH /
ID underscore RSA underscore do and that should add it okay identity added now I'm going to try to SSH in again and there we go so now I'm in so you can see that this is changed to root at docker
whatever the name of my droplet alright so in here in my home directory I have nothing so I'm going to just clone that application from github so I'm gonna go right here clone I'm gonna make sure I'm
using SSH and let's copy this link here and don't worry if you guys don't know what's going on here don't worry about it the whole point is to show you that I can take this application and put it
anywhere run the docker compose up and then it's going to set it up for us alright so I'm just going to get clone paste that link in okay say yes all right so let's LS wait
what happened here my public like he has denied oh my I need github keys now all right so we're gonna have to generate a key for github to work with the server so I can use SSH so I'm gonna do SSH –
god this is turning into a SSH tutorial key Jen and I guess that's fine no passphrase all right so now I want to get my SSH key from my server so I'm gonna say home dot has a seiche and what
it was it called just ID underscore RSA we want the public key so I'm gonna grab that and go to my github and go to settings go to SSH keys and add a new one and we'll say do server digitalocean
server my password in I think I recently changed my password there we go all right Jesus Christ okay so we have the SSH keys sorry about that guys now let's go back to our terminal and
hopefully we can clone this thing so I'm gonna run the clone again and that should have worked good so now we have the application on our server so docker node Mongo if I CD into docker – no –
Mongo and LS you'll see all of our files including the docker files and now we should just be able to run docker compose up so docker – compose up and it should set everything up for us
just like it did on the Mac on the local machine so it's just pulling the images as we have the images on the local machine but this is a completely different this is a server this is the
digitalocean machine I should've did the dash D to run it in the background but that's fine all right so now it should be running on our digitalocean server so let's grab that IP address let's put it
in here there it is so our node app is running on our digitalocean server in docker containers add some eggs here there we go awesome so I apologize for the little hiccup
with the SSH keys but maybe hopefully it helps someone out that that maybe is confused about that stuff I just generated a key copied it and put it into github same thing with digitalocean
and again I do have an SSH crash course if you just search for that I think it's the first one that comes up all right so that will run we can go ahead and stop it and we can do docker or – compose
down and that will get rid of everything okay you can see it removed the containers and the network and now if I reload it should just do nothing sorry about those dings yeah so you can see
that this is this is no longer working even though it's you can still see it but it's knit it's not a working application all right so I think that that's gonna be it guys for this video I
do want to do more docker stuff I want to show you how to set up other environments like little the lamp stack will do a WordPress one so I want to make this kind of an ongoing thing not
like my next video probably won't be a docker video but I want to have you know maybe maybe one a month or something like that because I want to get more into dev
not just on the channel but for myself DevOps is is one thing that is a little difficult for me you know we all have our areas where it's it's kind of difficult for us to grasp grasp things
and DevOps is definitely one of those areas for me as I as it seems like it is for a lot of developers but that's it guys thanks for watching hopefully you like this if you do please leave it a
like and I will see you next time
In this video we will take a simple Node/Express/Mongo app and dockerize it by adding a Dockerfile and using docker-compose to bundle services together. We will also run our containers on a Digital Ocean droplet.
Sponsor:
DevMountain Bootcamp – https://goo.gl/6q0dEa
Code For This Project:
https://github.com/bradtraversy/docker-node-mongo
$10 off Digital Ocean
https://m.do.co/c/5424d440c63a
Docker Help Gist:
https://gist.github.com/bradtraversy/89fad226dc058a41b596d586022a9bd3
💖 Become a Patron: Show support & get perks!
http://www.patreon.com/traversymedia
Website & Udemy Courses
http://www.traversymedia.com
Follow Traversy Media:
https://www.facebook.com/traversymedia
Posts by traversymedia
https://www.instagram.com/traversymedia