All right.
This one is going to be fun because we're going to be connecting one application to another.
We have a node API and we have a great submission portal.
And the great submission portal is going to be submitting grades to the node API.
And the node API is going to be storing them for the portal to be able to access them later.
Anyways, um, so here's the setup that we're going to have.
We're going to have a node server that's running and a flask app running as well, each, uh, each
one running in their own respective container.
The container that's running the flask app, it's going to provide the application with an environment
variable.
That environment variable is going to somehow help the application connect to the application inside
of this node server container.
Okay.
So the first thing we're going to do is build an image for the node app, build an image for the flask
app before we can talk about anything else.
So let's just go ahead and do that.
We're going to start by building an image for the node application.
Now let's assume that we know nothing about node.
We're relying on the developer who built this application to tell us how they would normally run the
app bare metal.
So first they're telling us, um, that you need to install the dependencies listed in so package dot
JSON as well as Package-lock.json.
Then they're telling us that once the dependencies are installed, start the API server with node app
dot js.
So very similar to what we would have done with a flask application instead of package dot JSON, it
would have been requirements.txt.
Then instead of node app dot js it would have been python app dot pi.
So again you can probably see, uh things repeating themselves a lot.
So the image that we're going to use in order to run node applications, let's just say node version
14, that should be fine.
Then we're going to set the working directory of uh we're going to set the working directory equal to
slash app.
So everything from here on out is going to happen inside of this working directory.
So here we need to copy package-lock.json as well as package.json.
Notice that the docker file is directly inside of the project itself which means it is in.
It is in the same directory as both package dot JSON files.
So we can just directly say copy package dot JSON into the working directory slash app and copy package
lock dot JSON into the working directory slash app.
You know what?
This is kind of repetitive.
What we can do is say copy package star dot JSON.
So this should include the package dot JSON.
And since star basically means whatever comes after uh, and just before the dot, it should also include
the package dot JSON.
So after we copy over the uh, both package dot JSON files, according to our developer, we can use
this npm install command in order to install the dependencies.
So let's go ahead and run this command during the image building process.
So remember the dependencies are going to be installed inside of the image.
That way once we're ready to create a container to run the app, all the dependencies are already going
to be there and it can just run the app easily Now we're going to say copy the rest of the source code
into.
Copy the rest of the source code inside the current directory into the working directory.
And before we do that, what I want to do is include a dot docker ignore file.
Um, because we're installing the dependencies inside of the image, we don't need to include this node
module so we can remove it, or we can tell Docker to ignore it when building the image.
We definitely don't want to include the docker file, so we specify that in our Docker ignore we don't
want to include the readme.
More on that later.
And that's pretty much it I think now.
Well, we're not ready to do anything yet.
Um our API is going to be running on some port.
Which port seems to be port 3000?
Because we're going to be running this inside of a container, it's going to be serving requests on
port 3000 of the container, which means we'll have to map it to.
Or actually we won't have to map it anywhere because it's only.
Yeah, sure.
We'll we'll map it to a host machine port.
Why not.
And then we provide the container with a command that it can use to execute the API.
So as our dear developer told us, after the dependencies have been installed, we can simply run the
API using app dot js.
So node app dot js and I think we're good.
Seems like I'm already seated into great submission portal.
Let me CD out of it CD into great submission API.
Oops Clear and then docker build t.
Now, before we do anything, what I want you to do is make sure that you're logged in.
Make sure make sure that you're logged in to your Docker Hub account.
So here you can see I am logged in.
It shows my username are slim 087.
So name your image, start with your username and then go slash whatever you want to name it.
In this case I'm going to call it Great Submission API.
And we're going to build an image from the docker file inside the current directory create submission
API.
All right.
And we are good.
Now what I want to do is create a container docker run.
I'm going to call the container node server and I'm going to specify dash dash rm so that the container
is removed as soon as we're done with it.
I'm going to specify a port, um, of 3001.
So whatever requests are served on the container port, 3000 will be mapped to our local machine port
3001.
Um, then we're going to say create a container from the image that we just created, our slim 087 slash
grade submission API.
Am I missing anything?
I think we're good.
Great service is running on port 3000, in this case 3000 of our container, but 3001 of our local hosts
of our local machines is port.
And if I have a look here, we can see that, uh, the node server accepts post requests on slash grades.
It's probably going to be the great submission portal that makes Post requests to this API.
We're going to make a Get request from our browser.
So we're going to say localhost 3001 slash grades.
And we get back an empty array because no grades have been submitted to this node server.
All right.
Not a problem because we need to connect our grade submission portal to this API.
Um we're going to keep this container running.
Press plus and we're going to start on a new terminal.
So now I'm going to CD into Lesson.
Starter projects zero seven Starter Code.
When a CD into great submission portal.
And now let's go ahead and build up this Docker file.
So this is a flask app.
Uh, we've dealt with flask apps before.
Here's what we got to do.
Um, we'll start by creating a Docker file to actually run the grade submission portal.
So from the base image let's say Python 3.8 slim the working directory of the container we'll just set
to slash app install the dependencies inside of the requirements.txt file.
So the docker file is in the same directory as requirements.txt.
So we can just say copy uh requirements.txt inside of the working directory.
And after doing so we can run pip install dash r requirements.txt, installing all the dependencies
during the image building phase.
After the dependencies that the app relies on have been installed inside of the image, we can copy
the remaining source code, but we don't want to copy over the um, the readme file and the docker file.
So how come I already have a docker ignore here?
I must have pushed it by mistake to the to GitHub, and it seems like I've already done that.
Excluded the docker file in the readme.
Okay, cool.
Make sure to do it as well.
Uh, the container port.
This app is going to be serving requests on port 5001.
So it'll be, uh, serving requests on port 5001 of the container.
Okay.
Then we're going to exit.
Then we're going to provide the container with commands that it can use to execute the application.
So you'll remember we can either set a shell environment variable that the flask run command can use
to figure out what the entry point of this flask app is.
Or we can just say Python app dot pi.
Keep it simple.
And I'm saying so now we're going to say Docker build dash t.
Once again start with your username.
In this case mine is our slim 087.
Please make sure to use your username.
That way we can push these onto your Docker Hub repo, which is lots of fun.
We'll build an image from the docker file at the current directory.
And now we're going to create a container docker run uh to do do do dash dash name I'm going to call
the container flask app.
And I'm going to say Dash P.
Um, the app is running on port 5001.
Let's map the container port to a host machine.
Port dash.
You know what?
I don't want to pass any environment variables in yet.
Let's just create a container out of the image 087 Grade Submission Portal.
And don't forget the dash dash r m command or flag which will remove the container once we're done with
it.
Okay, let's go to Local host 5001.
So upon pressing submit, it should make a Post request to the Grade Submission API, which should receive
that request and then send back a response which will allow it to go to the next page.
So we're going to say Harry got a score of 92 on, let's say, potions press submit connection error.
It wasn't able to connect to the API.
So right after the port mapping I'm going to set an environment variable grade service host.
So to give the application running inside the container with an environment variable, specify dash
e followed by the variable that you want to give it the key value pair.
So we're going to give it a grade service host of node server which means now this is going to read
HTTP node server 3000 slash grades okay.
So this should allow it to connect to the container that's running our node application.
Is it going to work?
Let's find out.
Submit a grade for Harry.
Score of 93.
Subject potions didn't work, so it did actually try to do HTTP slash no node server port 3000, but
it wasn't able to connect.
Why not?
Well, here's the thing.
Right now, the containers are completely isolated from each other.
What we need to do is create a network, run them inside the same network, and then from the flask
app, then it's going to be able to connect to the application that's running inside of the node server
container.
Okay.
So what I want to do is stop both containers control Z and I'm going to Create another terminal where
we're going to create a network.
Docker network.
Create my network.
And then we're going to run both containers on the exact same network.
So let me make sure I have no stopped containers.
Docker rm dash f I'm going to remove this container.
All right.
And going back to this command.
Uh, right after we name our app our container flask app, I am going to make sure that this runs on
the network.
My network.
So our great submission portal is running on the My Network.
Network.
Now we need to make sure that the Great Submission API is running on the same network.
Because it's only then that they'll be able to actually talk to each other.
Okay.
So network.
My network.
Beautiful.
Just test it out in the UI.
So I'm going to say Ryan.
Score of 85 subject potions.
Submit.
Everything works beautifully.
Um, yeah.
So you just got to ultimately make sure that they're both running on the exact same network.
And then from this app, you simply http, you set the host equal to the other container that's running
the API, because the application is running on container port 3000.
We say node server port 3000.
And then we'll be able to talk to the application serving requests on that port.
Pretty simple stuff, don't you think?
Uh, so this concludes, uh starter code seven.