-
The traditional 3 tier application starts
with the browser,
-
which is the client.
-
It then has an HTTP server,
-
and then it has a database for storage.
-
Now depending on what you're working
with,
-
it's going to take a long time to
configure this development environment.
-
Now we're going to go ahead and do this
with Docker in a couple of minutes here,
-
maybe more like five minutes,
-
and we will be leveraging images that have
been predefined.
-
On the lefthand side you can see the base
image
-
that simply gives you your operating
system.
-
The middle one is Node, which will be our
HTTP server.
-
Someone has gone through the trouble,
-
in this case, the official creators of
Node,
-
of creating an official Node image.
-
The same thing can be had from the
Mongo image.
-
Now I'm going to go ahead and show you
where they come from,
-
and they come from Docker Hub.
-
And if we go into it and we search for
Node,
-
I'm going to go ahead and enter that.
-
We can see here that there is an official
Node image
-
with over 2,000 stars and
over 10,000,000 pulls.
-
Now, if we do the same thing for Mongo,
we see similar numbers.
-
So obviously these are widely used by the
community
-
to be able to pull in larger Lego blocks
to start building our application.
-
So I'm going to go ahead and move to
the terminal now.
-
But right before I run my build of all
my images,
-
let me go ahead and show you what's in
the file.
-
I've created these.
-
As you can see here, I have a Dockerfile
and a docker-compose.
-
The very first line references that Node
image,
-
that official Node image.
-
It will look for it locally, if it doesn't
have it, it'll pull it down
-
and use that to add the additional
commands.
-
I then, within this fresh new image,
-
you can think of it as a new machine,
-
I'm going to add a working directory.
-
I then install nodemon
-
and I do this because I'm going to be
configuring a Node server application,
-
and every time I make a change, I want it
to stop and reload.
-
I then copy over a package.json
-
that simply has the requirements for
this particular application.
-
I then run npm install so that it loads
whatever packages
-
Node needs in this case for this
application.
-
I then copy over.
-
My server is just simply server.js and I
expose port 3000.
-
Now, my docker-compose brings together
all of the images that I might have.
-
As you see in this case, I am pulling in,
-
and each of these at the very lefthand
side of the file defines a service.
-
So I have db and a web,
-
and those are just names that I chose
myself.
-
What follows is the definition of this
service.
-
I have here an image,
-
which is Mongo, this is referring to that
DockerHub Mongo.
-
It's going to pull it down.
-
And then I'm going to expose a couple
of ports.
-
One of it is for the inside, the other
one's for the outside,
-
and if it crashes, I want it to restart.
-
This is, by the way, the default port.
-
I then do something similar for the web,
-
and this is referring to that Dockerfile.
-
So I'm first going build it.
-
I'm going to bring in that image, as you
saw.
-
And then it's going to run all of those
commands.
-
Once it's done, I'm simply mapping my
local directory system
-
to overlay on the internal one,
-
and I do that simply so I don't have to go
into the container.
-
I map my ports, in this case,
I want it to run on port 3000.
-
I then link to the database so that I can
see it from the Dock,
-
from the Node.js side of things,
-
and then at the very end I simply run a
command,
-
which is nodemon.
-
It's that node monitor npm package
-
that allows me to run a Node application.
-
And it simply reloads it every single time
I do an edit.
-
So that is the definition of my files.