Narrator: In the last section
we put together the core of the node.js web application.
If you didn't follow along
in the last section and you just copy pasted the code
just make sure that you've got your index.js file
and a package.json file.
You'll also want to make sure
that you've got your terminal open inside
of your project directory.
And so if I list out my current files and folders
I should see the index.js and package.json files.
Okay, so we've got some source code here
and now we need to figure out how we can wrap this all
up inside of a Docker container.
I wanna first begin by telling you
a little bit about node.js
and how we're going to start up this application.
So just in case you've never made use of node before
two commands that you need to be aware of
to install dependencies and start up this application.
Anytime that we want to start up a node app
we have to first install a set
of dependencies
and we can install all of our dependencies
by running a command npm install inside
of our project directory.
This starts up a little program called npm
which is somewhat colloquially
known as the node package manager.
This is going to run that program
and install a couple dependencies for us.
And of course it assumes
that npm is installed on our local machine.
We're inside the container as the case may be.
The other thing to be aware of
is that we're going to have to eventually
start our server up and to do so
we're going to run the command
npm start at the terminal as well.
And again, this also kind of assumes
that npm is already installed.
All right, so with all this in mind,
let's now start thinking
about the Docker file that we have to put together.
Now, this series of operations that we are gonna put
into the Docker file is going to end up looking very similar
to what we did in the last application.
So in this somewhat complicated diagram
our template that we're kind of following here
for our Docker file is to specify a base image
run some command to install some dependencies
which definitely sounds like something we have to do.
And then specify the command to run on startup
which as we just discussed is that npm start thing.
Now over when we were working on the Redis image
we said from Alpine to specify the base image.
We ran apk add Redis to install the dependency
and then we set up the default command as Redis server.
And so it really seems like we have
some very direct parallels this time around.
I think that for the node application
we could probably do a Docker file that specifies Alpine
as our base image.
We can then run npm install
to install all of our dependencies,
and then we can set up the default command of npm start to
run the server when the container first comes online.
So we're gonna take this type of approach right here
by creating a Docker file and then essentially
putting almost exactly these instructions inside of it.
Now, quick disclaimer, just quick disclaimer here.
Remember I had said we're gonna do a few things here
slightly wrong, just so you can see some very
common error messages that you are just about
guaranteed to see on your own personal projects.
So maybe in this discussion
or the series of steps that I just described
there might be something in here that doesn't quite
line up with what's reality or what we really have to do.
Okay?
All right, so let's get started.
I'm going to begin by making sure
I've got my code editor open
based on that simple web directory
and inside of here I'm gonna make a new Docker file.
Remember, it's Docker with a capital D
and there is no file extension on there.
Then inside of here
we're going to first specify our base image.
And just to be really complete
I'm gonna add in some comments for these steps
just to remind you what we're doing
every step along the way.
So I'll say specify a base image.
And you know what?
Let's use that Alpine image again.
It really seemed like it worked out last time for us.
Next up, we're going to install some dependencies.
To do so, we will run the command npm install
like I just said, the npm install command is how
we install dependencies on a node.js project.
And then finally, we will set up a default command
which we can do by running out CMD.
And then we put down a set of square brackets.
And then in a set of double quotes,
we separately list out all the parts of the command
that we want to run when the container is first created.
And so to start up our server
we are probably going to want npm start like so.
Notice that I've got a comma between those two strings.
All right, I think this looks pretty good.
It definitely looks very similar to what we did for Redis.
So let's now flip back over to our terminal
and see what happens when we try to build this image.
So I'm gonna list out my files and folders
and verify that I now see the Docker file inside of here.
And then remember to build an image
we can run Docker build
and then we put the dot in there
to specify the build context of the current directory.
So I'll run that and then we very quickly see npm not found.
All right, so first problem
of a couple that we're going to see.
So let's take a quick pause right here.
We're going to start to investigate
why we are seeing this error and fix it up.
So quick break and I'll see you in just a minute.