So we've got this Dockerfile.
How can we now turn this into an image
and then into a container ultimately?
Well, we need to open up the terminal
and again here, I will simply use the terminal integrated
into my IDE, which is the default system terminal though.
And here, we can now run docker
but not run but build
because now I don't wanna run a image,
at least not yet
but first of all, I wanna create an image.
I wanna create an image based
on the instructions in this Dockerfile
and that's what the build command does.
It tells Docker to build a new custom image based
on a Dockerfile.
And here we now need to specify the path
where Docker is able to find the Dockerfile
and if we just type a dot here,
we tell Docker that the Dockerfile
will be in the same folder
as we're running this command in.
And since I'm using the integrated terminal,
this is already navigated into this project folder.
Hence the Dockerfile is in that folder.
Hence, if I hit Enter,
this now creates this image.
And you see it executes a couple of steps here.
It executes the FROM command to take the node image.
Sets the working directory.
Copies our code.
Runs npm install.
We can ignore the warnings here.
Then exposes the port.
And recognizes this command you could say.
And at the end, it's done
and it built an image with this name here,
with this ID.
Now, keep in mind, as mentioned in the first course module,
on Windows, the output looks a bit differently.
Here you find your image ID here.
You can also assign custom names to images
but for the moment, we can go with that.
So copy the ID that was generated
and then you can run docker run and use that ID here.
And if you hit Enter,
this will now start this container
and you will see it doesn't finish,
it keeps on running.
The reason for that is that the command we executed here
starts a Node server,
which is also an ongoing process that doesn't finish.
And therefore, the container also keeps on running
because the command that was executed
when the container is started
is a command that doesn't finish.
And hence, the container keeps on going.
But you will notice
that if you visit localhost,
you won't see the website,
even though we exposed the port here
in our Dockerfile.
So why is that not working
and what can we do here?
Well, are almost here.
First of all, let's shut this container down
because clearly, it's not working as intended.
To do that, you can open up a new terminal
and there run docker ps
to see all the processes.
And if you just run docker ps,
without -a, you see only the running processes.
And here we see one container based
on this image we created.
And it's still up and running here
because as I said, it doesn't quit automatically.
Now, we can stop this container manually
by running docker stop
and then using the container name,
which was automatically assigned here.
If I now hit Enter,
this takes a short while
and it will shut down this container
and therefore also the Node server running inside
of the container.
Once this is finished,
if you run docket ps again,
you see no running container anymore.
Instead now you would have to enter ps -a
to see your container there.
It now exited.
Okay, so now we shut this down.
What went wrong though?
Why were we not able to listen
on port 80 for this custom container?
Because one step is missing here.
Yes, we have this EXPOSE 80 instruction in our Dockerfile
but actually, this instruction only is added
for documentation purposes.
It doesn't really do anything.
It is a best practice to add it
and you should add it to clearly document
which ports will be exposed by your container
but you need to do more.
You could actually also remove this EXPOSE 80 instruction
and still do what I'm about to show you
and it would still work.
So this instruction is really 100% optional,
yet as mentioned, recommended that you add it.
But what really maters is that when you run the container
with docker run that you then add a special option.
So therefore, what we need to do
is we need to run this container
but we need to add a extra flag here
and that's the -p flag in front of the image name,
which stands for publish.
And this allows us to tell Docker
under which local port,
so under which port on our machine here
this internal Docker container-specific port
should be accessible.
And the syntax here is as follows.
You have -p
and then you specify your local port
under which you wanna access this application.
In this case, for example, 3000.
This is up to you.
And then colon
and then the internal Docker container exposed port.
In this case, 80,
since we're exposing 80 in this container.
And with this now if we hit Enter,
this again starts
but now if I reload localhost:3000,
I see my application and I can Learn Docker in-depth
because now this is published
under the local port 3000.
And this is now our first custom image based
on the default Node image
with our own instructions
and our own Node app.
And I'm pretty sure that it at this point,
there still are many question marks in your mind
and in your eyes
and I will get to all of them
but I hope this general idea here is clear.
That we have a custom image based
on a existing image,
the node image, where we then have a couple of instructions.
For example, to copy our code
and install all dependencies.
And then, we create this image
with the docker build command
and we then run a container based on that created image
with the docker run command.
And then here again,
this container is still up and running.
If we wanna close it,
if we wanna stop it,
we again have to first of all, find the container name
with docker ps here.
And then we can run docker stop container name.
And of course, there are also ways
of assigning your own names
and so on and we'll get to these ways
and these features as well
but for now, these are the basics you should be aware of.
How to build your own image,
how to run that image as a container,
how to stop that container
and most importantly,
you should understand how these core concepts work together.