So now that we had a look at two kinds of communication,
there is one last kind of communication,
and that's from container to container.
For that, we first of all need a second container,
because up to this point,
we always had only 1 container working on its own.
Now I want to have 1 container for the note app
and one container for the MongoDB database,
Because as I mentioned earlier, already,
it is a good practice to have every container focus
on just one thing.
So one container for the node Web API,
and one container for the database
and therefore, I will first of all,
stop my running node application container here
with Docker stop favorites,
and once this is stopped,
I want to put my MongoDB database inside of a container.
So I will no longer use the locally installed one,
I could uninstall it
and if you never installed it in the first place,
which you probably didn't,
because I told you that you won't need it,
then you can now follow along because now
is the point of time,
where we're going to set up MongoDB together,
but inside of a container
and that means that we're going
to use two containers instead of just one now.
For that, let's start by creating a MongoDB container
and you could think that we're going
to write another Docker file for that,
but we don't even need to do this,
because if you search for Docker MongoDB,
you will find an official Mongo image
on Docker Hub.
So just as before, with the official node
and Python images, this is an official MongoDB image.
Now you'll find detailed instructions on how
to use it on this Docker Hub page.
That's true for all kinds of images by the way.
But we're going to set it up step by step together anyways,
so you don't need to go through these instructions for now.
Instead, what we're going to do here
is we're going to run a container
with Docker run based on this Mongo image.
Now that is an image which already exists here
on Docker Hub.
So I'm using this official image named Mongo.
If I run Docker run Mongo,
this would create a new container based on
this MongoDB image
and this image will spin up a MongoDB database.
So if I hit Enter here,
you should see some output like this
and this is now the running MongoDB database container.
Now, this is blocking the terminal here,
because I did not run it in detached mode.
Hence, I'll actually quit this
with Ctrl + C,
and then rerun it.
But now with Dash + D to run it in detached mode,
and also with Dash + Dash + name to give it a name
and I'll name it MongoDB.
So with that this is now running,
we can see this running MongoDB container.
So that's now container with
a MongoDB database inside of it.
Now, I want to alter my node js application code such
that we can connect to this database running in
this upper container
and that's now the tricky part,
host.docker internal will no longer work,
because that refers to my local host machine IP address,
not to the IP address of some other container
but instead we can do something interesting,
we can run a number of command
and that's the Docker container inspect command to
inspect this MongoDB container we just started.
Here we get a lot of information about this container,
but the interesting bit of
information can be found if you scroll up a bit,
this IP address here,
which you find under network settings in
this object here, there this IP address.
This is the IP address of your container
and this IP address can be used to contact this container.
So if I copy this IP address, which could be different
on your system,
so make sure you copy your IP address which you see
in your in your terminal when you inspect this container.
If we use this IP address here,
instead of host.docker internal,
then this node application should be able to connect to
this upper container and to the MongoDB database in here.
This port is the default port so
this shouldn't require any changes
and therefore we should now be able to talk to
this our container.
Hence we can build
this image again the favorites node image
because we updated the source code
and we can then try running
this favorites node application container based on
this updated image.
So if I hit Enter on this Docker run command,
let's inspect the running containers
and I see we got containers up and running
the MongoDB container and my node application container.
With that back here in postman,
if I send a request to localhost 3000 favorites,
talking to this dockerized node API,
I get back a response with an empty favorites array.
The fact that it's empty makes a lot of sense,
because this no longer is my MongoDB database
on my host machine
but instead, this is a brand new MongoDB database
in our MongoDB container
and therefore it of course does not have access
to the data stored in my local host MongoDB database.
It's a totally separate, isolated database.
After all, separation and isolation are core concepts
and core reasons for using Docker, right.
So we got no data back because there initially is no data
but and that's super important.
We can communicate here, we don't get an error
and that shows that our approach works,
and that the two containers are able to talk to each other.
I can also try storing a favorite again and that works
and if I thereafter send a GET request to favorites,
that data is there.
So this cross container communication works.
But of course, it's not very convenient.
We had to look up the IP address
of the our container in order to then use it here.
Definitely not a lot of work,
but still quite cumbersome.
It also means that we always have
to build a new image
whenever the MongoDB container IP address changed,
because we hard code that IP address here ,in the node app
and this of course, is not ideal.
So thankfully, there is an easier way
of having multiple Docker containers talk
to each other.