Instructor: In the last section, we took a first stab
at our docker file to build out this node JS project,
but we very quickly saw
that during the run NPM install step,
we got an error message that said NPM not found.
We're seeing this error message
because during step number two right here,
where we tried to execute the command NPM install
inside of a temporary container,
there is no copy of NPM available.
We're seeing this because we are using alpine
as our base image.
Remember, we had said that we select our base image
based upon the collection of default programs
that we need to successfully build our image.
By default, alpine is a very small image.
It's only about five megabytes large,
and so it has a rather limited set
of default programs included inside of it.
In fact, I kinda like this diagram right here.
What programs are included in the alpine image?
Well, this is supposed to be a tumbleweed of sorts,
not much, so there's not a whole lot
of stuff going on by default inside the alpine image.
You get a couple of very default Linux or Unix programs.
You get that package manager that we used
just a little bit ago, but that's pretty much it.
You don't get a lot of other stuff for free.
So when you are using the alpine image,
and you expect to run some fancy web application
depending upon node JS or depending upon Ruby or Java,
chances are you are gonna have to do some additional fixes
or some additional setup to get this thing working.
Now, to solve this issue, to solve the issue
of NPM not being available inside of our base image,
we have two options.
The first option is to find a different base image.
We can try to find a base image
that already has node and NPM pre-installed inside of it.
Alternatively, we can continue using the alpine image
and run an additional command, like right here,
to attempt to install node JS and NPM inside of our image.
So two approaches.
We can either essentially use someone else's work,
or we can try to build up our own image from scratch.
In our case, we're going to try using someone else's image.
We're gonna find an image that has already been configured
to have NPM pre-installed inside of it.
So to get started, I'm gonna flip back over to my terminal,
and we're going to navigate to Docker Hub,
which you'll recall is a repository of public images
that we can easily pull into our build process.
So I'm gonna open up a new tab
and navigate to hub.docker.com.
And then on the top bar, I'm gonna find the explore button.
You'll see a listing
of very popular images immediately appear.
And so of course, right there is alpine.
We're gonna scroll down a little bit,
and a few options down,
you'll see node listed right here.
If you don't see node listed on here,
you can always do a search up here
at the top left-hand side.
And we're essentially just looking
for the repository called Node,
and it should have a little sub-description of official.
So we can click on this,
we'll get a short description right here.
Now the description in this case is not super helpful.
It says, oh yeah, node JS, here's what it is.
But well, what are we really downloading?
Like what is this repository?
This repository is a docker image
that has node pre-installed on it.
So if we want to, we can attempt to build our image
based upon this node one.
And this node image already has node pre-installed,
already has NPM pre-installed.
And so we don't have to worry
about getting more errors like this,
where NPM is not found.
Now before we just go and use node willy-nilly,
I wanna show you a little bit more inside this description.
So you'll see this full description panel right here.
And then first section here is supported tags
and respective docker file links.
So this is a listing of all the different versions
of this image that is available.
You can get different versions of node JS encapsulated
in each of these different tags.
So for example, if I have a application
that requires node version 6.14,
I can find the tag version 6.14 right here.
And then inside of my docker file,
when I specify my base image,
I can say node, and I can specify the version of it
specifically by adding on 6.14.
So this will give me a image
that contains a pre-installed version of node JS,
specifically version 6.14 of node JS.
Now in our case, we don't want version 6.14.
Instead, I want you to scroll around just a little bit,
and you'll eventually see one tag in here called alpine.
You might have to do a command F search
for alpine, and you'll see that well, yeah,
there's a lot of examples on it on here.
So here's alpine right here.
I want you to remember this is a tag.
So this is not the name of the repository.
So when you see alpine right here, it doesn't mean
that we're supposed to use like alpine,
like we were just before.
The first word right here is the name of the repository.
You place a colon, and then you put down a tag name.
And so in this case, when you see alpine right here,
it means that we can optionally select node colon alpine.
So now you might be curious, why would we do this?
Like what is the purpose of alpine being tied onto node?
Well, alpine is a term in the docker world
for an image that is as small and compact as possible.
Many popular repositories are going to offer alpine versions
of their image.
And an alpine version of node in particular means
that you're not going to get a bunch
of additional pre-installed programs.
So the default node installation
might include extra programs like say git,
or a package manager, or I don't know,
some fancy text editing tools inside that image.
When you get the alpine version of an image,
it means you're getting the absolute
most stripped down version of that image possible.
So it's going to essentially have node JS
and pretty much nothing else.
It will have some very basic programs,
like say the ping command, maybe,
you know, you have to look at the actual image
to figure that out.
But it might have the ping command.
You'll have say cat, maybe a simple text editor, LS,
simple programs like that.
But in general, you're getting a very stripped down version
of that image.
So for us, the alpine version works just fine,
'cause essentially, all you and I need is node and NPM,
and NPM is included in this node image right here,
and we need absolutely nothing else.
So we are totally fine using the alpine version
of the node image.
All right, so with all that in mind,
now that we've made the change to the from right here,
we now have specified a base image
that hopefully is going to include NPM inside of it.
And I can tell you, it definitely is.
This is going to have NPM pre-installed,
we're definitely good to go.
So let's now try to rebuild our image
with this change and see if that error message goes away.
First things first, do make sure
that you save the docker file, and then once you save it,
we'll flip back over to the terminal
and do a docker build dot again.
All right, so now you can see
that we're downloading the new node alpine image.
We then get down to step number two right here
where we run NPM install.
But very shortly after that,
we get a series of error messages,
s and the first of which says
no such file or directory package dot json.
So the package dot json is a file that exists
inside of our current working directory,
but it looks like the container is complaining that,
hey, I don't have this file available.
Like, where's the package dot json file?
I need this to know which dependencies I need to install.
So let's take a quick pause right here,
and we're gonna start to investigate
why we're seeing this error message.