-: In the last section,
we put together our first Docker Compose file.
It has two separate services,
or essentially types of containers available in it.
The first is called Redis Server
and it uses the image redis.
The second is called Node App, and it uses the Docker file
that we put into our current directory.
So we're now gonna figure out
how we can take this Docker Compose file
and create the two separate containers.
But before we do, one quick thing I wanna mention here.
Remember the entire reason that we started talking
about Docker Compose in the first place?
We had said, oh yeah,
we want to use Docker Compose because it makes networking,
or connecting these containers together,
very straightforward.
But you'll notice that we have put absolutely
no configuration into this file right here
to kind of specify any layer of networking.
So believe it or not, by just defining these two services
inside this file, Docker Compose is going
to automatically create both these containers
on essentially the same network,
and they're gonna have free access to communicate
to each other in any way that they please.
So by just using Docker Compose to create container one
and Docker container number two, the two have free access
to each other and can exchange as much information
as they want without having to exchange
or open up any ports between the two.
So when the two containers are created using Docker Compose,
we don't have to go through
any like port declaration like this.
This port declaration right here is solely to open
up access to our container on our local machine.
We don't have to do any additional steps like this
to connect together the two separate containers.
Now, that might be great for me to say, but in reality,
hey, how do we actually kind of access the Redis server
from our Node.js code?
Well, let's open up our index.js file
and I'll show you how it's done very easily.
Now, back inside of our index.js file.
Remember, we've got that Redis.createClient
call right here, and I had said that we were going
to eventually come back to this and add in a little
bit of information about the location
of the Redis server that we are running.
So to specify where this Redis server is running,
I'm gonna put in a set of curly braces, like so,
and then we're going to specify a host option.
Now, usually if we were not using Docker,
if this was just a traditional node application
without any Docker stuff whatsoever, we would usually put
in some type of address right here, like
https myredisserver.com or something like that.
So usually we would put in some type
of connection URL right here.
But since we are making use of Docker Compose,
back over here, so here's our Docker Compose file,
we can connect to this other container running
the Redis server simply by referring
to it by its name of Redis-server.
So rather than putting in some long form connection URL,
like this right here, we're gonna delete
that and we will replace it with Redis-server.
Now, one thing to be aware of here is that node.js,
ExpressJS, which is the kind of the framework
we're using to actually render and respond to requests,
and Redis itself have no idea what reddest-server means.
Redis is gonna just take that string
and it's gonna kind of use it on good faith and say,
you know what, I'm just going to assume
that this is a meaningful URL.
I'm gonna assume that this is something
like HTTPS://myredisserver or something like that.
And so Redis is just gonna make a good faith,
or this Redis client right here is gonna make
a good faith effort to connect
to this server at this host name.
When the connection request goes out
from this node application, Docker's gonna see it,
it's gonna see that it's trying to access a host
called reddish-server, and it's gonna say,
oh, I know what you're looking for.
You are looking for this other container over here.
The container with the name Redis-server.
And so when our Redis client right here,
tries to connect to the Redis server,
it's going to get automatically redirected over
to this other container running our copy of Redis.
Now, just to be really complete here,
technically when we create the client,
we can also specify a port that the Redis
server is running on.
By default, the port that's always
used with Redis is 6379.
So I'm just gonna add that in there for completion's sake.
But again, it is a default port number.
So now when our express application starts up,
or the node application starts up,
it's gonna try to create a connection to a Redis server.
It's going to reach out looking
for a host name of Redis-server.
Docker is going to see that it's looking
for Redis-server server
and it's gonna redirect that connection
over to this other running container.
So that's what's going to automatically
connect together these two containers
and get them to communicate together in some fashion.
Okay, so that's it.
Let's take another quick pause right here.
We're gonna come back to the next section
and we're gonna figure out how we're going
to actually start all of our different containers
with this Docker Compose file.