- We've been talking about how Docker images
are made up of multiple layers
and the more that you add to the image,
from your Docker file,
the more layers you will have.
To help demonstrate this I've edited
the dd-docker Docker file here to add
a number of new files.
These are just simple text files
that I moved into the current directory.
And these will be added to this Docker file
when we do a build.
So if we do a docker build and
we specify the Docker file, dd-docker
and let's specify a tag for the image
that will be built.
Let's call it ununtu:v2
and we'll specify the build context
to be the current directory.
So we just built ubuntu version two.
If we do a docker image history ubuntu:v2
take a look at all these layers that make up this image.
There's multiple layers.
And that's from all the files that we copied in,
as well as the apt-get update,
and even the label that we created
and the bin bash that we executed.
So we were the reason that those layers were created,
but having multiple layers isn't always very efficient.
So how can we reduce the image size
and move it down to a single layer?
Well, you have a couple of different options.
The first option is to use a new
experimental feature called squash.
And it's not like the vegetable,
squash actually squashes down the images,
multiple image layers into a single image layer.
So, here's how it's done.
First off, you need to make sure
that you have experimental enabled.
So when you do a docker version,
you should see experimental is true
and if you're unaware how to enable experimental
you can go out there and do a quick Google on that.
Essentially, it's editing the Docker service JSON file
and then restarting Docker.
And perhaps by the time that you
are actually performing these commands
on your own version of Docker,
that feature may no longer be experimental.
So to use the squash feature,
you actually can perform the same build if we go back here.
So we've got docker build -f
to specify the Docker file, -t
and we're going to tag it with version three this time.
And then we're going to specify this
new experimental option called squash, right there.
So let's run the same docker build as before
with the squash option specified this time.
And now let's do a docker image history,
first on the original image that we built, version two,
and you can see all the layers that are up here.
We've got multiple layers.
And if we run the same command on version three,
notice we just have one layer,
and there's a comment, this is actually a comment.
It says, essentially, "Hey, all these layers were merged
"into this one layer."
So we were able to use the squash command
to squash down that image into a single layer.
Now there is another way to reduce image size
and eliminate the image history.
If we do a docker image history on our web server,
called nginx, you can see there's a long history here
and it has a fair amount of size,
roughly over a hundred megabytes.
So if we want to do a docker container run on this image,
with a -d to start it up as a daemon,
and do a docker ps,
we can see we've got nginx running,
actually twice now, I ran it once before in my testing.
So what we can do with this is we can do
a docker container export on the jovial_jackson container,
and we can export that to nginx.tar.
All right, so let's do an ls -l
and there's the nginx.tar.
And then what we can do is we can do
a docker image import on nginx.tar.
Right, so we brought this image back in.
Let's do a docker image ls,
and this is actually the container that we just brought in,
it says six seconds ago.
We didn't give it a tag.
Let's do a clear.
Now I'll show that again.
We can do a docker tag on image e69,
and call it newnginx.
All right, docker image ls.
And there's the newnginx.
You can see it's 107 megabytes, maybe a little bit smaller,
but let's do a docker image history on newnginx.
And there you go.
You can see we just have a single image layer.
On that the history is completely gone,
and most likely we reduced the size
of the image to some degree.
So with that, the best option to reduce image size
and squash the layers down into a single image layer,
is to use the new experimental squash feature
of docker build.
You can also export a running container
and then import that back in as an image
to squash the image size down to a single layer.