We're now successfully able to visit our running web application site the browser.
But there's one last little thing that I want to point out.
I'm going to start my container up using the image that we've already built.
But this time I'm going to start up a shell inside the container so that we can do a little bit of debugging
inside there.
Remember that we can start up a shell inside of most Alpine based containers by running Docker run dash
I.T. and I'll put down my container name of Steven Greider simple web.
And then I will override the default startup command with sdh.
Remember this is a reference to the shell program.
It will start up a shell that we can type commands into inside the container.
Now you'll notice that I did not specify a port mapping here.
It's because we are not going to actually start up the server.
I just want to attempt to look at the files and folders inside the container so I'll run that and I
will get a command prompt based inside of the root directory of the container.
Now inside of here I'm going to print out all the available files and folders with L S so inside of
here.
You'll notice that when we did that copy operation we copy it into the root directory or the root folder
of the container.
And so in this root directory we can see the darker file a package lock dot Jason File which is automatically
generated by NPM package that Jason indexed J.S. in all of our install dependencies were placed into
this node modules folder.
And again these were all placed into the root directory of this container.
Now this is definitely not the best practice.
And the reason for that is that if we happen to have any files or folders that conflict with a default
folder system like if we have a folder called var or root or run or Lib super super likely for us to
have a folder called live inside of our project we might accidentally overwrite some existing files
or folders inside the container which is definitely not ideal.
So we're gonna make a little change to our Docker file and rather than copying everything directly into
the root project directory we're actually gonna copy everything into kind of a nested directory instead.
Now rather than just changing the copy command inside of our Docker file and saying oh yeah copy it
into like some application folder or something like that there's actually an instruction that we can
use inside the docker file that is specifically meant to address this whole issue right here of accidentally
overwriting files or folders by copying into a root directory.
So let's take a look at what the instruction is so we can add the work dir or work directory instruction
into the docker file and then pass a reference to a folder to it then any following commands or any
following instructions so we add to our Docker file we'll be executed relative to this folder.
So in other words if we add the work dir and then a folder above the copy instruction right here it
will make sure that we only execute or Sammy it'll make sure that when we actually do the copy instruction
it won't copy it into the work directory.
I mean the root directory.
It'll copy it into the directory that we have specified as the working directory instead.
So let's try doing that right now.
I'm going to add in an instruction of work dir and then we're going to use a folder of slash user slash
app.
So say slash user slash app if this folder does not exist inside the container it'll be automatically
created for us.
Now you might be curious as to why I'm using it user slash app.
Well to be honest in the No GST world it doesn't make a tremendous difference where you put your application
into in a Linux based operating system.
There definitely are places you don't want to put it but at the end of the day the user folder is a
safe place to put your application it's specified as the location for all of user home directories.
Essentially it's an okay place for us to put our application.
Now as I say that there's definitely a lot of Linux diehards out there who would probably disagree with
me and they might say oh no you should put this in to var or you should put it into the home directory
instead.
So there is some disagreement out there on where the best place is for your application.
I can just about guarantee you that if you put it into user slash app you're probably going to be OK.
OK.
So now we've added into the worked our instruction.
I'm not going to save the docker file and then we'll flip back over to our terminal I'll exit the running
container by typing in exit and then we're going to make sure that we first rebuild our container and
then we will try to relaunch it.
So I'll do a rebuild with docker build dot.
Now let's not forget to tag it so docker build dash t your Docker ideas slash simple web and then a
dot.
There we go.
So we're going to rebuild it.
Now one thing that you'll notice is really interesting because we made a change to an instruction above
the copy and npm install everything after that instruction has to rerun from scratch.
So we cannot use any cash versions.
In other words the npm install command has to run again and reinstall all of our dependencies because
we can not use the Cash version of that step so we got our output here.
Let's try starting up our container again.
First started up in normal mode and try to visit inside my browser.
So we'll do a docker run DSP to set up a port mapping and we'll map 80 80 to 80 80 and then the container
that I want to run is steam Greider slash simple web
so we'll run that and then I'll go back over in my browser.
I'll try to visit local host 80 80 and yet it looks like we're good to go.
I can refresh the page and if we also try to start up a shell inside the container we'll see that all
of our project directories or something all of our project files are no longer in the root directory
to do so we can either rerun the docker run command with the I.T. and SDH attached to it or we can use
that Docker exact command.
You recall we can use Docker exact to start up a second process inside of a running container.
Let's try doing it that way just for a little bit of variety.
So I will open up a second terminal window.
I'll get the idea that running container by using Docker.
Yes.
So here's the idea right here and then we can attach to that container and start up a shell inside of
it by using Docker exactly dash I.T. the I.D. of the container and then the program that we want to
run inside of it which in this case is Shell.
Don't forget the I.T. right here is to attach standard in and a nice looking terminal to the shell that
starts up.
So I'll run that and you'll notice that we enter directly in to the user slash app folder because we
had setup that working directory previously that worked our instruction right here.
Not only affects commands that are issued later on INSIDE OF OUR Docker file it also affects commands
that are executed inside the container later on through the docker exact command so if I now do an LSD
right here I'll see all of my project files and folders nicely isolated inside of this single file and
I can change back to my root directory by doing CDE forward slash printout all the files and folders
there with Elvis and I'll see that I do not have any possible conflicts going on.
We have nicely spaced out application or kind of isolated our application into this folder right here.
All right.
That's very much it looking pretty good now believe it or not there is one last little thing that I
want to look at.
So let's do one more quick break and we'll come back in the next section and do one quick quick last
little thing.
So I'll see you in just a minute.