1 00:00:02,440 --> 00:00:10,110 ‫Requirements for this lecture are that you've seen the previous network lectures and are understanding 2 00:00:10,140 --> 00:00:14,830 ‫of the key concepts for Docker networks and the commands that go with it. 3 00:00:14,850 --> 00:00:15,150 ‫All right. 4 00:00:15,150 --> 00:00:22,140 ‫And this lecture, you're going to learn and play with DNS and how it affects containers in custom networks 5 00:00:22,170 --> 00:00:23,970 ‫and default networks. 6 00:00:24,180 --> 00:00:25,350 ‫We're going to go over 7 00:00:25,350 --> 00:00:32,580 ‫quickly how important it is that we have good DNS because you can't rely on IP addresses inside containers 8 00:00:32,940 --> 00:00:38,490 ‫since things are so dynamic. We'll look at the differences between default and custom networks in 9 00:00:38,490 --> 00:00:40,890 ‫regards to how they deal with DNS. 10 00:00:41,160 --> 00:00:48,000 ‫And then we'll check out the --link option for container run for enabling DNS on the default 11 00:00:48,000 --> 00:00:48,860 ‫bridge network. 12 00:00:50,270 --> 00:00:55,640 ‫So there's one more thing that's crucial to all of these containers and virtual networks and them talking 13 00:00:55,640 --> 00:01:02,480 ‫to each other; and that's naming. Because in the world of containers constantly launching and disappearing 14 00:01:02,510 --> 00:01:08,030 ‫and moving and expanding and shrinking, and all the wonderfulness of these micro services that we're 15 00:01:08,030 --> 00:01:14,560 ‫seeing crop up everywhere, is that we no longer can easily rely on IP addresses as the way to talk 16 00:01:14,570 --> 00:01:18,710 ‫from one thing to the other. Because we can't assume from minute to minute that the IP addresses are even 17 00:01:18,710 --> 00:01:19,920 ‫going to be the same. 18 00:01:19,970 --> 00:01:24,650 ‫The container might go away or it might fail and then Docker brings it up somewhere else. 19 00:01:24,920 --> 00:01:29,400 ‫It's just too dynamic and too complicated to deal with that. 20 00:01:29,460 --> 00:01:37,650 ‫So it turns out that there is a built-in solution for this, and that is DNS naming. Docker uses the 21 00:01:37,650 --> 00:01:43,370 ‫container names as the equivalent of a host name for containers talking to each other. 22 00:01:43,380 --> 00:01:48,450 ‫Now if you remember we just had two containers running... 23 00:01:51,060 --> 00:01:57,690 ‫if you remember the Nginx was on a new network, called my_app_net. 24 00:01:57,690 --> 00:01:59,780 ‫Right. 25 00:01:59,940 --> 00:02:04,120 ‫And has the one container on it. Because I created this new network, 26 00:02:04,140 --> 00:02:06,060 ‫that's not the default bridge network, 27 00:02:06,060 --> 00:02:13,080 ‫it actually gets a special new feature, which is automatic DNS resolution for all the containers on that 28 00:02:13,080 --> 00:02:18,520 ‫virtual network from all the other containers on that virtual network using their container names. 29 00:02:18,540 --> 00:02:24,900 ‫If I were to create a second container on that virtual network, they'll be able to find each other, regardless 30 00:02:24,900 --> 00:02:28,280 ‫of what the IP address is, with their container names. 31 00:02:28,410 --> 00:02:38,340 ‫So let's try this: docker container run name ... and we'll call it my nginx...and we're going to specify the 32 00:02:38,340 --> 00:02:42,380 ‫network as my app net 33 00:02:42,420 --> 00:02:45,570 ‫from the nginx image. 34 00:02:45,570 --> 00:02:52,980 ‫Now, if we look at that network, we should see 2 containers; and, if I do a docker container exec, 35 00:02:54,270 --> 00:03:00,800 ‫from the new container I just created -- oops we also need the IT in there -- 36 00:03:02,900 --> 00:03:08,150 ‫And we do a ping to the new nginx, 37 00:03:11,940 --> 00:03:19,170 ‫you notice that DNS resolution just works. This makes it super easy for you to have one container and 38 00:03:19,170 --> 00:03:24,660 ‫you need to set a configuration file in it to talk to, maybe let's say the PHP server will talk to 39 00:03:24,660 --> 00:03:30,810 ‫the MySQL backend. Ctrl-c out of that, and you'll see that it works the other way as well. 40 00:03:30,840 --> 00:03:42,180 ‫If I jump into the new nginx container and I try to ping the my ngnix, you see that the resolution 41 00:03:42,180 --> 00:03:43,440 ‫works both ways. 42 00:03:43,440 --> 00:03:49,320 ‫And this is what solves a huge problem when you're spinning up containers because you can't predict 43 00:03:49,770 --> 00:03:54,900 ‫how long they're going to last and where they might be a minute from now in a production design where 44 00:03:54,900 --> 00:03:57,900 ‫you've got a cluster of Docker Swarm servers. 45 00:03:57,900 --> 00:04:03,120 ‫So it may not change very much on your local machine, but if you stop 3 or 4 containers, and then 46 00:04:03,120 --> 00:04:06,730 ‫you start the same containers, and you start them in a different order, 47 00:04:06,780 --> 00:04:12,450 ‫they may not have the same IP address. But their host names, or their container names, will always be the 48 00:04:12,450 --> 00:04:13,320 ‫same. 49 00:04:13,320 --> 00:04:15,390 ‫And so you can rely on them. 50 00:04:15,410 --> 00:04:23,990 ‫Now I should note, if we do a docker network list, the default bridge network has one disadvantage here. 51 00:04:23,990 --> 00:04:28,140 ‫It does not have the DNS server built into it by default. 52 00:04:28,370 --> 00:04:31,360 ‫So you can use the --link. 53 00:04:31,460 --> 00:04:39,110 ‫So when you create a container, you'll notice there's a link option and you can actually specify manual 54 00:04:39,110 --> 00:04:42,740 ‫links between containers in that default bridge network. 55 00:04:42,740 --> 00:04:48,230 ‫But really, it's just much easier to create a new network for your apps so that you don't have to do 56 00:04:48,230 --> 00:04:49,570 ‫this every time. 57 00:04:49,820 --> 00:04:55,550 ‫And in a future section, when we talk about Docker Compose, you'll see how so much of this gets even easier 58 00:04:55,910 --> 00:05:02,240 ‫because Compose automatically will create new virtual networks whenever you spin up an app with it. 59 00:05:02,240 --> 00:05:05,320 ‫So communicating amongst your containers gets even easier. 60 00:05:06,760 --> 00:05:14,500 ‫As a quick recap, we covered in this lecture about how containers can't really, or shouldn't really, 61 00:05:14,650 --> 00:05:19,670 ‫rely on IP addresses for talking to each other because they just can't be relied on. 62 00:05:20,110 --> 00:05:25,600 ‫And that DNS is really the standard here for how we do intercommunication between containers on the 63 00:05:25,600 --> 00:05:28,320 ‫same host and across hosts. 64 00:05:28,450 --> 00:05:33,580 ‫And so I recommended you always create custom networks since it's just easier that way than doing a 65 00:05:33,580 --> 00:05:35,490 ‫--link all the time. 66 00:05:35,740 --> 00:05:39,550 ‫And then I gave you a teaser about Docker Compose and how it's going to make all this easier, 67 00:05:39,700 --> 00:05:40,900 ‫especially the networking.