WEBVTT

00:00.110 --> 00:05.870
Before we go ahead and build and push up our Docker containers to Google Cloud, let's go ahead and

00:05.870 --> 00:11.060
refactor our Docker files to make them a little bit lighter and more productionized.

00:11.090 --> 00:16.820
Currently right now, if we go into our reservation's Docker file, you can see that we're copying over

00:16.820 --> 00:23.090
the entire project in one go, which means we're going to be copying over all of our other microservices

00:23.090 --> 00:29.150
into the build when in fact we only need to copy over the reservations folder for the reservation service

00:29.150 --> 00:32.320
and the respective folder for each of these services.

00:32.330 --> 00:33.940
So let's go ahead and refactor this.

00:33.950 --> 00:42.260
We're going to copy the apps slash reservations folder into app slash reservations into the Docker container,

00:42.260 --> 00:47.120
and then we're going to go ahead and copy the Libs folder over to the Libs directory because of course

00:47.120 --> 00:51.520
we're going to need the common directory we have in here with all of our common code.

00:51.530 --> 00:59.270
We'll also need to make sure that we copy over the tsconfig.json into our Docker file as well.

00:59.270 --> 01:03.630
So when we build it, we know how to apply our TypeScript settings.

01:03.630 --> 01:07.590
Let's CD into our apps folder and then into the reservations folder.

01:07.590 --> 01:14.340
I'll go ahead and Docker build, give this a tag of reservations, specify the file is at the current

01:14.340 --> 01:20.220
directory and we want our context of where to actually build this from to be at the root of the project.

01:20.220 --> 01:22.710
So let's go ahead and let this finish building.

01:22.710 --> 01:27.540
Okay, So we can see we have an error being thrown here from the current user decorator.

01:27.540 --> 01:33.120
If we go into the current user dot decorator, you can see we're in our common folder, but we're trying

01:33.120 --> 01:38.250
to import code that lives in a different microservice.

01:38.280 --> 01:40.620
In this case, the auth microservice.

01:40.650 --> 01:46.560
We definitely don't want to be importing from this microservice directly because that means we're going

01:46.560 --> 01:49.230
to have to couple all of these services together.

01:49.230 --> 01:51.300
We want to keep them nice and separated.

01:51.300 --> 01:57.510
So just like we did for our DTO, let's go ahead and create a new Common Models folder here and I'll

01:57.510 --> 02:00.990
go ahead and create an index here as well.

02:00.990 --> 02:09.210
Now let's go ahead and copy over the existing user dot schema that lives in the auth folder.

02:09.210 --> 02:14.190
We'll go ahead and bring this down into the Common Models folder now.

02:14.190 --> 02:17.880
So now that we have this user schema, let's go ahead and export it.

02:17.880 --> 02:25.410
Let's go ahead and export everything here from user dot schema and then in the index dot ts, make sure

02:25.410 --> 02:29.820
we export everything from the models folder here.

02:29.970 --> 02:35.550
Now let's go ahead and copy the user document here and find everywhere that we're currently referencing

02:35.550 --> 02:35.970
it.

02:35.970 --> 02:40.770
Firstly, in the auth controller here, you can see we're trying to import it directly from the common

02:40.770 --> 02:41.370
folder.

02:41.370 --> 02:42.270
Let's change this.

02:42.270 --> 02:49.560
To use the app slash common import as we know is the better way to do this because we can always change

02:49.560 --> 02:50.580
this out later.

02:50.580 --> 02:55.080
Next, go to the auth dot service and we will do the same exact thing here.

02:55.080 --> 03:03.600
We'll import the user document and change the import to app slash common next, go into the user controller

03:03.600 --> 03:06.570
and do the same thing for the user document here.

03:06.570 --> 03:12.510
Next in the users module, we'll go ahead and update and import the user document as well as the user

03:12.510 --> 03:13.080
schema.

03:13.080 --> 03:15.030
Next in the users repository.

03:15.060 --> 03:19.680
Go ahead and change this to be from app slash common the user document.

03:20.010 --> 03:25.350
Now let's go back into our reservations and go ahead and try to rebuild this image.

03:25.350 --> 03:31.230
And we can see we have now finished building our image and now our image is only going to contain the

03:31.230 --> 03:33.590
reservations app and nothing else.

03:33.600 --> 03:33.840
Okay.

03:33.840 --> 03:37.800
So let's go ahead and repeat the same process for the rest of our services.

03:37.800 --> 03:41.910
I'll go ahead and copy our refactor Docker file lines here.

03:41.980 --> 03:44.520
Let's go ahead and start off with the auth service.

03:44.520 --> 03:50.490
So go into the Docker file here and instead of copying over all of the projects, we'll only copy over

03:50.490 --> 03:56.880
the auth project or the auth app now as well as the common Libs folder.

03:57.000 --> 04:01.680
Also make sure we copy over the tsconfig.json here.

04:01.690 --> 04:10.350
Then in the terminal we will go into CD app slash auth and run Docker build tag auth, specify the file

04:10.350 --> 04:15.300
at the root path here and then go up to directories to the base of our project.

04:15.300 --> 04:21.060
So you can see we have an error now in the auth controller spec because of some old test code we have

04:21.060 --> 04:21.930
lying around.

04:21.930 --> 04:26.940
So make sure we get rid of this by deleting the auth controller dot spec.

04:27.270 --> 04:27.630
Okay.

04:27.630 --> 04:30.060
So now we've successfully built the auth service.

04:30.060 --> 04:33.510
Let's go ahead and continue next with the notifications.

04:33.510 --> 04:39.690
We'll go ahead and follow the same process here, make sure we copy the TS config and then copy over

04:39.690 --> 04:45.600
just the specific service we need in this case, app slash notifications.

04:49.800 --> 04:55.470
And now in the terminal, we will just cd into the notifications project and run the same command.

04:55.470 --> 04:59.670
Just changing the tag out here for notifications.

04:59.790 --> 05:04.800
You can see we have the same error about the notifications controller spec, so make sure we delete

05:04.800 --> 05:08.760
this as well before going ahead and rebuilding.

05:08.760 --> 05:12.420
So our last service here will then be the payment service.

05:12.450 --> 05:21.360
Let's go ahead and copy this over here and make sure we now copy the payments app into this app and

05:21.360 --> 05:24.450
also copy over the TS config here.

05:24.450 --> 05:30.630
And of course make sure before we build, we get rid of the payments controller spec here and go ahead

05:30.630 --> 05:35.430
and run Docker, build dash T payments for the payments tag.

05:35.730 --> 05:36.150
Okay.

05:36.150 --> 05:42.630
So now we have all of our microservices built using Docker and we are now copying over just the code

05:42.630 --> 05:44.820
we need to run each individual service.

05:44.820 --> 05:50.440
However, one existing improvement we can make is you can see right now for each Docker file, we're

05:50.440 --> 05:57.130
copying over the same exact package.json at the root of the project, which means we're sharing all

05:57.160 --> 06:02.470
dependencies needed for each service amongst all the different services, which is not really great

06:02.470 --> 06:05.950
because some of these dependencies are the services don't even need.

06:05.950 --> 06:13.930
For example, any authentication related dependencies here like Bcrypt or Nestjs passport would only

06:13.930 --> 06:17.020
be needed from the auth service and not anything else.

06:17.050 --> 06:23.500
We can go ahead and improve this by creating separate package.json for each service, copying those

06:23.500 --> 06:29.410
in to just that service and making sure that our dependencies are only used for the services that need

06:29.410 --> 06:29.980
them.

06:30.160 --> 06:32.410
Let's go ahead and do just that.

06:32.410 --> 06:37.450
First, we'll start off by going into the terminal and seeding into the auth folder and then we can

06:37.450 --> 06:41.800
run npm init here to generate a new package.

06:41.800 --> 06:48.670
I'm going to go ahead and call this sleeper slash auth here as our package name and just go through

06:48.670 --> 06:51.420
the default setup here.

06:51.430 --> 06:58.210
Now you can see in the auth folder here we have a new Package.json and this is great because now we

06:58.210 --> 07:02.830
can add dependencies just for the auth service here.

07:02.830 --> 07:09.880
So let's go ahead and start pulling out the dependencies that only auth will need from our root Package.json.

07:09.910 --> 07:14.050
The first one we can see here will be the Nestjs JWT package.

07:14.050 --> 07:22.480
We'll go ahead and create a new dependencies object here and let's go ahead and add Nestjs JWT and remove

07:22.480 --> 07:23.620
it from this one.

07:23.680 --> 07:28.660
We'll also go ahead and get rid of Nestjs passport and add this in too just off.

07:28.660 --> 07:33.790
And of course you can see we'll keep the common packages here that all of our services will need or

07:33.790 --> 07:39.310
the libs common folder will use, but just the dependencies that only individual services will use,

07:39.340 --> 07:40.420
we will be moving.

07:40.420 --> 07:42.190
So let's keep on going here.

07:42.190 --> 07:46.740
We can move Bcrypt js and move this into auth as well.

07:46.750 --> 07:54.300
Next we'll copy out all of the passport packages and move them directly into the auth Package.json.

07:54.310 --> 07:59.710
Next we'll move on to the dev dependencies here, so I'll go ahead and take out the types for Bcrypt

07:59.710 --> 08:07.240
and now add them to a new dev dependency object here and I'll paste in the bcrypt types.

08:07.270 --> 08:14.630
Next we'll take out the types for all passport packages here and move them into the auth dev dependencies.

08:14.650 --> 08:14.920
Okay.

08:14.920 --> 08:20.410
So now that we have a separate package.json in the auth service, we need to tell our Docker file to

08:20.410 --> 08:25.470
make sure we go into this directory and install all the dependencies in this directory as well.

08:25.480 --> 08:30.280
So after we copy the apps, auth microservice over you of course.

08:30.310 --> 08:35.590
See we've already installed the dependencies from the root package.json, but we're going to go ahead

08:35.590 --> 08:45.940
and then run CD into apps, slash auth and run NPM install again to get the dependencies just for auth

08:45.940 --> 08:46.380
here.

08:46.390 --> 08:51.730
Now in the terminal, if we go ahead and build, we should see that we get a new Docker image built

08:51.730 --> 08:52.990
with these dependencies.

08:52.990 --> 08:58.720
Just for the auth service, let's go ahead and continue this same process of separating out our dependencies

08:58.720 --> 09:02.950
for the notification service where we can pull out things like node mailer.

09:02.950 --> 09:08.920
So we'll follow the same process, we'll go CD into the notifications folder and then I'm going to run

09:09.220 --> 09:14.260
NPM init and I'll call this at Sleeper slash notifications.

09:14.260 --> 09:17.350
I'll go ahead and continue to all of these default settings.

09:17.350 --> 09:23.680
So now we can see we have our notifications package.json and let's go ahead and add a new dependencies

09:23.710 --> 09:29.950
object here and pull out the ones from the root Package.json We're going to go ahead and pull off Node

09:29.950 --> 09:33.160
Mailer and add it as part of this dependencies list.

09:33.190 --> 09:38.920
Next, we'll also pull off the types here off the dev dependencies and go ahead and add it to the dev

09:38.920 --> 09:40.220
dependencies section.

09:40.270 --> 09:47.470
We'll go ahead and copy this line from the auth docker file where we CD into the directory and NPM install

09:47.470 --> 09:49.180
and add it to the notifications.

09:49.360 --> 09:50.230
Her file now.

09:50.230 --> 09:55.800
So we'll just CD into apps, slash notifications here and run NPM install.

09:55.810 --> 10:00.910
So now we can run Docker, build and change the tag here to be notifications.

10:04.490 --> 10:04.820
Okay.

10:04.820 --> 10:10.850
And lastly, the only other service we're going to create a new package.json for, for its own dependencies

10:10.850 --> 10:14.780
is going to be the payment service and that's because of Stripe here.

10:14.780 --> 10:21.470
So let's go ahead and follow this last pattern here by seeding into the payments directory and we'll

10:21.470 --> 10:29.390
run NPM init here and I'll give it a name of sleeper slash payments and continue through the setup here.

10:29.390 --> 10:32.480
So we generate a package.json and payments.

10:32.480 --> 10:37.790
Now let's go ahead and create a new dependencies here.

10:40.090 --> 10:46.560
And we'll go ahead and copy over Stripe, paste that in and make sure we get rid of this comma here.

10:46.570 --> 10:53.200
So let's go into the notifications Docker file, copy this line and add it into the payments Docker

10:53.200 --> 10:53.560
file.

10:53.560 --> 10:59.890
Right before we build it, we'll CD into app slash payments here and run NPM install to make sure we

10:59.890 --> 11:02.800
get the service specific dependencies.

11:02.800 --> 11:08.830
We'll now go ahead and run Docker build dash T payments for the payments tag.

11:12.130 --> 11:17.890
Okay, so Payments has finally finished building and now we have all of our services with a refactored

11:17.890 --> 11:23.530
productionized Docker file to make it a bit more lean and ensure we only have the dependencies we need

11:23.530 --> 11:28.180
for each individual service and they will all share these common dependencies.

11:28.210 --> 11:31.010
We're now ready to push our images up to gcloud.

11:31.030 --> 11:36.580
Let's go ahead and click on our first service here, the reservation service, and click the copy button

11:36.580 --> 11:39.890
up here where we get the path to the repository.

11:39.910 --> 11:42.060
Let's go back into the route here.

11:42.070 --> 11:45.700
Let's go ahead and tag our images so that we can push them up to Gcloud.

11:45.730 --> 11:51.610
I'll go ahead and start with reservations by running Docker tag reservations and then pasting in the

11:51.610 --> 11:53.060
Gcloud link here.

11:53.080 --> 11:56.620
However, we need to also provide the name of the image we're pushing up.

11:56.620 --> 11:58.960
So I'm going to call this the production image.

11:58.960 --> 12:04.630
Then we'll simply call Docker, push and paste in the full URL to the repository.

12:05.080 --> 12:08.210
Let's go ahead and repeat the same process for auth.

12:08.230 --> 12:14.060
I'll go ahead and swap out reservations for auth here so that we tag the correct auth image.

12:17.380 --> 12:23.110
Then we'll go ahead and push this up to slash auth on the production image.

12:25.000 --> 12:27.160
Let's go ahead and continue the same process.

12:27.160 --> 12:30.250
Next for the payments image.

12:30.250 --> 12:33.190
I'll go ahead and swap out auth for payments here.

12:33.610 --> 12:36.520
Then we'll go ahead and push up to payments.

12:39.850 --> 12:44.770
And we'll go ahead and complete the process one more time for the notifications image now.

12:48.920 --> 12:52.820
Go ahead and now push up to notifications production.

12:56.990 --> 13:02.630
We should be able to go and look at every repository here and see that our production image is pushed

13:02.630 --> 13:03.670
up properly.

13:03.680 --> 13:08.930
Now that we have all of our images and our Docker file is Productionized, let's go ahead and see how

13:08.930 --> 13:11.690
we can actually start deploying this on machines.
