Sending HTTP requests
to outside APIs works,
but what about services
or servers running on our local host machine?
At the moment, this is not working,
which is why I had to comment this code out.
Now I will comment it back in and remove this line here.
And now I wanna make sure that my Dockerize application
is able to talk to my locally installed MongoDB database.
Now, you will only be able to follow along
if you installed MongoDB,
as I showed you a couple of minutes ago.
But you don't need to do that
because this will only be an intermediate step
until we later also put MongoDB into a container.
But for the moment, it's not in a container.
MongoDB is installed and running on my local machine.
So not in a container, but on my local machine.
This Node app on the other hand is in a container.
And we just saw that this code here
won't work just like that.
So how can we make it work?
For that all first stopped my running Node container
so that we can restarted it later.
But that alone, of course won't do the trick.
Just restarting is not enough.
We need to change something about this code
or about the way we start this container.
And for communication between your Dockerize application
and your code in your Docker container
and your local host machine
for this kind of communication,
you really only need to change your code in here.
You don't need to start your container differently.
Instead, there is a special instruction,
a special hint you can give to Docker in your code.
There is a special address you can use for your requests.
You need to replace local hosts with a special domain,
which is understood by Docker
and that's host.docker.internal.
This special domain is recognized by Docker.
It's understood by Docker.
And it's translated to the IP address off your host machine
as seen from inside the Docker container.
And you can use this anywhere where you need a domain,
where you need a URL.
Here, it's for a MongoDB type of request.
But it could also be for an HTTP request,
if you had some web server running on your machine
and your code would need to talk to that.
Here, it's MongoDB though.
Now under the hood,
this will be transformed to this IP address
off your local host machine
as seen from inside this container.
But the great thing is
that you don't have to worry about that.
You just use this special domain
and Docker will do the rest.
So to make this work,
we now need to rebuild this image
since we changed the source code.
And after this image was rebuilt,
we can run our container again,
based on this rebuild updated image.
So I will hit enter here.
And if we now inspect the running containers,
we see that this container is up and running.
And if I try this with Postman
and I send a GET request to local host 3,000 favorites,
I get this favorite,
which I stored a couple of lectures ago.
Because I'm now successfully talking
to this MongoDB database which runs on my host machine,
but I'm talking to it, from inside this Dockerize Node app.
And the data which has stored a couple of lectures ago
is still there,
because this database is at the moment
not running in a container,
but on my host machine.
And therefor all the data of course survives.
And this therefor proofs that this communication
from inside your Dockerize app to your host machine
is also possible with this special domain.
And that's the second type of communication,
from Dockerize app to host machine.
So from the container to host machine.
This works but you need to use host.docker.internal
as a URL, as a domain, so to say.
Then Docker can do all required transformations
and it's able to ensure that this communication
between container and host machine works.