-: In the last section, we spoke about
how we're going to use Docker Compose
to automate some of the commands
that we were previously writing our long form
using Docker CLI.
The big purpose of Docker Compose
is not only to automate some of these long-winded commands,
it's also to make sure that we have the ability
to easily start up multiple Docker containers
at the same time
and connect them together in some automated fashion.
Now to make use of Docker Compose,
we're essentially gonna take the same commands
that we were running before,
like docker build and docker run,
but we're going to kind of encode these commands
into a very special file in our project directory
called docker-compose.yml.
Now, to be really clear,
we're not just going to like copy-paste these into the file.
We're going to write out these commands
more or less in a special syntax inside the YAML file.
Once we create that file,
we'll then feed it into the Docker Compose CLI
and it will be up to the CLI to parse that file
and create all the different containers
with the correct configuration that we specified.
Now, the Docker Compose file
can sometimes be a little bit complicated,
so I'm gonna give you a quick preview
of what we're going to do inside of it.
So here's essentially what we're gonna write into that file.
We're going to first start off by saying,
"Hey, Docker Compose,
we've got a couple containers that we want you to create.
In total, there are going to be two separate containers.
One container is going to be called redis-server,
and we want you to create that container
by using the Redis image
which can be pulled down from Docker Hub.
We want you to also create a second image,
or a second container, called node-app,
and we want that container to be created
using the Dockerfile in the current directory.
Remember, that's the Dockerfile
that you and I just put together two seconds ago,
this one right here.
In addition, after making that container,
we want you to map some ports from the container
to our local machine
so that we can access everything running inside of it."
So that's just a quick little preview
'cause, again, some of the syntax that's gonna go in here
is gonna sometimes look just a little bit crazy.
Let's now flip back over to our code editor
and we're gonna start writing out our Docker Compose file.
Okay, so back over here inside of my root project directory,
I'm gonna make a new file called dacker...
Excuse me, docker-compose.yml.
And then inside of here, we're gonna write out
all that configuration.
The first thing we're going to do
is add in one little required line,
which is gonna be a version, a colon,
and then inside of a set of single quotes, three.
That specifies the version of Docker Compose
that we're trying to use with this formatted file
that we're putting together right now.
After that, we'll then put together
this big block right here.
So we're first going to head it off
with kind of a big section header that says,
"Docker Compose, here's what we want you to do."
So to tell it, "Here's what we want you to do,"
we're gonna write out services and then put a colon down.
Now you're gonna see the term services
start to pop up a lot in the Docker world,
especially as you start working with Docker Compose.
Anytime you see the word service,
it's essentially saying a container.
Now to be precise,
it's not quite saying, like, exactly a container,
it's kind of saying, like, a type of container.
So in this case, by putting down Redis server and Node app,
we are saying that we are defining two services
inside this Docker Compose file,
and both these services take the form
of these two different Docker containers.
Now, the first service that we are going to create
is going to be one that we're going to call redis-server.
I'll then put down a colon after that,
and then inside of here, we're gonna specify the image
that we want Docker Compose to use
to create this service or this container.
So I'm gonna say, "Use the image Redis
to create this service or create this container."
So what you see right here
is essentially accomplishing the same purpose
as these three blocks right here.
It's saying, "We want you to create a set of containers.
This one is gonna be called Redis server
and we want you to create it using the Redis image."
So now we'll do something very similar
for our other type of container that we're going to build.
So notice I have indented a little bit right here,
and we're going to create the Node app service,
so I'll say node-app.
We want this container to be built using the Dockerfile
inside the current directory,
So rather than just specifying an image
like we did right there, I'm gonna say build: .
and that means look in the current directory
for a Dockerfile
and use that to build this image
that's gonna be used for this container.
And then after that, we're gonna specify
all the different ports
that we want to have be opened up on this container.
Now to specify the ports, we're gonna put a dash,
excuse me, ports, like so, and then indent it.
We're gonna put a dash, like so.
Now the reason we're putting a dash right here
is dash in a YAML file is how we specify an array,
so we can technically map many different ports
inside of a single Docker Compose file
for a single service or a single container.
But in our case, we only want to map one set of ports,
so we're going to connect 80, 81 on our local machine
to 8081 inside the container.
Now, remember the syntax here?
The first number is the port on your local machine,
the second is the port inside the container.
So if we wanna stick to the same convention
that we used previously, we could do 4001, like so.
You know what, let's just stick with 4001
just to keep the distinction between these two numbers
a little bit more clear.
Okay, so that's pretty much it.
Now let's take a quick pause right here.
We're gonna come back to the next section
and we're gonna talk a little bit
about how we make use of this Docker Compose file
to actually bring up these two containers.
So, quick break and I'll see you in just a minute.