So we created our first Dockerfile.
Therefore, we then also built our first custom image
and we ran the first container based on a custom image.
Now, there is way more
which we can and will learn about this Dockerfile
and also about the Docker command
and all the subcommands we can run there.
All the different ways of configuring things.
We are going to explore that.
But before we explore anything related to that,
it is important that we fully understand
how an image and a container work
and how they work together.
And for that, let's say in our Node application code,
we wanna change that code.
We wanna change that code and say
My Course Goal! Here for example.
This is a tiny change made to this HTML code
which now should be reflected in our running application.
So if I rerun this command to restart this container
based on this image, which we built,
the application, the Node application starts again.
And if I now reload localhost 3000
I see this application again.
But we also see one problem.
My little change in code isn't reflected here.
Keep in mind, I added an exclamation mark
after My Course Goal.
Here, we still see My Course Goal like this
without the exclamation mark.
So, why is this change not reflected?
I mean, I even restarted the container in the meantime.
In case you didn't do that by the way,
you learned that with docker PS
you can see running containers, then you can pick the name
and run docker stop to stop it and then restart it.
But again, that restarting didn't do anything here.
I don't see that change in my web application.
Because we have to understand how images work,
keep in mind that this is part of our source code,
our Node application code.
What are we doing with that code?
Well, in the Docker file, we instruct Docker,
to in the end, copy everything inside of this project folder
including the server.js file, which holds my source code
into the container file system to be precise
into the app folder.
Then we run npm install,
tell Docker that it should open up port 80
and start the server when the container is launched.
Now, therefore we do one important thing.
We copy our source code into the image,
and we basically take a snapshot of our source code
at the point of time we copied.
If I thereafter edit my source code,
as I did it here when I added this exclamation mark,
this change is not part of the source code in the image.
We need to rebuild our image to copy our updated source code
into a new image.
And that is really crucial.
Now, if this sounds cumbersome and strange
that we have to rebuild our image
every time we change our code,
I have a good message for you.
We will find a more elegant and faster way
of picking up changes in our code later.
But the core takeaway here is important
and will always be true.
Images are basically locked and finished
once you built them.
Everything in the images is read only then,
and you can't edit it from the outside
by simply updating your code
just because you copied that code in the past.
The image doesn't care about the past.
Once this copy operation is done,
you can change your outside code however you want.
You can even delete it
and the image will not be affected.
You need to rebuild to pick up external changes
and basically copy all the updated code into the image.
So therefore,
what we need to do here
is we need to run docker build dot again
to rebuild this image
and therefore to build a new image in the end.
So, let's wait for this operation to finish.
And once we get this new image,
we also got a new image name,
because it now really is a totally new image.
It's almost the same as the previous one,
but technically it's totally different.
It has totally different code inside of it.
And we can now docker run this new image
by using this new image name.
And if we do this now and we reload localhost 3000,
you now see the exclamation mark is there
and this change has now been picked up.
Because we rebuilt a new image
with our new code inside of it.
And I'm putting so much emphasis on that,
because it is super crucial to understand
that an image is really a closed template in the end.
These instructions are executed to create the image,
and thereafter it's locked, it's finished.
And if you change something which you copied
into your image thereafter, that has no impact.
So with that all,
also stop this newly created Docker container here.
And we can now dive a bit deeper into images
and again, also containers.
Because there is more which we need to explore here.