So let's get started.
We get these three building blocks the database,
the backend API node server
and this frontend react application.
Now, I will start with the MongoDB database.
For that I shut down my locally installed MongoDB database,
which was running off screen.
And I will open a new terminal here.
I still have my node and my react application running.
So that still works.
Node and react is still running.
But now I opened a new terminal
where I wanna spin up a container for MongoDB
to which we then can talk from our backend node API here.
And for this,
I'm going to use the official Mongo image again
which you find on Docker Hub.
And I already mentioned that we learn how we can use it
in the documentation here.
Now we can start by simply running docker run mongo.
This image is available
since it's an official Docker Hub registered image.
And this would spin up a container based on this image.
Now I will give my container a name, mongodb maybe.
And I will also ensure
that it's removed automatically if it's stopped.
And I will run it in detached mode.
Now, that's the most basic form
of starting this MongoDB container.
And that's essentially what we also did in the last module.
There, we also added a network.
But I'll come back to that later.
Now, this would start this database.
But especially if my backend here
and my frontend is not dockerized yet,
I wanna do one additional thing here.
I wanna publish one of the ports made available
by this Mongo image.
Because as long as the backend API is not dockerized,
this API, this node API,
still talks to the database here when it connects
as if it would be running on my local host machine.
Now if we spin up a Docker container
containing the database,
for this to work, I need to expose this port 27017
from my Docker container to my local machine.
So that our services can connect to it
through my local machine.
And that's something we can do on this Mongo image.
Actually, if you search for 27017
which is the default MongoDB port,
you'll see that this image actually in the end
exposes this port.
So what we can do without any issues here,
is we can expose this port on the same port
on our local host machine.
And with that,
we can ensure that MongoDB runs in a container.
But our locally running backend,
which currently is not dockerized yet,
will still be able to talk to it.
So that's the command I wanna run.
To ensure that it works,
I'll first of all run docker container prune
to remove all stopped containers
to ensure I don't have any old MongoDB containers
lying around.
And thereafter, I'll run this command.
And with docker ps,
we there after see this container to be up and running.
And now I will go back to my other terminal
where the node API is running.
With control+ C, we can stop this or I can stop this here.
You probably don't have it up and running.
But I can stop this here.
We're soon going to dockerize it as well.
And I can then restart it with node app.js.
And it successfully connects to MongoDB,
even though I shut down my locally installed MongoDB.
And that means that indeed, it must have connected
to this MongoDB container we started here.
And we can have a look there
by looking into the logs of this MongoDB container.
And this is quite hard to read here for sure.
But actually, if we read through this,
well, huge amount of lines here, we see that something
based on the (indistinct) has connected here.
And that's indeed my local node application.
So that's working. We now dockerized does MongoDB database.