1 00:00:00,000 --> 00:00:04,500 [Intro Music] 2 00:00:04,500 --> 00:00:05,640 In the previous lecture, I 3 00:00:05,730 --> 00:00:08,070 explained to you what happened when we 4 00:00:08,070 --> 00:00:10,050 have created a new hello-world 5 00:00:10,050 --> 00:00:12,360 container from hello-world image. 6 00:00:13,050 --> 00:00:14,850 And basically executable file 7 00:00:14,850 --> 00:00:17,760 called 'hello' was executed; and 8 00:00:17,820 --> 00:00:19,530 responsibility of this file is 9 00:00:19,530 --> 00:00:20,910 simply to bring text to the 10 00:00:20,910 --> 00:00:23,550 terminal. That's it. And afterwards 11 00:00:23,550 --> 00:00:26,070 container was stopped. Now, let me 12 00:00:26,070 --> 00:00:28,770 explain you what container actually 13 00:00:28,800 --> 00:00:32,430 is. On this diagram, you see image 14 00:00:32,460 --> 00:00:35,490 layers here. And you already know 15 00:00:35,490 --> 00:00:38,640 that every image is read-only. All 16 00:00:38,670 --> 00:00:40,560 'fs' layers in the image are read- 17 00:00:40,560 --> 00:00:42,270 only. You're not able to modify 18 00:00:42,270 --> 00:00:45,030 them. When you create a new 19 00:00:45,030 --> 00:00:47,070 container from a specific image, 20 00:00:47,700 --> 00:00:50,280 Docker creates a very small 21 00:00:50,340 --> 00:00:53,910 writeable layer, and you are able to 22 00:00:53,940 --> 00:00:56,040 read from that layer, and write to 23 00:00:56,040 --> 00:00:57,960 that layer, create new files and 24 00:00:57,960 --> 00:01:01,290 folders. And every container has 25 00:01:01,290 --> 00:01:04,379 its own writeable layer. In this 26 00:01:04,379 --> 00:01:06,060 example, there are two different 27 00:01:06,060 --> 00:01:08,010 containers, Container A and 28 00:01:08,010 --> 00:01:10,710 Container B created from the same 29 00:01:10,770 --> 00:01:13,560 image. Let me quickly do that 30 00:01:13,560 --> 00:01:15,780 actually. Let me go to terminal, and 31 00:01:15,780 --> 00:01:17,850 create one more hello-world 32 00:01:17,850 --> 00:01:21,390 container. 'docker run hello-world', 33 00:01:21,900 --> 00:01:24,330 and I see same result. And if I'll 34 00:01:24,330 --> 00:01:26,880 enter 'docker ps -a' command now, 35 00:01:27,150 --> 00:01:29,250 I will see that there are now two 36 00:01:29,280 --> 00:01:32,040 different stopped containers, and 37 00:01:32,040 --> 00:01:34,110 those two different containers used 38 00:01:34,140 --> 00:01:36,780 same hello-world image, here and 39 00:01:36,780 --> 00:01:39,540 here. But the command that was 40 00:01:39,570 --> 00:01:41,250 executed inside of every of those 41 00:01:41,250 --> 00:01:43,620 containers was the same. It was 42 00:01:43,620 --> 00:01:47,700 launch of the 'hello' file. Alright, 43 00:01:47,850 --> 00:01:50,100 let's proceed. After creation of the 44 00:01:50,100 --> 00:01:53,022 container, Docker actually launches 45 00:01:53,022 --> 00:01:55,440 specific process, specific 46 00:01:55,440 --> 00:01:57,090 process inside of the container. 47 00:01:57,840 --> 00:02:00,810 And as long as at least one process 48 00:02:00,840 --> 00:02:02,670 is running inside of the container, 49 00:02:03,180 --> 00:02:05,340 container is considered as running. 50 00:02:06,390 --> 00:02:09,030 But as soon as process ends, 51 00:02:09,389 --> 00:02:12,330 container is stopped. Let me 52 00:02:12,330 --> 00:02:13,699 explain you all of this using 53 00:02:13,699 --> 00:02:16,200 another diagram. On this diagram 54 00:02:16,290 --> 00:02:18,060 there is Docker Host. It is a 55 00:02:18,090 --> 00:02:20,610 Linux computer. There is Docker 56 00:02:20,640 --> 00:02:23,790 Engine installed inside. Also every 57 00:02:23,820 --> 00:02:25,800 Linux machine has a Linux Kernel, 58 00:02:25,890 --> 00:02:28,560 and such resources as CPU, RAM, 59 00:02:28,590 --> 00:02:30,960 network and hard drive. And let's 60 00:02:30,960 --> 00:02:33,360 suppose that we have already pulled 61 00:02:33,480 --> 00:02:36,180 some image from Docker Hub. Here 62 00:02:36,180 --> 00:02:38,070 was this image located on the hard 63 00:02:38,070 --> 00:02:40,740 drive of this Docker Host. And this 64 00:02:40,770 --> 00:02:42,510 image, let's assume has two 65 00:02:42,510 --> 00:02:45,240 different 'fs' layers, 'image fs 1', 66 00:02:45,270 --> 00:02:48,750 and 'image fs 2'. And again, those 67 00:02:48,780 --> 00:02:51,030 image layers are located on hard 68 00:02:51,030 --> 00:02:53,850 drive of this Docker Host. When you 69 00:02:53,880 --> 00:02:57,060 enter 'docker run', some image command, 70 00:02:57,630 --> 00:02:59,940 Docker will create additional 71 00:02:59,970 --> 00:03:02,760 writeable layer for new container. 72 00:03:02,880 --> 00:03:05,130 That is the first action. And this 73 00:03:05,160 --> 00:03:07,530 writeable layer will be created here 74 00:03:07,590 --> 00:03:09,360 on the hard drive of this Docker 75 00:03:09,360 --> 00:03:13,380 Host. Next you know that every 76 00:03:13,410 --> 00:03:16,500 image has instructions, which are 77 00:03:16,530 --> 00:03:18,810 executed when new container is 78 00:03:18,810 --> 00:03:20,730 created. And in case with 79 00:03:20,730 --> 00:03:22,620 hello-world image this command was 80 00:03:22,650 --> 00:03:26,520 run 'hello' executable file. And 81 00:03:26,520 --> 00:03:29,580 that means that new process on this 82 00:03:29,580 --> 00:03:31,770 Docker Host is created for a 83 00:03:31,770 --> 00:03:35,310 particular new container, like this. 84 00:03:36,180 --> 00:03:38,910 New process is created. This 85 00:03:38,910 --> 00:03:42,540 process does some job. And 86 00:03:42,540 --> 00:03:44,790 afterwards if this process ends, 87 00:03:45,810 --> 00:03:49,140 Docker removes this container from 88 00:03:49,140 --> 00:03:52,020 memory. And in such case, when 89 00:03:52,020 --> 00:03:53,820 there are no processes running 90 00:03:53,850 --> 00:03:56,280 inside of the container, container 91 00:03:56,340 --> 00:03:59,670 will be stopped by Docker. But 92 00:03:59,700 --> 00:04:02,460 those files, writeable 'fs' layer 93 00:04:02,460 --> 00:04:04,710 for this container remains on the 94 00:04:04,710 --> 00:04:08,640 hard drive. But if you remove a 95 00:04:08,700 --> 00:04:11,610 particular stopped container, then 96 00:04:11,610 --> 00:04:13,740 Docker of course removes this 97 00:04:13,770 --> 00:04:17,760 writeable 'fs' layer as well. It 98 00:04:17,760 --> 00:04:19,529 means that you are able to create 99 00:04:19,560 --> 00:04:22,200 multiple containers like so. Create 100 00:04:22,200 --> 00:04:24,060 Container 1 with additional 101 00:04:24,060 --> 00:04:27,000 writeable 'fs' layer, but notice 102 00:04:27,000 --> 00:04:29,610 that we don't need to create those 103 00:04:29,610 --> 00:04:32,520 layers because those layers are 104 00:04:32,520 --> 00:04:35,250 already present on hard drive. And 105 00:04:35,250 --> 00:04:37,920 this container will actually reuse 106 00:04:37,950 --> 00:04:41,160 those 'fs' layers. Any processes 107 00:04:41,190 --> 00:04:42,720 running inside of this container 108 00:04:42,840 --> 00:04:44,910 will reuse those 'fs' layers. 109 00:04:45,510 --> 00:04:47,700 Alright, writeable 'fs' layer was 110 00:04:47,700 --> 00:04:50,700 created, process was launched. And 111 00:04:50,700 --> 00:04:52,290 now let's suppose that this process 112 00:04:52,320 --> 00:04:54,570 is a long running process, like 113 00:04:54,600 --> 00:04:57,300 database process; or for example, 114 00:04:57,390 --> 00:04:59,460 we execute some scripts, perform 115 00:04:59,460 --> 00:05:01,260 some long running actions, and so 116 00:05:01,260 --> 00:05:03,870 on; and in such case when process 117 00:05:03,900 --> 00:05:06,720 is running, container is kept 118 00:05:06,720 --> 00:05:08,580 running, and there is process in 119 00:05:08,580 --> 00:05:11,670 memory of this Docker Host. And of 120 00:05:11,670 --> 00:05:14,010 course, Linux Kernel manages all 121 00:05:14,010 --> 00:05:17,010 requests to resources like CPU, RAM, 122 00:05:17,040 --> 00:05:19,290 network from this process. All 123 00:05:19,290 --> 00:05:21,900 requests come via Linux Kernel. 124 00:05:22,860 --> 00:05:24,480 Afterwards, when this container is 125 00:05:24,480 --> 00:05:25,680 running, let's suppose that you 126 00:05:25,680 --> 00:05:27,210 want to create a new container 127 00:05:27,270 --> 00:05:30,330 using same image. In such case 128 00:05:30,330 --> 00:05:33,270 again, Docker creates new writeable 129 00:05:33,270 --> 00:05:35,850 'fs' layer for new container. 130 00:05:36,930 --> 00:05:39,450 Those layers remain here. This 131 00:05:39,450 --> 00:05:41,850 Container 2 will simply reuse 132 00:05:41,910 --> 00:05:45,270 those two 'fs' layers. And next it 133 00:05:45,300 --> 00:05:47,550 creates new process for new 134 00:05:47,550 --> 00:05:50,670 container. And again, this process 135 00:05:50,940 --> 00:05:52,680 communicates with resources like 136 00:05:52,680 --> 00:05:55,050 CPU, RAM, network and hard drive 137 00:05:55,200 --> 00:05:58,750 via common Linux Kernel. Container 1 138 00:05:58,750 --> 00:06:01,000 and Container 2 communicate via 139 00:06:01,000 --> 00:06:03,800 common Linux Kernel. But 140 00:06:03,840 --> 00:06:05,400 processes are completely 141 00:06:05,400 --> 00:06:07,080 independent. They are different, 142 00:06:07,170 --> 00:06:09,300 but running on the same Docker 143 00:06:09,300 --> 00:06:12,690 Host. Alright, this is what happens 144 00:06:12,720 --> 00:06:14,670 when you create new containers 145 00:06:14,700 --> 00:06:17,460 based on a specific image. And key 146 00:06:17,460 --> 00:06:20,610 point here is that 'fs' layers of 147 00:06:20,640 --> 00:06:22,410 basic image that was used for 148 00:06:22,410 --> 00:06:24,570 creation of those containers are 149 00:06:24,600 --> 00:06:27,360 reused, they are not copied to any 150 00:06:27,360 --> 00:06:29,970 new container. Every new container 151 00:06:30,030 --> 00:06:33,240 gets just new writeable 'fs' layer, 152 00:06:33,330 --> 00:06:36,360 that's it. And when process is 153 00:06:36,360 --> 00:06:37,980 stopped in specific container, 154 00:06:38,190 --> 00:06:41,220 Docker stops container as well and 155 00:06:41,220 --> 00:06:44,400 removes it from memory. But files 156 00:06:44,460 --> 00:06:46,620 of this container still remain on 157 00:06:46,620 --> 00:06:48,690 the hard drive until you enter 158 00:06:48,810 --> 00:06:51,360 'docker rm' command, and remove 159 00:06:51,390 --> 00:06:53,730 specific stopped container. As you 160 00:06:53,730 --> 00:06:55,620 see from this diagram, Docker 161 00:06:55,650 --> 00:06:57,330 utilizes the resources of Docker 162 00:06:57,330 --> 00:07:00,240 Host very efficiently. First it 163 00:07:00,240 --> 00:07:02,670 reuses files on the hard drive. 164 00:07:03,090 --> 00:07:05,190 There is no need to copy a file 165 00:07:05,190 --> 00:07:07,440 system layers from specific image 166 00:07:07,470 --> 00:07:10,500 when you create new containers. For 167 00:07:10,530 --> 00:07:12,690 every new container, Docker creates 168 00:07:12,720 --> 00:07:15,270 just new writeable 'fs' layer, 169 00:07:15,930 --> 00:07:18,090 all remaining underlying 'fs' 170 00:07:18,090 --> 00:07:20,610 layers are reused from this image. 171 00:07:21,780 --> 00:07:24,000 And next Docker does not keep a 172 00:07:24,000 --> 00:07:26,550 container running if there are no 173 00:07:26,580 --> 00:07:28,500 processes running inside of the 174 00:07:28,500 --> 00:07:30,780 container. It immediately stops 175 00:07:30,780 --> 00:07:32,670 container when there are no 176 00:07:32,670 --> 00:07:35,670 processes running. Alright, let's 177 00:07:35,670 --> 00:07:38,070 now go to terminal and let's remove 178 00:07:38,100 --> 00:07:39,750 containers that we have created 179 00:07:39,750 --> 00:07:42,060 before. Basically picture in our 180 00:07:42,060 --> 00:07:44,190 case is like this, there are no 181 00:07:44,190 --> 00:07:46,200 containers running, but there are 182 00:07:46,230 --> 00:07:49,260 still two containers on hard drive, 183 00:07:49,350 --> 00:07:51,150 but those containers are stopped. 184 00:07:51,630 --> 00:07:53,760 Let's go to terminal. And here 185 00:07:53,790 --> 00:07:55,650 again, list containers that were 186 00:07:55,650 --> 00:07:57,780 stopped. There are no containers 187 00:07:57,810 --> 00:08:01,350 running. And now let's use 'docker 188 00:08:01,380 --> 00:08:03,720 rm' command in order to remove 189 00:08:03,750 --> 00:08:06,480 stopped container. And let's first 190 00:08:06,510 --> 00:08:08,460 remove this container. Let's take 191 00:08:08,490 --> 00:08:11,010 its ID, and let's remove it like 192 00:08:11,010 --> 00:08:13,230 so. And let's remove second 193 00:08:13,230 --> 00:08:15,510 container, but let me use alternative 194 00:08:15,510 --> 00:08:17,220 version of this command, 'docker 195 00:08:17,580 --> 00:08:21,180 container rm'; and you're also able 196 00:08:21,180 --> 00:08:23,940 to use a randomly generated name in 197 00:08:23,940 --> 00:08:25,710 order to perform some actions with 198 00:08:25,710 --> 00:08:27,690 container. Let me copy this name 199 00:08:27,750 --> 00:08:30,450 and paste here as argument for 'rm' 200 00:08:30,450 --> 00:08:33,630 command; and second container was 201 00:08:33,630 --> 00:08:36,030 removed as well. And now there are 202 00:08:36,058 --> 00:08:38,220 no containers that are currently 203 00:08:38,220 --> 00:08:41,909 running or stopped. Great. But I 204 00:08:41,909 --> 00:08:44,640 still have image, 'docker images', 205 00:08:45,210 --> 00:08:47,490 on the list, hello-world image and 206 00:08:47,520 --> 00:08:49,860 busybox image. It means that now 207 00:08:49,890 --> 00:08:52,409 picture is following. There are no 208 00:08:52,500 --> 00:08:55,590 'fs' layers for containers, but there 209 00:08:55,590 --> 00:08:58,320 is still image on hard drive with 210 00:08:58,350 --> 00:09:00,480 corresponding 'fs' layers. Basically, 211 00:09:00,480 --> 00:09:01,860 there are two different images at 212 00:09:01,860 --> 00:09:03,960 the moment. And you're able of 213 00:09:03,960 --> 00:09:06,690 course, remove images as well using 214 00:09:06,720 --> 00:09:10,110 'docker rmi' command. And here you 215 00:09:10,110 --> 00:09:13,380 could type either IMAGE ID or NAME 216 00:09:13,410 --> 00:09:14,910 of the image in order to remove it. 217 00:09:15,210 --> 00:09:19,560 Let's remove busybox image. Deleted. 218 00:09:19,860 --> 00:09:22,080 And let's remove hello-world image. 219 00:09:22,710 --> 00:09:26,640 hello-world. hello-world image was 220 00:09:26,670 --> 00:09:28,980 removed as well. And now if I'll 221 00:09:28,980 --> 00:09:30,750 have a look at the list of images, 222 00:09:31,080 --> 00:09:33,330 I will see empty list. And that 223 00:09:33,330 --> 00:09:34,830 means that at the moment, there 224 00:09:34,830 --> 00:09:38,280 are no images on hard drive, on 225 00:09:38,280 --> 00:09:40,590 local Docker Host. That's how 226 00:09:40,620 --> 00:09:42,390 picture looks like at the moment. 227 00:09:43,230 --> 00:09:45,030 Alright guys, I hope you enjoyed 228 00:09:45,030 --> 00:09:46,680 this lecture. And basically that's 229 00:09:46,680 --> 00:09:48,360 all for this section, where I 230 00:09:48,360 --> 00:09:50,610 have made small introduction into 231 00:09:50,640 --> 00:09:53,160 images and containers. And next let 232 00:09:53,160 --> 00:09:54,930 me briefly summarize what we have 233 00:09:54,930 --> 00:09:56,910 covered in this section. I'll see 234 00:09:56,910 --> 00:09:57,870 you next. Bye-Bye. 235 00:09:57,870 --> 00:10:00,000 [no audio]