- Docker administrators need to know
how to manage images from the command line.
If we do a docker space image command, you can see
there's a number of different command line options.
For example we did a build, we built an image
from a docker file using the build command.
We used the history command
to show us the history of an image.
What's the build history of the image?
What are the different layers
that were created to build this image?
We also used the inspect command,
to show us a detailed inspection of the image.
But there are a number of other commands on this list
that we have yet to use.
For example import, which imports
the contents of a tar file.
To create an image, there's load,
which loads in an image from a tar archive.
There is the ls command,
which use to list out docker images.
Prune allows you to remove unused images.
There is the push and the pull command,
to push or pull images to and from a repository or registry.
Then there's the rm command, to remove one or more images.
You can also use save to save images to a tar archive,
and then finally tag, to tag a docker image.
So let's try out a few of these commands.
The first one if we clear the screen here,
the first one I wanna use is docker space ls.
Of course this is a basic command
that just lists out the images that are on this host.
You can see from the list, what repository the image is in,
what tag the image has, a truncated version of the image ID,
when the image was created and the size of the image.
Let's say we wanted to get rid of one of these images,
you can do a docker image space rm to remove an image,
and then either you can use the image name, with the tag,
or you can use the image ID.
For example if we use the image ID,
it just has to be unique in the list there.
In other words the first few letters
have to be able to uniquely identify that image.
For example if we try to remove that image, 3ed right there,
we get a message that the image cannot be deleted,
it must be forced because the image
is being used by a stock container.
So first we would want to actually go in
and use docker container ls,
and do a -a to see the stock containers,
and then in this case do a docker container rm, e48,
so we removed that container.
But if we clear the screen here,
and let's do a docker image ls
and then a docker image rm, again on the 3ed.
And there we go, we deleted that image
and it lists out every layer of the image, as it's deleted.
So let's do a docker image ls again,
you can see that it's gone.
If we do a clear, another way,
the actually deprecated way of doing a docker image ls,
is just to run docker images.
So docker images and docker image ls,
do the exact same thing, but docker would prefer
people use the docker image standard
with the sub-commands instead of docker images.
Another command that you might see is docker rmi,
to remove one or more images here.
So docker rmi allows you to remove an image,
which is the exact same thing
as doing a docker image rm, exact same command.
The version that docker prefers is docker image rm.
So we showed how to list images,
we showed how to remove images,
let me now show you how to prune images.
If we do a docker image command,
you can see there is a command option called prune,
which removes unused images.
If we clear the screen here and do a docker image ls,
you can see what images are currently on this host.
If we do a docker image prune,
it says this will remove all dangling images.
So I'll say yes here and indeed 204 megabytes was reclaimed
and images we deleted in a number of different layers.
Let's do a docker image ls again,
but what do you know, there's still remaining images.
This brings up an important point here,
which is it says it will remove all dangling images.
So what is a dangling image?
Well a dangling image is an image that was built,
but doesn't have a tag, so it's untagged out there,
and according to docker, it's not going to be used,
if it doesn't really have a name,
it doesn't have a tag if you will.
So what if we want to remove all the images?
Well we have to do a docker image prune -a.
And notice how we get a different message here,
this says "This will remove all images
without at least one container associated with them."
So the good thing about this is if you have images
that are being used by containers,
those images won't be deleted with this.
If I say yes, there we go,
we reclaimed a lot more space in doing that,
and if I clear the screen and do a docker image ls,
now we can see that all of our images are really gone.
So you learned how to prune images,
remove images, list out images.
Those are the basics when it comes to managing images
at the CLI in docker.