With Docker, you can create so-called container networks,
also called just networks.
So, what are networks then?
Well, the idea is that you might have multiple containers
and you want to allow communication
between these containers.
So, essentially the scenario we have
with the node web API container and the MongoDB container.
Now, with Docker, you can put all these containers
into one and the same network
by adding the --network option on the Docker run command.
This then creates a network in which all containers
are able to talk to Java,
and Docker is then automatically doing this
IP look up and resolving stuff,
which we did manually a couple of minutes ago.
And that's a really useful feature
for having multiple, isolated containers
with their own duties and tasks,
which still are able to talk to Java,
which is exactly what we want here in our example.
So, let's see how we could utilize networks here.
And for that, I'm going to stop the favorites container
and I'm then also going to stop the MongoDB container,
because I wanna restart both, but in a different way.
So, let's stop favorites and let's stop MongoDB,
and let's then explore how we can use networks here.
Let's start with MongoDB.
We started the MongoDB container with this command here,
Docker run -d --name mongodb and then using the Mongo image.
Now, we can add another option here
and that's the --network option,
and here we can then pick any name of our choice.
Let's say, favorites net.
So, favorite-net is my network name.
Now, if I hit enter, this fails to run
because I just stopped the MongoDB container,
I did not remove it,
so, therefore I will actually run Docker container prune
to remove all stopped containers,
so that this MongoDB container is also removed
and thereafter,
we would be able to run a container named MongoDB again,
but still, if I try to run this
with this --network option, if I hit enter,
I get an error that the network favorites net is not found.
Unlike with volumes, for networks,
Docker will not automatically create them for you
if you want to run a container using a network,
instead you have to create them on your own.
So, first of all,
I'm going to remove this newly started MongoDB container,
which then was stopped because we failed to
find that network,
but once that is done, we can create a network
with the Docker network command.
With --help you again see all options,
and the most important option here at the moment
is to create option.
With Docker network create, you create a new Docker network,
and then you don't need any sophisticated configuration,
you just need to pick a name, for example, favorites-net.
That's your custom network name,
and you can simply hit enter thereafter,
and this network gets created and set up by Docker.
It's a Docker internal network,
which you can then use on Docker containers
to let them talk to Java.
All the hard work and heavy lifting
is taken care of by Docker here.
Now, with Docker network LS,
you can inspect all existing networks
and you see your own network here,
as well as a couple of built in default networks,
which you can ignore here,
and with that created, with that network existing,
you can try this Docker run MongoDB command again
with this --network flag here
using that network, favorites net, which we just set up,
and if you now hit enter, this container starts
and it stays up and running,
but it's now part of this favorites net network.
And now any other container
can also be part of that network,
and if two containers or possibly more containers also
are part of the same network, they can talk to Java.
The only remaining question
is how they can talk to Java.
How should this note JS code look like
to ensure that this request works?
This IP address here
might be the IP address of the other MongoDB container,
but I mentioned that we don't want to hard code it in here.
MongoDB should automatically resolve it instead,
and the cool thing is
if two containers are part of the same network,
you can just put the other containers name in here.
So, in my case, MongoDB,
because my other container is named MongoDB here.
I picked this name when I started the MongoDB container,
so, you can then use the name here.
If you picked a different name, use that different name,
if you didn't pick a name, use the auto-generated name
and plug it in here as a domain.
And just as with host.docker internal,
which was translated to your local host IP address
as seen from inside this Docker container,
the other Docker container's name
will also be translated by Docker
to the IP address of that container,
and this automatic transformation will work
if both containers are part of the same network.
So, save this updated code
and then build the image again because the code was updated
and thereafter run the note application again
with Docker run --name
favorites -d --rm,
and then exposing the port
and now very important, when our a flag,
a flag to put this container
into the same network as our MongoDB container is part of.
So, at --network here as well
and then it was the favorites net.
So, this same network we used for the MongoDB container,
both containers have to be part of the same network
in order to talk to Java with this simplification
of just using the other container's name.
If you now hit enter this, it starts
and it stays up and running,
which is always a good sign, proves that it works.
If you inspect the logs
of the favorites
container,
you should see something like this,
which is actually a warning,
which actually proves that it works
because it's just this warning and no connection error.
So, indeed the node application was able to connect
to the MongoDB database running in another container,
and we still see both containers up and running here.
Now with that, if you again tried it with Postman,
I can again, get my favorites
and we see an empty response, an empty aray of favorites,
but a success response, no error and that is what matters,
and we can send a post request to slash favorites
and the data does get stored
until after we can also get it here.
And this now works and proves that both containers
again, are able to communicate,
this time, however, with this simplification
of using the built in network feature,
and that's important.
Two containers normally are not able to talk to Java,
unless you create such a container network
or you manually look up the container IP as you saw,
but with such a container network,
as you see here, it works
and this is therefore how you typically ensure
that multiple containers can talk to Java
because this is a common requirement,
it's not a niche scenario, indeed it is very common
that two or more containers need to talk to Java
and with networks
set up the way you saw it here over the last lectures,
this is a breeze,
and by using the container names here in your code,
it's then especially simple to do as you saw.
And I also wanna point out something else.
When we launch a container like the MongoDB container,
which another container connects to,
in our case, the favorites container
will connect to the MongoDB container,
then as you saw, you don't need to publish any port
when running that to be connected to container.
When I run the MongoDB container,
I don't have the -p option here.
The reason for that is that the -p option is only required
if we plan on connecting to something in that container
from our local host machine
or from outside the container network.
Now, in this case, we don't do that.
The only thing which connects to this MongoDB container
will be our favorites container,
which will be part of the same Docker network,
and in such cases,
when you have a container to container connection,
then you don't need to publish the port
because internally in that container network,
all the containers can freely communicate with each other
and you don't need to expose any ports.
That's really only needed if you wanna connect
to a container from your local host machine
or from outside that container in general.