WEBVTT

00:01.110 --> 00:02.190
Instructor: We're now successfully able

00:02.190 --> 00:04.530
to visit our running web application inside the browser.

00:04.530 --> 00:07.470
But there's one last little thing that I wanna point out.

00:07.470 --> 00:09.750
I'm gonna start my container up using the image

00:09.750 --> 00:11.550
that we've already built, but this time

00:11.550 --> 00:13.950
I'm gonna start up a shell inside the container,

00:13.950 --> 00:17.160
so that we can do a little bit of debugging inside there.

00:17.160 --> 00:18.720
Remember that we can start up a shell

00:18.720 --> 00:21.480
inside of most Alpine-based containers

00:21.480 --> 00:24.423
by running Docker run dash IT,

00:25.260 --> 00:27.030
and I'll put down my container name

00:27.030 --> 00:29.860
of Steven Grider simpleweb

00:30.840 --> 00:33.960
and then I will override the default startup command

00:33.960 --> 00:35.040
with sh.

00:35.040 --> 00:37.560
Remember, this is a reference to the Shell program.

00:37.560 --> 00:38.670
It will start up a shell

00:38.670 --> 00:42.330
that we can type commands into inside the container.

00:42.330 --> 00:43.163
Now you'll notice

00:43.163 --> 00:45.240
that I did not specify a port mapping here.

00:45.240 --> 00:46.080
It's because we are not

00:46.080 --> 00:47.580
going to actually start up the server.

00:47.580 --> 00:49.260
I just want to attempt to look

00:49.260 --> 00:51.933
at the files and folders inside the container.

00:53.010 --> 00:53.843
So I'll run that

00:53.843 --> 00:55.320
and then we'll get a command prompt,

00:55.320 --> 00:58.650
based inside of the root directory of the container.

00:58.650 --> 01:00.300
Now inside of here, I'm gonna print out

01:00.300 --> 01:03.363
all the available files and folders with ls.

01:04.740 --> 01:06.240
So inside of here, you'll notice

01:06.240 --> 01:08.490
that when we did that copy operation,

01:08.490 --> 01:10.920
we copied into the root directory

01:10.920 --> 01:12.990
or the root folder of the container.

01:12.990 --> 01:14.400
And so in this root directory,

01:14.400 --> 01:18.000
we can see the Dockerfile, a package lock dot JSON file,

01:18.000 --> 01:20.940
which is automatically generated by NPM,

01:20.940 --> 01:23.220
package dot JSON, index dot JS,

01:23.220 --> 01:24.960
and all of our install dependencies

01:24.960 --> 01:27.210
were placed into this node modules folder.

01:27.210 --> 01:28.860
And again, these were all placed

01:28.860 --> 01:32.790
into the root directory of this container.

01:32.790 --> 01:35.640
Now, this is definitely not the best practice,

01:35.640 --> 01:36.690
and the reason for that

01:36.690 --> 01:39.810
is that if we happen to have any files or folders

01:39.810 --> 01:42.420
that conflict with the default folder system,

01:42.420 --> 01:45.540
like if we have a folder called var or root or run

01:45.540 --> 01:48.780
or lib, super, super likely for us

01:48.780 --> 01:50.940
to have a folder called lib inside of our project,

01:50.940 --> 01:52.710
we might accidentally overwrite

01:52.710 --> 01:56.310
some existing files or folders inside of the container,

01:56.310 --> 01:58.680
which is definitely not ideal.

01:58.680 --> 02:01.320
So we're gonna make a little change to our Docker file

02:01.320 --> 02:03.240
and rather than copying everything

02:03.240 --> 02:05.910
directly into the root project directory,

02:05.910 --> 02:07.170
we're actually gonna copy everything

02:07.170 --> 02:10.320
into, kind of, a nested directory instead.

02:10.320 --> 02:11.820
Now, rather than just changing

02:11.820 --> 02:15.240
the copy command inside of our Docker file and saying,

02:15.240 --> 02:18.120
oh yeah, copy it into, like, some application folder

02:18.120 --> 02:19.230
or something like that,

02:19.230 --> 02:21.210
there's actually an instruction

02:21.210 --> 02:23.192
that we can use inside the Dockerfile

02:23.192 --> 02:24.960
that is specifically meant to address

02:24.960 --> 02:26.280
this whole issue right here,

02:26.280 --> 02:28.740
of accidentally overriding files or folders

02:28.740 --> 02:30.600
by copying into a root directory.

02:30.600 --> 02:33.093
So let's take a look at what the instruction is.

02:34.650 --> 02:38.700
So we can add the workdir or work directory instruction

02:38.700 --> 02:40.050
into the Dockerfile

02:40.050 --> 02:43.083
and then pass a reference to a folder to it.

02:44.100 --> 02:48.090
Then, any following commands or any following instructions

02:48.090 --> 02:49.500
that we add to our Docker file

02:49.500 --> 02:53.010
will be executed relative to this folder.

02:53.010 --> 02:56.280
So in other words, if we add the workdir and then a folder

02:56.280 --> 02:59.340
above the copy instruction right here,

02:59.340 --> 03:01.920
it will make sure that we only execute,

03:01.920 --> 03:02.760
or excuse me, it'll make sure

03:02.760 --> 03:04.740
that when we actually do the copy instruction

03:04.740 --> 03:07.530
it won't copy it into the root directory,

03:07.530 --> 03:10.080
It'll copy it into the directory that we have specified

03:10.080 --> 03:12.690
as the working directory instead.

03:12.690 --> 03:15.000
So let's try doing that right now.

03:15.000 --> 03:17.940
I'm gonna add in an instruction of workdir,

03:17.940 --> 03:19.260
and then we are going to use

03:19.260 --> 03:22.500
a folder of slash user slash app.

03:22.500 --> 03:25.560
So let's say slash user slash app.

03:25.560 --> 03:27.900
If this folder does not exist inside the container,

03:27.900 --> 03:30.090
it'll be automatically created for us.

03:30.090 --> 03:33.750
Now you might be curious as to why I'm using user slash app.

03:33.750 --> 03:36.360
Well, to be honest, in the Node.js world

03:36.360 --> 03:39.030
it doesn't make a tremendous difference

03:39.030 --> 03:40.710
where you put your application into

03:40.710 --> 03:42.750
in a Linux-based operating system.

03:42.750 --> 03:44.850
There definitely are places that you don't want to put it,

03:44.850 --> 03:46.260
but at the end of the day,

03:46.260 --> 03:49.020
the user folder is a safe place to put your application.

03:49.020 --> 03:51.000
It's specified as the location

03:51.000 --> 03:54.000
for all of user home directories.

03:54.000 --> 03:55.680
Essentially, it's an okay place

03:55.680 --> 03:57.270
for us to put our application.

03:57.270 --> 03:58.230
Now, as I say that,

03:58.230 --> 04:00.630
there's definitely a lot of Linux diehards out there

04:00.630 --> 04:02.130
who would probably disagree with me

04:02.130 --> 04:04.650
and they might say, oh no, you should put this into var

04:04.650 --> 04:07.320
or you should put it into the home directory instead.

04:07.320 --> 04:08.850
So there is some disagreement out there

04:08.850 --> 04:11.070
on where the best place is for your application,

04:11.070 --> 04:12.600
but I can just about guarantee you

04:12.600 --> 04:14.640
that if you put it into user slash app,

04:14.640 --> 04:16.473
you're probably gonna be okay.

04:17.940 --> 04:20.940
Okay, so now we've added in the workdir instruction.

04:20.940 --> 04:23.040
I'm now gonna save the Dockerfile

04:23.040 --> 04:24.870
and then we'll flip back over to our terminal.

04:24.870 --> 04:28.590
I'll exit the running container by typing in exit

04:28.590 --> 04:29.640
and then we're going to make sure

04:29.640 --> 04:31.860
that we first rebuild our container

04:31.860 --> 04:34.290
and then we'll try to relaunch it.

04:34.290 --> 04:38.820
So I'll do a rebuild with Docker build dot.

04:38.820 --> 04:40.200
Oh, let's not forget to tag it.

04:40.200 --> 04:42.600
So Docker build dash T,

04:42.600 --> 04:47.550
your docker ID slash simpleweb, and then a dot.

04:47.550 --> 04:48.383
There we go.

04:49.800 --> 04:51.300
So we're going to rebuild it.

04:51.300 --> 04:53.550
Now, one thing that you'll notice is really interesting

04:53.550 --> 04:56.280
because we made a change to an instruction

04:56.280 --> 04:58.920
above the copy and NPM install,

04:58.920 --> 05:02.700
everything after that instruction has to rerun from scratch,

05:02.700 --> 05:05.070
so we cannot use any cached versions.

05:05.070 --> 05:08.130
In other words, the NPM install command has to run again

05:08.130 --> 05:10.050
and reinstall all of our dependencies,

05:10.050 --> 05:13.173
'cause we cannot use the cached version of that step.

05:15.300 --> 05:16.470
So we got our output here.

05:16.470 --> 05:18.690
Let's try starting up our container again.

05:18.690 --> 05:20.640
I'll first start it up in normal mode

05:20.640 --> 05:22.950
and try to visit it inside my browser.

05:22.950 --> 05:27.300
So we'll do a docker run dash P, to set up a port mapping

05:27.300 --> 05:30.690
and we'll map 8080 to 8080.

05:30.690 --> 05:33.030
And then, the container that I want to run

05:33.030 --> 05:35.883
is Steve Grider slash simpleweb.

05:39.180 --> 05:43.830
So we'll run that and then I'll go back over to my browser.

05:43.830 --> 05:47.260
I'll try to visit localhost 8080

05:48.630 --> 05:50.160
and yep, it looks like we're good to go.

05:50.160 --> 05:52.080
I can refresh the page.

05:52.080 --> 05:55.110
And if we also try to start up a shell inside the container,

05:55.110 --> 05:57.180
we'll see that all of our project directories,

05:57.180 --> 05:58.560
or excuse me, all of our project files

05:58.560 --> 06:00.600
are no longer in the root directory.

06:00.600 --> 06:03.960
To do so, we can either rerun the Docker run command

06:03.960 --> 06:07.050
with the IT and SH attached to it

06:07.050 --> 06:09.570
or we can use that docker exec command.

06:09.570 --> 06:11.160
You'll recall we can use Docker exec

06:11.160 --> 06:14.760
to start up a second process inside of a running container.

06:14.760 --> 06:15.750
Let's try doing it that way,

06:15.750 --> 06:17.493
just for a little bit of variety.

06:18.600 --> 06:21.780
So I will open up a second terminal window.

06:21.780 --> 06:24.480
I'll get the ID of that running container

06:24.480 --> 06:26.430
by using Docker PS.

06:26.430 --> 06:28.530
So here's the ID right here

06:28.530 --> 06:30.360
and then we can attach to that container

06:30.360 --> 06:32.100
and start up a shell inside of it,

06:32.100 --> 06:37.100
by using Docker exec dash IT, the ID of the container

06:37.470 --> 06:39.960
and then the program that we want to run inside of it,

06:39.960 --> 06:42.000
which in this case is shell.

06:42.000 --> 06:43.470
Don't forget the IT, right here,

06:43.470 --> 06:46.950
is to attach standard in and a nice looking terminal

06:46.950 --> 06:48.350
to the shell that starts up.

06:49.410 --> 06:50.880
So I'll run that and you'll notice

06:50.880 --> 06:55.050
that we enter directly in to the user slash app folder

06:55.050 --> 06:58.470
because we had set up that working directory previously.

06:58.470 --> 07:00.330
That workdir instruction, right here,

07:00.330 --> 07:03.270
not only affects commands that are issued later on

07:03.270 --> 07:04.680
inside of our Docker file,

07:04.680 --> 07:07.350
it also affects commands that are executed

07:07.350 --> 07:08.910
inside the container later on

07:08.910 --> 07:10.653
through the Docker exec command.

07:12.240 --> 07:14.370
So if I now do an ls right here,

07:14.370 --> 07:16.650
I'll see all of my project files and folders

07:16.650 --> 07:19.380
nicely isolated inside of the single file.

07:19.380 --> 07:21.540
And I can change back to my root directory

07:21.540 --> 07:24.300
by doing CD forward slash,

07:24.300 --> 07:26.790
print out all the files and folders there with ls

07:26.790 --> 07:27.623
and I'll see

07:27.623 --> 07:30.540
that I do not have any possible conflicts going on.

07:30.540 --> 07:32.940
We have nicely spaced our application,

07:32.940 --> 07:34.890
or kind of, isolated our application

07:34.890 --> 07:37.170
into this folder right here.

07:37.170 --> 07:38.520
All right, so that's pretty much it.

07:38.520 --> 07:39.520
Looking pretty good.

07:40.500 --> 07:41.333
Now, believe it or not,

07:41.333 --> 07:44.490
there is one last little thing that I wanna look at.

07:44.490 --> 07:46.380
So let's do one more quick break

07:46.380 --> 07:48.150
and we'll come back in the next section

07:48.150 --> 07:50.610
and do one quick last little thing.

07:50.610 --> 07:52.260
So I'll see you in just a minute.
