So to put this into a container,
I will first of all quit my running service here,
which ran locally on my host machine.
I can delete package-lock JSON and Node modules,
because I don't need them locally anymore.
Instead, we're going to dockerize this.
And I already have a Docker file here,
it was also part of the attachment.
It's a straightforward Docker file for dockerizing
this as a Node application,
and it does what we did many times before in this course.
Hence, we can go ahead and just run docker build
and build this Docker file.
And maybe also add a name and a tag,
and I'm going to name it favorites-node.
And I could add a tag,
but I will go with the latest tag
which is automatically added.
So with that, that's built,
this image is built.
And now we can run a container based on it.
So we can use docker run to run a container
on this favorites-node image.
And I'll give this container a name of favorites.
I'll run it in detached mode and remove it
when it shuts down.
We definitely need to publish a port,
because I do work with a port in there with localhost 3000,
or I'm using the 3000 port.
And we could use environment variables for that,
but I'm keeping it simple here, it's hard coded.
And therefore I will expose the 3000 port
on 3000 thousand port of our local machine.
Now I won't add any volume or bind mount here.
We could definitely add our code here as a bind mount,
but I wanna keep this instruction simple here
in the terminal.
And we need no volume because this application
does not write anything to any file
which would need to survive
a container shut down and removal.
There is nothing which needs to survive that
except for the data stored in the database,
but that database is not part of this container.
And I really wanna emphasize this,
this MongoDB database which I'm connecting here
is not part of the container.
Sure, my Node code is part of it,
and therefore, also this code where I try to connect,
but the installation of MongoDB
is not part of this container.
We have no code in the Docker file to install MongoDB
or anything like that.
So therefore, I really wanna emphasize this,
this container only contains the Node app
and all the code that belongs to it,
it does not contain the MongoDB database.
So therefore, if we now run this, let's see what we get.
If I hit Enter, this of course works.
But if I now have a look at my running containers
with docker ps, I see no containers there.
And of course for stopped containers,
we also don't see it,
because we started this container with the --rm flag.
So to see what's wrong, let's again start it
but now in non-detached mode.
Let's start it, and we see this crashes.
And it crashes with an error message
where in the end some connection fails,
and it's a Mongo network error.
So in the end, what fails here is this connection
where I try to connect my Node application
to the MongoDB database.
And that of course is a problem.
It means that connecting to our host machine failed here.
Because MongoDB the database itself
is running on my host machine here.
I installed it before this module started,
so I have MongoDB installed.
And if you follow the installation instructions
on MongoDB.com,
you can of course also follow along,
try this on your own and you will get the same result.
So that is good to know,
out of the box connecting to the host machine like this
with localhost fails.
Now, before we look into fixing this,
I wanna see if sending HTTP requests
to some other website would work.
And in order to test this,
we can copy app listen 3000 here
and move that out of this mongoose connect code
and temporarily comment this mongoose connect code out.
So that for the moment,
we can start this Node container
without connecting to the MongoDB database
just by spinning up the web server.
So I'm going to save this and then since I changed the code,
I'm going to rebuild my image with the same name and tag.
And thereafter I'm going to rerun the container.
And I'll again do that in detached mode now.
So running the container on the updated image.
And with that if we now run docker ps,
we see the container stays up and running.
Of course, because now I'm not trying to connect
to the database anymore and this was the part which failed.
So therefore, of course parts of our application won't work.
To be precise, the favorites endpoints won't work.
But the movies endpoint and the people endpoint,
these endpoints should work fine.
Because there I'm not working with my local database,
I'm instead reaching out to the Star Wars dummy API.
Hence, if I now try sending a get request
to localhost 3000 movies again, this works.
And the same if I send it to localhost 3000 people,
it works just fine.
And this is now using the API running in our container,
which is exposed on port 3000.
So this works, and this has one important implication.
Out of the box,
containers can send requests to the World Wide Web.
You can communicate with web API's and web pages
from inside your dockerized applications.
You don't need any special setup
or any changes to your code, this code just works.
Sending this HTTP request
from inside the dockerized application works.
It works just as it worked without containers
and without Docker.
And that's super important to understand and keep in mind.
Sending requests from inside your container
to the World Wide Web just works.
It works out of the box without any special settings.
So now let's have a look at the other two ways
of communication and understand how we can make those work.