So that's our application running with Docker now
and it works, right?
Everything is great.
Well, here's what's not so great.
First of all, let's stop this application
which you of course can do.
Let's stop this container.
And since I added dash dash RM when I created the container
it is also automatically removed when it's stopped.
Once it's stopped, let's restart it.
And since I did add dash dash RM
we have to use Docker run again to create a new container
because thanks to RM the previous container was deleted.
Now let's run this container without dash dash RM.
So it's a new container again, with the name feedback app
and therefore, I can of course reload the app and it works.
But you'll notice that if I try to view feedbackawesome.txt,
this file doesn't exist.
It fails to get this file.
And of course, this worked a couple of minutes ago.
Now of course, that's the case
because we deleted the container
when we stopped it the first time.
If I for example, here, readd this,
added again in the newly started container,
we can view it here.
And if I now stop this container, feedback app
and keep in mind that now it will not be removed
because we did not add dash dash RM
on the Docker run command.
So if I now stop it without the container being removed
and I restart it thereafter,
you will notice that now if I reload,
the file is still there.
So the file was not lost because we stopped the container
but because we removed the container.
And I guess that makes a lot of sense.
I talked about this isolation concept in great depth
and I explained depth with the Docker file we build an image
and that image has its own internal file system
which is detached from the hosting machine file system.
And when we then launch Docker container based on the image,
that container is added as a extra thin read-write layer
on top of this image.
So it basically has access to this image file system
and it's able to read and write there
without manipulating the image though.
But it's able to read and write there.
It has its own copy of that you could say
managed in a very efficient way though.
So that makes all sense that we can read and write in there.
But the problem is,
that the file system is inside of the container.
So if we stop it and restart it everything is fine
because the container never changed.
And just because it was stopped does not mean
that its file system is cleared or erased.
If that would be the case
our application code would be gone as well, right?
So that's not happening.
But if we remove a container, then that's different.
Then all the data in the container is cleared
because the container overall is cleared and deleted.
And if we then run a new container,
even if it's spaced on the same image,
all the data that was created and stored
in the previous container is lost,
because the image is read-only.
So the container, when a file is generated in the container
does not write this file into the image.
It writes it in its own read-write layer
which is added on top.
Therefore, if the container is removed
we're just left with the image which was never changed.
So when we then start a new container
well, that starts with the same basic file system again
without the changes made by the previous container
which might've been executed on the same image.
And that's a core idea of Docker, multiple containers
based on the same image are totally isolated
from each other.
But here of course, that's a problem.
It means that we lose this text file
if we stop the container and remove it thereafter.
And in many applications, this behavior is a problem
because here, for example,
we wanna keep those feedback text files around
even if a container is deleted.
We would have a comparable scenario if we would be dealing
with user accounts or with product data submitted by users
or any other kind of data
which shouldn't be disappearing suddenly
but which instead should survive containers being deleted.
Because in reality, you're going
to remove containers quite a bit.
If we change something about our code
and be built a new image and start a new container
will not restart the old container.
We wanna use the new container
which uses the latest code snapshot.
So therefore, in that case, we would be losing all the data.
And that's exactly the problem I've been describing
over the last minutes.
So now that we know the problem really well
and that we know that containers are able to write data
but that this data is lost when the container is removed
now that we know all of that,
what is the solution?