instructor: Now, throughout this course,
we'll see way more commands and way more ways of running
and configuring images and containers.
Now, I wanna focus on a couple of other key functionalities,
which will often be useful.
And that starts with managing our images and containers,
especially the ones which we maybe don't need anymore.
If I print my running containers,
I currently got two containers up and running.
If I print all my containers,
you see i have a decent list here.
You might have less containers than that
because I also started
and stopped some containers off screen,
but still by now, if you followed along,
you also have a couple of stopped
and also a couple of running containers.
And of course, this list gets longer and longer
the more containers you're working with.
And therefore it might make sense to clean this list up
from time to time and to remove containers
you don't need anymore.
And you can do this with the docker rm command,
which stands for remove and the this removes containers.
Now, again, if I print containers,
I got two running containers.
And for example, if you remove a running container
by parsing the container name to rm, you get an error,
you cannot remove a running container,
which I guess makes sense.
So we, first of all need to stop running containers
to be able to then remove them thereafter.
Therefore I will do that here.
I will stop the two containers I still have up and running
at this point here with docker stop and there after,
we can use the rm command
to remove those containers as well.
And I wanna clean up all my containers here
so that I have a cleaner and shorter ps -a list here.
Therefore what we can do is we can run docker rm,
and then actually parse multiple container names at once,
simply separated by white space.
And that's what I'm doing here.
I'm just copying all those container names
and I'm parsing them to docker rm.
This is a bit cumbersome,
but I'll soon show you a way of ensuring that containers
are removed automatically if not needed anymore.
So here I'm doing this manually though,
just copying in all those containers,
which I started in the past, which are now all stopped.
And after running this, if I run docker ps -a again,
I have an empty list
and that's how we can clear and remove containers.
Now, clearly this is a very cumbersome way of doing it.
So I'll soon show you a more elegant
or easier way of removing a container
when it's not needed anymore.
Before we do that, though,
let's dive into images
and see how we can manage those.
For Debian, also got a command ps lists, containers,
images, lists images, and here by default,
we see all the images we have.
Here we see the node image,
which we pulled in earlier, and you'll see,
this was created three days ago, not by us,
but by the node team and we pulled it down and used it.
And you also see the size it's occupying
on your hard drive somewhere.
And then we see our accustom images,
which we built based on our docker file.
And you see the size is basically node
and then a tiny bit of extra size,
which are our node modules dependencies and our source code.
So the majority of the size is made up by node
and they're by the way, in this node image,
which we used they're the node image,
all it built up on another image,
which is basically a Linux operating system layer
on which this node images is placed.
So here again, this size is not just node
and the node executable tools,
but those tools and the operating system image
on which the node image builds up.
But that's just a side node.
Now the main thing here is that we got a bunch of images,
which we might or might not need.
And we can also remove images here with the rmi command.
So rm for removing containers, rmi for removing images,
and you can then simply grab the image ID
and parse that to rmi like this.
And now this deletes this image and basically all the layers
inside of the image, which is what you see here.
So now if I print docker images again,
we see all the images except for the image
which we just removed.
Now, one important note about removing images,
you can only remove images if they're not getting used
by any container anymore
and that includes stopped container.
If you have a container which is stopped,
then you can't remove images being used by that container.
You need to remove that container first.
So no matter if a container is started or stopped
images belonging to that container, can't be removed.
The container needs to be removed first.
Now, if you wanna get rid of all the images,
which are not being used in running containers
at the moment, you also got the docker image.
So not images but image, prune command,
and this will remove all unused images.
In this case, this would actually remove all the images
and I don't wanna remove all images.
So here I'll just manually remove a couple of images.
You can also remove more than one image at the same time,
just by adding the different image IDs
separated by white space after rmi
and once that is done
I'm left with just a node image
on which I'm building up in my own custom images
and my last image which I built
based on that most recent docker file we have here.
And now of course, we could also run
a new container based on that image.
And that's something I wanna do, but not just like this.