- [Instructor] Docker containers use a layered
storage file system, which honestly is a stroke of genius.
Let me show you how it works.
Docker images are made up of multiple read-only layers,
and multiple containers are typically based
on the same image.
So when you start up an image
or you instantiate an image into a container,
a top writable layer is created,
which is then deleted
when the container is actually removed.
Docker uses what they call storage drivers
to manage the contents of the image layers,
as well as that top writable layer.
Each storage layer has different implementation,
but all drivers use this layered, stackable concept
and the copy-on-write strategy.
What that means is that a read-only file is only brought
from the lower read-only layers
into the writable layer
when that file is being modified.
Let me show you visually how this works.
Let's say that we're going to build our own container.
That container starts off with what they call a manifest.
The manifest is basically a text file
that describes what the container is going to be made up of.
Let's say that the manifest says that we should install
a base operating system, which you typically would use.
You would start your container based on, let's say,
an Ubuntu operating system.
From there, you might make some configuration changes
to the operating system,
perhaps configure a hostname, network settings,
usernames, and passwords.
After that, you might add some applications.
Perhaps you install a web server or a database server.
From there, perhaps there's some more configuration changes
that need to be made for that web server.
And then finally, you instantiate the container
or run the container, and then this top writable layer
is created so that the application has, for example,
a temporary file system to write temporary data.
And as you make any configuration changes
to the read-only files,
they're brought into that top writable layer,
and they can be optionally pushed back down.
So this is how the Docker layered file system works,
and Docker calls it the Docker Union File System.
Let's go to the command line so I can show you
the Docker layered file system in action.
Here in Docker, if we do a docker image ls,
you can see, for example,
we have a Docker image called httpd.
This is our httpd web server.
Notice that the image name or the image ID starts with 805.
If we clear the screen and then do a docker image inspect
on this image that starts with 805,
there's a lot of information
that you get from that inspect output.
But at the bottom of the inspect output,
you can see this list right here that shows
the different layers that make up this specific image.
You can see there's a number of different layers
that are all combined to create this single image.
So how did those multiple layers get created?
Well, let's clear the screen.
And if we do a docker history
on this image that starts at 805,
take a look at what happened here.
The creation of the image starts at the bottom,
with this first command here that added a file.
That created 123 megabytes of the base image.
From there, there were a number of configuration changes,
and then an apt-get update was run,
which added another 44 megabytes to the image.
From there, there were some more configuration changes.
A file was copied into the image
and then some additional configuration changes.
Finally, you get the image ID up here
that we have been using, which starts with 805.
Now, all of these other configurations, as you can see,
their image IDs are missing.
Those were actually performed on a different host,
not on this Docker container host.
So this image was actually built somewhere else.
It's from these multiple configurations and additional data
that's added to the image that we get
at these multiple layers that make up this Docker image.