All right.
Now we're going to move on to starter code number eight.
Before we do, I want to make note of the fact that our container commands are getting really, really
messy.
We have two different terminals.
Each one of them is running a different process.
And we had to orchestrate each container to run inside of the same network.
And if you think about it, what if we had an application with six different microservices interacting
with each other?
Are we going to start up six different terminals, run them all in the same network, and uh, give
each one environment variables within the command itself?
What if one of the containers had like six different environment environment variables that it needed
to function properly?
You can already see how things are going to get really messy when we do that.
So what we're going to do is set up a Docker compose file that has all of the container definitions
written inside of it.
That way when we call docker compose up.
Docker compose is going to start up all the containers for us.
And when we call.
Docker compose down, docker compose is going to destroy the containers for us and do the cleanup.
And the great thing about Docker compose is that all the containers are going to be running inside of
the same environment, which means they're going to be part of the same network.
So Docker Compose also orchestrates that for us, which means from here I can simply refer to the node
server container name as the host, uh, followed by the port, and therefore be able to connect to
the application that's running inside of it.
All right.
Uh, pretty cool stuff.
Very promising stuff.
Before we do anything, I'm going to remove this terminal.
Remove this terminal, and I'm going to say Docker system Prune dash a I want to delete everything.
Make sure there are no more images.
We still have the great submission API for whatever reason.
Let's just remove it forcefully.
Docker image m.
Say dash f to force it couldn't be forced it because image is being used by a running container.
So we'll say docker PS dash a remove this container first docker rm container id dash f.
Then we'll remove the image.
All right let's start from a clean slate docker images docker PS dash a.
All right let's get started.
Enough talking.
So what I want to do is start by setting a version of three for docker compose.
And the docker compose environment is going to start up a bunch of services.
Every service in Docker compose is going to be a container that's running some sort of application,
some sort of service.
Indenting in a YAML file is really important.
So if I want to specify a service within this field of services I'm going to press enter and then press
tab.
So everything I do here belongs to services.
One of the services is going to be a flask app.
Okay.
So right here I'm specifying that I want to run a container called flask app.
The image from which we're going to create this container.
Again the indentation is really important.
Press enter.
And now the image from which we're going to create this container is going to be the one that we pushed
onto Docker Hub.
I'm going to pull this image 1.0.0.
I'm not going to do latest because what if in the future I pushed an image that was incompatible with
this great submission API?
I want to be specific about the version itself.
All right.
And now the container that we create from this container definition, it's automatically going to be
called flask app.
But I want to specify a container name regardless.
And notice how I've got this autocomplete set up.
That's mainly because I've got the Docker extension installed on VSCode.
Make sure that you do the same.
I'm pretty sure there was a section about that in the very beginning of this course.
All right.
So now I'm going to specify ports.
Now ports unlike these fields accepts an array of fields.
An array in YAML is represented like this.
Um, you press enter, followed by a tab.
Tab, meaning any port we specify here belongs to this field.
One of the ports.
Here we can map the container port to a host machine port.
This is equivalent to the port mapping that we were using in our command.
But the nice thing is, instead of writing command flags, we can specify everything in one file.
We're basically just converting the commands from before into container definitions.
All right.
And you'll remember that the flask application relied on an environment variable that is equal to node
server, which is going to be the name of the container where we run our great submission API.
Okay, That is all the environment variables that the Great Submission Portal relies on.
The next thing we need to do is say depends on.
So what service does this flask app rely on?
It relies on a great submission API service.
We're going to call the container that is hosting that service node server.
So this dictates the order in which Docker compose creates the container.
In this case we're telling Docker compose.
Create the node server container first because the flask app depends on it.
Then create the flask app container.
You'll see that all in action.
All right.
Now we're going to make sure that we're at the same level as the flask app service so that we can create
the node server container service.
All right.
Press tab the image that we're going to use to create this node server container is going to be the
one that we pushed onto Docker Hub.
All right.
And we're going to use image version 1.0.0 because it is the one that's compatible with this portal.
All right.
The container name is going to be Node server.
It's really important that you call the container node server.
Because our flask app container is providing the application inside of it with an environment variable.
Great service host equal to node server.
And now we specify the ports.
The great submission API, if we look at the documentation runs on port 3000 of the container, we can
just map that to port 3000 of our host machine.
So we got the port mapping set up and that's pretty much it.
Pretty easy, don't you think?
If I go ahead, let me go.
Uh, just one second, guys.
Where is that pesky command?
If I go ahead and copy this over here, we basically just recreated this command, but inside of Docker
compose.
So here we've got the port mapping.
Here we've got the image from which we're creating the container.
Here it's just a random output.
We don't need to specify a network anymore okay.
So now what I'm going to do is once again Docker system prune dash a make sure everything is really
gone.
All right.
It did delete the network.
My network.
Anyways.
Now I need to CD out of grade submission API.
I'm at oh seven starter code.
I need to be inside of oh eight starter code, CD, CD out of that CD into oh eight starter code.
Anyways docker compose up.
So notice how it starts by creating the node server.
And then it's going to create the flask application.
So great service.
So the node server starts running on port 3000.
The flask app depends on it.
So it runs second which means now if I go to localhost 5001
everything should work smoothly.
Harry, give him a score of 94 in potions.
Everything works beautifully.
The flask.
The application running inside the flask app container was able to seamlessly interact with the application
running inside the node server container.
Because they're both running inside the same Docker compose environment, we pass an environment variable
of grade service host or whatever equal to node server into the application running inside of the flask
app container.
It was able to make an HTTP request with the host name, node, server and port of 3000.
The node server host name allows it to find the container node server running inside of the same network,
giving it access to the application that's running on port 3000 of the container, and therefore be
able to make requests to it.
And then that was pretty much it.
Now we're going to move on Or before we do anything, we need to remove all of these containers.
And the way we're going to do it is we're not going to stop this process ourselves on a separate terminal.
We're going to say docker compose down.
I'm at the wrong directory, so I need to go inside of oh eight starter code.
Docker compose down.
It removed the containers.
It's stopping the node server currently.
It's taking quite a bit of time to do it, but it is what it is.
As long as it's doing the cleanup for us, I don't care.
It removed both containers.
It removed the network that they were both running on.
So Docker Compose created the network for us.
Isn't that pretty cool?
Ran them both inside the same network and that's all she wrote.
Now let's move on to starter code number nine.