Instructor: In last section,
we saw that the npm install command failed to run
because the package.json file was not found,
and we spoke about why that was.
In this section, we're gonna figure out how we can make sure
that the package.json file is available
before npm install is executed inside of the container.
To do so, we're gonna add one little instruction
to our Docker file.
This is going to be an instruction
that we've not used before.
It's the copy instruction.
And as you might guess,
the copy instruction is used to move files
and folders from our local file system on your machine
to the file system inside of that temporary container
that is created during the build process.
The overall syntax for this is copy,
then a path to the folder on your local file system,
and then the path that you want to copy that stuff to
inside the container.
Right now, I'm showing these commands with a dot slash,
which means the current working directory.
Now, one little technicality here, the path from our folder,
the first argument right here,
is relative to the build context.
And you'll recall that I had said
that the build context is the dot right here
when we execute docker build at the terminal.
So build right here means simpleweb directory
or the current working directory.
So we have set the current build context to be simpleweb.
So then inside the copy command right here,
when I say dot slash,
that means the build context, which is simpleweb.
I know that's a little bit confusing.
It's essentially kind of like two layers of indirection.
Again, we're gonna see a better example
of why we would alter this build context stuff
in the future when we start working
on some more complicated projects.
For right now, all we really need to know is
that if we add this right here to our Docker file,
we're gonna copy everything
from our current working directory into the container.
And that's pretty much it.
So let's flip over to our Docker file and we're gonna add
in this instruction and then try to rebuild our image.
Now we definitely wanna make sure
that the package.json file is available
before we run npm install.
I'm gonna add in the new instruction right above that.
So I'll say copy dot slash, dot slash.
Again, everything from the current working directory
of simpleweb to the current working directory
inside the container.
All right, so I'm gonna save this
and then we're going to try to run the docker build command
again over at our terminal.
So I'm gonna flip back over, and I'll do docker build dot.
We'll run that.
Now between the last section and this one,
I removed my alpine image or the node alpine image.
So I had to re-download it.
But after that, we should see the copy step.
There it is right there.
We'll then see npm install.
Now you will notice a couple of little warnings.
The warnings are totally fine.
If you see a notice and then three warnings in a row,
that's totally fine, no issue.
So we've now successfully installed
all of our npm dependencies.
And then if we keep on scrolling down,
we'll eventually see at the the bottom
that we have correctly generated this new image.
And this is the ID of the new image.
Now remember, it's not really nice
to work with IDs all the time.
Let's try running the docker build command again.
But this time we're going to tag the image.
So I'll say docker build dash t, then my docker ID,
and then the name of this project,
which we are calling simpleweb.
And then I'm gonna make sure that I pass in
the build context on the very end.
You'll notice I did not put on the colon latest
to the tag right there.
It's because, remember, the colon latest tag is
automatically appended if you don't specify it in here.
Now one thing I just wanna remind you about,
please don't forget the dot on the very end.
And once you've got that all on there,
we'll hit Enter to rebuild it.
And now we have tagged that newly built image
as your docker ID slash simpleweb.
So now last thing we're going to do is try to start
out that image and get the node server running.
So I'll do a docker run stephengrider slash simpleweb.
All right, so I very quickly see
that the node index.js command was issued
through the npm start command.
I then see that the server is correctly listening
on port 8080.
So I think that we're ready
to open up a web browser and test this thing out.
So I'm gonna flip on over to my browser.
And to visit that running application,
we should be able to go to local host colon 8080.
But once I do, I get a nasty little error message
that says, this site can't be reached.
Well, we definitely got our image built,
and we are running a container out of it,
but we still are not able to actually visit this port
or see the web application running.
So let's take a quick pause right here and we'll continue
in the next section and figure out what is going wrong.