-: In the last section,
we spoke about how Docker Compose automatically
sets up some networking between these different services,
or these different types of containers that we define
inside of our Docker composed file.
Now that all this stuff is supposedly set up,
let's figure out how to start up Docker Compose
using this file.
All right, so remember, the entire purpose,
or, well, I should say one of the purposes
of Docker Compose is to kind of make it easier
to run all the different commands that you usually run
with docker run.
So whereas before, we would start up a new instance
of a container using docker run,
and then the name of the image that we wanted
to create the container out of
to create a instance of all the containers,
or all the images or all the services
listed inside of our composed file.
We're going to run the command docker-compose up.
Now, that one's pretty straightforward.
So essentially, instead of saying run, we're gonna say up.
And we don't have to specify an image.
We're not specifying an image,
because Docker Compose is going to look
into our current working directory,
and automatically look for a docker-compose file
inside there.
Now, the thing to be aware of
is that before, we had the two separate Docker commands
to build an image and run it,
so if we wanted to rebuild the image from our Dockerfile,
and then start an instance of it, two separate commands.
But in the Docker Composed world,
if we ever want to rebuild the images that are listed inside
of our Docker composed file,
for example, if we wanted to have Docker Compose
rebuild this container right here,
or the image used for that container,
we write docker compose up --build.
And so this --build right here is what essentially
tells Docker Compose, "Start up our containers again,
but make sure you try to rebuild them ahead
of time so that we get all the latest changes."
Okay, so let's give this a shot.
I'm gonna flip on over to my terminal.
I'm inside of my project directory,
and I can definitely verify
that I have my docker-compose file inside of here.
So I'm going to run docker-compose up.
Now, I want you to take a look
at the very first line right here.
The very first line says,
"Creating network visits_default."
So like I said before, when you just create a new set
of containers or services with Docker Compose,
it's going to automatically make a network for you
that's going to join those different containers together.
After that, we can see that it is creating an image
for our Node application.
And then if we scroll down a little bit more,
we're gonna see a whole bunch of different output down here.
The most important lines to see are,
"Creating visits Node app one."
So that's a single instance of our node-app service.
And then we've got a single instance
of our Redis Server as well.
After that, we'll then start to see all the output
from both of those different services start to appear.
So every line here that's appearing in yellow for me
is output from the Redis Server.
And if I scroll down a little bit more,
I'll eventually see some output from the Node application.
So it looks like based on ready
to accept connections right here,
and listing a Port 8,081 right here,
definitely looks like both of our containers
set up were created successfully.
So let's open up our browser.
We're gonna try to open up our Node application,
and see if the entire app works.
All right, so I'm gonna open up Local Host 4,001.
Remember, we made that kind of last minute change there.
We had hard coded to say Port 8,081,
but at the last minute we decided to change the port used
to 4,001.
So make sure you're go into Local Host, 4,001.
And then sure enough, we see number of visits as zero,
and then we can refresh.
And every time that we refresh the page,
we'll see that number of visits start to increment.
Awesome.
So that's a basic usage of Docker Compose.
We created two separate services,
and Docker Compose automatically made connections
between the two available.
The real key thing to keep in mind here
is that in order to communicate between the two,
we used a host name of the name of that other service,
or that other container that was created,
and that name is specified inside
of our Docker composed file right here.
So anytime you are making use
of maybe some database driver
inside of your web application layer,
anywhere you would normally put in a connection URL,
or connection URI,
you can instead just list the name of that other container,
and the host name will be automatically resolved
for you by Docker.
All right, now, quick break.
There is still one or two quick things
I want to go over on Docker Compose,
so I'll see you in just a minute.