Basics of Networking | Networking in Docker #2
Tags: docker, networking, devops, cloud
Published: 2021-12-08 15:51:01

> Understand the core IP network concepts useful for container networking

---

Link to video: https://www.youtube.com/watch?v=BnAozPAMXnc

---

What is an IP address or a network port? How do they relate to a network interface? 🤔

If you've ever struggled with networking concepts, this blog is for you.

# Introduction

The purpose of this blog is to go over some basic networking topics, which will be specially useful to master networking for Docker containers.

If you're confident with your networking know-how, you can skip this blog and go directly to the next one.

But if you have no or very limited networking knowledge, or want to revise the core concepts, read on!

Here's what we will discuss:

- Network Interfaces,
- Internet Protocol (IP),
- Public vs Private networks,
- IP addressing,
- Subnet mask and CIDR,
- Network Ports,
- DNS

NOTE: We will refer to computers/devices in a network as hosts.

To recap from [part 1](https://www.techmormo.com/posts/docker-networking-1-why-is-networking-important/)...

> A network is formed when a group of devices is connected over a medium to share data.

But how do hosts connect to a network?

# Network Interfaces

A *network interface* is the point of interconnection between a host and a network.

It can either be physical (like the [one](https://en.wikipedia.org/wiki/Network_interface_controller) below) or virtual (emulated using software).


![network-interface-card by Michael Schwarzenberger from pixabay](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rc7o64tfntnvi8ztf7aw.jpg)

---

A local network (e.g. [LAN](https://en.wikipedia.org/wiki/Local_area_network)) can be formed merely by connecting multiple hosts together (generally by using hardware devices like switches or hubs).

This eventually results in many local networks where communication between hosts is possible but only within that local network.

But how would one transmit data across networks?

# Internet Protocol (IP)

> IP (or [Internet protocol](https://en.wikipedia.org/wiki/Internet_Protocol)) is a standard protocol for transmitting packets of data between hosts across networks, connected to form an IP network.

A host can send data to another one by specifying the destination host's IP address on the data packet. The packet of data can then be routed to the correct host via one or more routers.

A router is a special-purpose device with two or more network interfaces, connecting two or more networks, which allows it to forward data packets from one network to another.

Thus, IP allows us to form a network of networks i.e. internetwork.

## Public vs private networks


![public-vs-private-network-representation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mmkjwvqd8krh6p3jdo6.png)

A network is said to be _public_ if anyone can access it from outside. The Internet is the best and probably the only pure example of such a network.

A _private network_ is one to which access is restricted. Local networks like those in a company or school are examples of private networks.

Private networks offer security by not exposing hosts directly to the Internet.

## IP addresses

Each host in an IP network can have one or more IP addresses, depending on how many network interfaces it has.

An IP Address is a numeric expression for identifying a particular host on a particular network.

`10.2.0.10` is an example of an IP address.

32 bits are used to represent the address, so both addresses below are equivalent - the top one being decimal while the bottom one is binary.

```bash
   10   .   2    .   0    .  10
00001010.00000010.00000000.00001010
```

Since 8 bits are used to create each of the four-dot (`.`) separated segments, the value of each segment can range from 0 to 255.

## Subnets masks & CIDR

Since an IP network can span multiple smaller networks (subnets), there needs to be a way to specify the network a host belongs to in order to route data packets to it.

The IP address of a host is often specified as `10.2.0.10/24`, where `/24` specifies the part of the address that belongs to the subnet i.e. the subnet mask. This is also called the [CIDR notation](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing).

For example, the IP `10.2.0.10/24` uses the first 24 bits to identify the network, whereas the rest of the 8 bits are used to identify hosts within that network. This allows 2^8 or 256 different IP addresses (practically fewer as some IPs are reserved).

The following shows the minimum and maximum IP address values for this subnet.

```bash
min: 00001010.00000010.00000000.00000000
max: 00001010.00000010.00000000.11111111
```

Therefore, the larger the subnet mask (i.e. the bigger the number after `/`), the fewer the hosts that subnet can accommodate.

---

Okay, we now know how data is transmitted to the correct network and host using IP.

But a host could be running many different applications. How can one host reach a particular application on a destination host?

# Network Ports

> _A **network port** is a logical endpoint for specifying the correct host application/process being communicated with._

A port is _identified for each [Transport Layer](https://en.wikipedia.org/wiki/Transport_layer) protocol and IP address combination by a 16-bit unsigned number._ The two most prominent Transport Layer protocols are [User Datagram Protocol](https://en.wikipedia.org/wiki/User_Datagram_Protocol) (UDP) and [Transmission Control Protocol](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) (TCP).

This means, for a host with a given IP address, there can be up to 65535 ports available for TCP and UDP each. Each application (or networked service) running on the host could then use zero or more of the ports that are available.


![analogy-of-ports](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx3fbkjgumz8var1rklk.png)

Port numbers from 0 to 1023 are called well-known or system ports and are reserved for standard networked services, like TCP port 80 for HTTP, TCP port 443 HTTPS, etc.

---

Just one more networking concept to remember...

# Domain name system

The [Domain name system](https://en.wikipedia.org/wiki/Domain_Name_System) (DNS) was invented to translate domain names to IP addresses (and vice versa), as it is difficult for us humans to remember IP addresses.

At the simplest level, DNS records are domain name to IP address mappings, like:

```bash
google.com    172.217.194.100
```

Records like these (and more complex ones) are distributed across multiple levels of _name servers_, which serve to provide this domain name to IP address translation.

Your host machine keeps a local cache of recently queried DNS records and asks name servers when it doesn't find the requested domain record locally.

You can check the IP addresses for `google.com` by typing this in your terminal:

```bash
nslookup google.com
```

---

# Conclusion

In this blog, we briefly discussed - network interfaces, the Internet Protocol, public vs private networks, IP addresses, subnets, network ports, and DNS.

If we keep these concepts in mind, Docker container networking will be much easier to understand and apply.

Thanks for making it so far! 🎉

See you on the next one.

Till then…

Be bold and keep learning.

But most importantly,

Tech care!