1
00:00:02,060 --> 00:00:03,100
<v instructor>Now, throughout this course,</v>

2
00:00:03,100 --> 00:00:06,420
we'll see way more commands and way more ways of running

3
00:00:06,420 --> 00:00:09,230
and configuring images and containers.

4
00:00:09,230 --> 00:00:13,150
Now, I wanna focus on a couple of other key functionalities,

5
00:00:13,150 --> 00:00:15,230
which will often be useful.

6
00:00:15,230 --> 00:00:19,480
And that starts with managing our images and containers,

7
00:00:19,480 --> 00:00:23,260
especially the ones which we maybe don't need anymore.

8
00:00:23,260 --> 00:00:25,200
If I print my running containers,

9
00:00:25,200 --> 00:00:28,090
I currently got two containers up and running.

10
00:00:28,090 --> 00:00:30,840
If I print all my containers,

11
00:00:30,840 --> 00:00:33,360
you see i have a decent list here.

12
00:00:33,360 --> 00:00:36,000
You might have less containers than that

13
00:00:36,000 --> 00:00:37,490
because I also started

14
00:00:37,490 --> 00:00:40,500
and stopped some containers off screen,

15
00:00:40,500 --> 00:00:43,230
but still by now, if you followed along,

16
00:00:43,230 --> 00:00:45,330
you also have a couple of stopped

17
00:00:45,330 --> 00:00:48,140
and also a couple of running containers.

18
00:00:48,140 --> 00:00:52,580
And of course, this list gets longer and longer

19
00:00:52,580 --> 00:00:54,950
the more containers you're working with.

20
00:00:54,950 --> 00:00:58,130
And therefore it might make sense to clean this list up

21
00:00:58,130 --> 00:01:00,900
from time to time and to remove containers

22
00:01:00,900 --> 00:01:02,930
you don't need anymore.

23
00:01:02,930 --> 00:01:06,550
And you can do this with the docker rm command,

24
00:01:06,550 --> 00:01:11,053
which stands for remove and the this removes containers.

25
00:01:11,890 --> 00:01:13,620
Now, again, if I print containers,

26
00:01:13,620 --> 00:01:15,700
I got two running containers.

27
00:01:15,700 --> 00:01:19,010
And for example, if you remove a running container

28
00:01:19,010 --> 00:01:22,780
by parsing the container name to rm, you get an error,

29
00:01:22,780 --> 00:01:24,700
you cannot remove a running container,

30
00:01:24,700 --> 00:01:26,610
which I guess makes sense.

31
00:01:26,610 --> 00:01:30,840
So we, first of all need to stop running containers

32
00:01:30,840 --> 00:01:33,550
to be able to then remove them thereafter.

33
00:01:33,550 --> 00:01:35,320
Therefore I will do that here.

34
00:01:35,320 --> 00:01:39,220
I will stop the two containers I still have up and running

35
00:01:39,220 --> 00:01:44,220
at this point here with docker stop and there after,

36
00:01:44,510 --> 00:01:46,860
we can use the rm command

37
00:01:46,860 --> 00:01:49,690
to remove those containers as well.

38
00:01:49,690 --> 00:01:52,200
And I wanna clean up all my containers here

39
00:01:52,200 --> 00:01:56,563
so that I have a cleaner and shorter ps -a list here.

40
00:01:57,750 --> 00:02:01,430
Therefore what we can do is we can run docker rm,

41
00:02:01,430 --> 00:02:05,730
and then actually parse multiple container names at once,

42
00:02:05,730 --> 00:02:08,510
simply separated by white space.

43
00:02:08,510 --> 00:02:09,920
And that's what I'm doing here.

44
00:02:09,920 --> 00:02:12,420
I'm just copying all those container names

45
00:02:12,420 --> 00:02:15,283
and I'm parsing them to docker rm.

46
00:02:16,290 --> 00:02:17,530
This is a bit cumbersome,

47
00:02:17,530 --> 00:02:20,900
but I'll soon show you a way of ensuring that containers

48
00:02:20,900 --> 00:02:24,640
are removed automatically if not needed anymore.

49
00:02:24,640 --> 00:02:27,340
So here I'm doing this manually though,

50
00:02:27,340 --> 00:02:30,150
just copying in all those containers,

51
00:02:30,150 --> 00:02:34,293
which I started in the past, which are now all stopped.

52
00:02:35,610 --> 00:02:39,070
And after running this, if I run docker ps -a again,

53
00:02:39,070 --> 00:02:40,850
I have an empty list

54
00:02:41,760 --> 00:02:45,700
and that's how we can clear and remove containers.

55
00:02:45,700 --> 00:02:49,560
Now, clearly this is a very cumbersome way of doing it.

56
00:02:49,560 --> 00:02:51,520
So I'll soon show you a more elegant

57
00:02:51,520 --> 00:02:54,400
or easier way of removing a container

58
00:02:54,400 --> 00:02:56,290
when it's not needed anymore.

59
00:02:56,290 --> 00:02:57,970
Before we do that, though,

60
00:02:57,970 --> 00:02:59,790
let's dive into images

61
00:02:59,790 --> 00:03:02,220
and see how we can manage those.

62
00:03:02,220 --> 00:03:06,400
For Debian, also got a command ps lists, containers,

63
00:03:06,400 --> 00:03:10,040
images, lists images, and here by default,

64
00:03:10,040 --> 00:03:12,580
we see all the images we have.

65
00:03:12,580 --> 00:03:14,530
Here we see the node image,

66
00:03:14,530 --> 00:03:17,320
which we pulled in earlier, and you'll see,

67
00:03:17,320 --> 00:03:20,340
this was created three days ago, not by us,

68
00:03:20,340 --> 00:03:24,720
but by the node team and we pulled it down and used it.

69
00:03:24,720 --> 00:03:27,630
And you also see the size it's occupying

70
00:03:27,630 --> 00:03:30,870
on your hard drive somewhere.

71
00:03:30,870 --> 00:03:32,930
And then we see our accustom images,

72
00:03:32,930 --> 00:03:36,060
which we built based on our docker file.

73
00:03:36,060 --> 00:03:39,060
And you see the size is basically node

74
00:03:39,060 --> 00:03:41,680
and then a tiny bit of extra size,

75
00:03:41,680 --> 00:03:46,570
which are our node modules dependencies and our source code.

76
00:03:46,570 --> 00:03:49,210
So the majority of the size is made up by node

77
00:03:49,210 --> 00:03:51,840
and they're by the way, in this node image,

78
00:03:51,840 --> 00:03:54,720
which we used they're the node image,

79
00:03:54,720 --> 00:03:57,010
all it built up on another image,

80
00:03:57,010 --> 00:04:00,440
which is basically a Linux operating system layer

81
00:04:00,440 --> 00:04:02,670
on which this node images is placed.

82
00:04:02,670 --> 00:04:06,020
So here again, this size is not just node

83
00:04:06,020 --> 00:04:08,260
and the node executable tools,

84
00:04:08,260 --> 00:04:12,060
but those tools and the operating system image

85
00:04:12,060 --> 00:04:14,010
on which the node image builds up.

86
00:04:14,010 --> 00:04:16,030
But that's just a side node.

87
00:04:16,030 --> 00:04:18,950
Now the main thing here is that we got a bunch of images,

88
00:04:18,950 --> 00:04:21,290
which we might or might not need.

89
00:04:21,290 --> 00:04:26,070
And we can also remove images here with the rmi command.

90
00:04:27,090 --> 00:04:32,090
So rm for removing containers, rmi for removing images,

91
00:04:34,050 --> 00:04:37,150
and you can then simply grab the image ID

92
00:04:38,180 --> 00:04:41,600
and parse that to rmi like this.

93
00:04:41,600 --> 00:04:45,220
And now this deletes this image and basically all the layers

94
00:04:45,220 --> 00:04:48,450
inside of the image, which is what you see here.

95
00:04:48,450 --> 00:04:51,230
So now if I print docker images again,

96
00:04:51,230 --> 00:04:53,700
we see all the images except for the image

97
00:04:53,700 --> 00:04:55,350
which we just removed.

98
00:04:55,350 --> 00:04:58,300
Now, one important note about removing images,

99
00:04:58,300 --> 00:05:01,870
you can only remove images if they're not getting used

100
00:05:01,870 --> 00:05:03,670
by any container anymore

101
00:05:03,670 --> 00:05:06,370
and that includes stopped container.

102
00:05:06,370 --> 00:05:09,170
If you have a container which is stopped,

103
00:05:09,170 --> 00:05:12,630
then you can't remove images being used by that container.

104
00:05:12,630 --> 00:05:14,960
You need to remove that container first.

105
00:05:14,960 --> 00:05:18,130
So no matter if a container is started or stopped

106
00:05:18,130 --> 00:05:21,400
images belonging to that container, can't be removed.

107
00:05:21,400 --> 00:05:24,163
The container needs to be removed first.

108
00:05:25,380 --> 00:05:27,510
Now, if you wanna get rid of all the images,

109
00:05:27,510 --> 00:05:29,870
which are not being used in running containers

110
00:05:29,870 --> 00:05:33,220
at the moment, you also got the docker image.

111
00:05:33,220 --> 00:05:36,690
So not images but image, prune command,

112
00:05:36,690 --> 00:05:39,880
and this will remove all unused images.

113
00:05:39,880 --> 00:05:43,130
In this case, this would actually remove all the images

114
00:05:43,130 --> 00:05:45,810
and I don't wanna remove all images.

115
00:05:45,810 --> 00:05:50,010
So here I'll just manually remove a couple of images.

116
00:05:50,010 --> 00:05:53,660
You can also remove more than one image at the same time,

117
00:05:53,660 --> 00:05:56,500
just by adding the different image IDs

118
00:05:56,500 --> 00:05:59,490
separated by white space after rmi

119
00:06:01,350 --> 00:06:03,130
and once that is done

120
00:06:03,130 --> 00:06:05,340
I'm left with just a node image

121
00:06:05,340 --> 00:06:07,930
on which I'm building up in my own custom images

122
00:06:07,930 --> 00:06:11,330
and my last image which I built

123
00:06:11,330 --> 00:06:14,843
based on that most recent docker file we have here.

124
00:06:16,230 --> 00:06:18,420
And now of course, we could also run

125
00:06:18,420 --> 00:06:21,260
a new container based on that image.

126
00:06:21,260 --> 00:06:24,653
And that's something I wanna do, but not just like this.

