Before we go ahead and build and push up our Docker containers to Google Cloud, let's go ahead and
refactor our Docker files to make them a little bit lighter and more productionized.
Currently right now, if we go into our reservation's Docker file, you can see that we're copying over
the entire project in one go, which means we're going to be copying over all of our other microservices
into the build when in fact we only need to copy over the reservations folder for the reservation service
and the respective folder for each of these services.
So let's go ahead and refactor this.
We're going to copy the apps slash reservations folder into app slash reservations into the Docker container,
and then we're going to go ahead and copy the Libs folder over to the Libs directory because of course
we're going to need the common directory we have in here with all of our common code.
We'll also need to make sure that we copy over the tsconfig.json into our Docker file as well.
So when we build it, we know how to apply our TypeScript settings.
Let's CD into our apps folder and then into the reservations folder.
I'll go ahead and Docker build, give this a tag of reservations, specify the file is at the current
directory and we want our context of where to actually build this from to be at the root of the project.
So let's go ahead and let this finish building.
Okay, So we can see we have an error being thrown here from the current user decorator.
If we go into the current user dot decorator, you can see we're in our common folder, but we're trying
to import code that lives in a different microservice.
In this case, the auth microservice.
We definitely don't want to be importing from this microservice directly because that means we're going
to have to couple all of these services together.
We want to keep them nice and separated.
So just like we did for our DTO, let's go ahead and create a new Common Models folder here and I'll
go ahead and create an index here as well.
Now let's go ahead and copy over the existing user dot schema that lives in the auth folder.
We'll go ahead and bring this down into the Common Models folder now.
So now that we have this user schema, let's go ahead and export it.
Let's go ahead and export everything here from user dot schema and then in the index dot ts, make sure
we export everything from the models folder here.
Now let's go ahead and copy the user document here and find everywhere that we're currently referencing
it.
Firstly, in the auth controller here, you can see we're trying to import it directly from the common
folder.
Let's change this.
To use the app slash common import as we know is the better way to do this because we can always change
this out later.
Next, go to the auth dot service and we will do the same exact thing here.
We'll import the user document and change the import to app slash common next, go into the user controller
and do the same thing for the user document here.
Next in the users module, we'll go ahead and update and import the user document as well as the user
schema.
Next in the users repository.
Go ahead and change this to be from app slash common the user document.
Now let's go back into our reservations and go ahead and try to rebuild this image.
And we can see we have now finished building our image and now our image is only going to contain the
reservations app and nothing else.
Okay.
So let's go ahead and repeat the same process for the rest of our services.
I'll go ahead and copy our refactor Docker file lines here.
Let's go ahead and start off with the auth service.
So go into the Docker file here and instead of copying over all of the projects, we'll only copy over
the auth project or the auth app now as well as the common Libs folder.
Also make sure we copy over the tsconfig.json here.
Then in the terminal we will go into CD app slash auth and run Docker build tag auth, specify the file
at the root path here and then go up to directories to the base of our project.
So you can see we have an error now in the auth controller spec because of some old test code we have
lying around.
So make sure we get rid of this by deleting the auth controller dot spec.
Okay.
So now we've successfully built the auth service.
Let's go ahead and continue next with the notifications.
We'll go ahead and follow the same process here, make sure we copy the TS config and then copy over
just the specific service we need in this case, app slash notifications.
And now in the terminal, we will just cd into the notifications project and run the same command.
Just changing the tag out here for notifications.
You can see we have the same error about the notifications controller spec, so make sure we delete
this as well before going ahead and rebuilding.
So our last service here will then be the payment service.
Let's go ahead and copy this over here and make sure we now copy the payments app into this app and
also copy over the TS config here.
And of course make sure before we build, we get rid of the payments controller spec here and go ahead
and run Docker, build dash T payments for the payments tag.
Okay.
So now we have all of our microservices built using Docker and we are now copying over just the code
we need to run each individual service.
However, one existing improvement we can make is you can see right now for each Docker file, we're
copying over the same exact package.json at the root of the project, which means we're sharing all
dependencies needed for each service amongst all the different services, which is not really great
because some of these dependencies are the services don't even need.
For example, any authentication related dependencies here like Bcrypt or Nestjs passport would only
be needed from the auth service and not anything else.
We can go ahead and improve this by creating separate package.json for each service, copying those
in to just that service and making sure that our dependencies are only used for the services that need
them.
Let's go ahead and do just that.
First, we'll start off by going into the terminal and seeding into the auth folder and then we can
run npm init here to generate a new package.
I'm going to go ahead and call this sleeper slash auth here as our package name and just go through
the default setup here.
Now you can see in the auth folder here we have a new Package.json and this is great because now we
can add dependencies just for the auth service here.
So let's go ahead and start pulling out the dependencies that only auth will need from our root Package.json.
The first one we can see here will be the Nestjs JWT package.
We'll go ahead and create a new dependencies object here and let's go ahead and add Nestjs JWT and remove
it from this one.
We'll also go ahead and get rid of Nestjs passport and add this in too just off.
And of course you can see we'll keep the common packages here that all of our services will need or
the libs common folder will use, but just the dependencies that only individual services will use,
we will be moving.
So let's keep on going here.
We can move Bcrypt js and move this into auth as well.
Next we'll copy out all of the passport packages and move them directly into the auth Package.json.
Next we'll move on to the dev dependencies here, so I'll go ahead and take out the types for Bcrypt
and now add them to a new dev dependency object here and I'll paste in the bcrypt types.
Next we'll take out the types for all passport packages here and move them into the auth dev dependencies.
Okay.
So now that we have a separate package.json in the auth service, we need to tell our Docker file to
make sure we go into this directory and install all the dependencies in this directory as well.
So after we copy the apps, auth microservice over you of course.
See we've already installed the dependencies from the root package.json, but we're going to go ahead
and then run CD into apps, slash auth and run NPM install again to get the dependencies just for auth
here.
Now in the terminal, if we go ahead and build, we should see that we get a new Docker image built
with these dependencies.
Just for the auth service, let's go ahead and continue this same process of separating out our dependencies
for the notification service where we can pull out things like node mailer.
So we'll follow the same process, we'll go CD into the notifications folder and then I'm going to run
NPM init and I'll call this at Sleeper slash notifications.
I'll go ahead and continue to all of these default settings.
So now we can see we have our notifications package.json and let's go ahead and add a new dependencies
object here and pull out the ones from the root Package.json We're going to go ahead and pull off Node
Mailer and add it as part of this dependencies list.
Next, we'll also pull off the types here off the dev dependencies and go ahead and add it to the dev
dependencies section.
We'll go ahead and copy this line from the auth docker file where we CD into the directory and NPM install
and add it to the notifications.
Her file now.
So we'll just CD into apps, slash notifications here and run NPM install.
So now we can run Docker, build and change the tag here to be notifications.
Okay.
And lastly, the only other service we're going to create a new package.json for, for its own dependencies
is going to be the payment service and that's because of Stripe here.
So let's go ahead and follow this last pattern here by seeding into the payments directory and we'll
run NPM init here and I'll give it a name of sleeper slash payments and continue through the setup here.
So we generate a package.json and payments.
Now let's go ahead and create a new dependencies here.
And we'll go ahead and copy over Stripe, paste that in and make sure we get rid of this comma here.
So let's go into the notifications Docker file, copy this line and add it into the payments Docker
file.
Right before we build it, we'll CD into app slash payments here and run NPM install to make sure we
get the service specific dependencies.
We'll now go ahead and run Docker build dash T payments for the payments tag.
Okay, so Payments has finally finished building and now we have all of our services with a refactored
productionized Docker file to make it a bit more lean and ensure we only have the dependencies we need
for each individual service and they will all share these common dependencies.
We're now ready to push our images up to gcloud.
Let's go ahead and click on our first service here, the reservation service, and click the copy button
up here where we get the path to the repository.
Let's go back into the route here.
Let's go ahead and tag our images so that we can push them up to Gcloud.
I'll go ahead and start with reservations by running Docker tag reservations and then pasting in the
Gcloud link here.
However, we need to also provide the name of the image we're pushing up.
So I'm going to call this the production image.
Then we'll simply call Docker, push and paste in the full URL to the repository.
Let's go ahead and repeat the same process for auth.
I'll go ahead and swap out reservations for auth here so that we tag the correct auth image.
Then we'll go ahead and push this up to slash auth on the production image.
Let's go ahead and continue the same process.
Next for the payments image.
I'll go ahead and swap out auth for payments here.
Then we'll go ahead and push up to payments.
And we'll go ahead and complete the process one more time for the notifications image now.
Go ahead and now push up to notifications production.
We should be able to go and look at every repository here and see that our production image is pushed
up properly.
Now that we have all of our images and our Docker file is Productionized, let's go ahead and see how
we can actually start deploying this on machines.