# Container networking: bridges, namespaces and Pods

See how Linux namespaces and veth pairs connect containers, how Docker networks provide isolation, and where Kubernetes CNI fits.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 11. About 24 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/11-container-networking

## What you will learn

- Explain what a network namespace isolates
- Trace a container packet across a veth pair and host bridge
- Use a Docker network alias and inspect a container route
- Distinguish the Kubernetes Pod network from a Service

> **Lab status.** **Verified lab:** a user-defined Docker bridge network, an Nginx container, and an Alpine client ran briefly. `getent hosts` resolved the network alias, `ip` showed the client's interface and route, and an HTTP response came from the container. The script removes its container and network on exit. <br />**Cluster lab:** none. Kubernetes networking is explained with a diagram; no cluster was created.

Before containers, each application needed its own machine or a carefully managed shared host. Containers made
processes lighter, but those processes still needed separate addresses and a path to other services. Linux
namespaces isolate network state; virtual Ethernet links connect that state to a host bridge. Docker and Kubernetes
build higher-level networks from those parts. In this lesson, you will inspect a Docker bridge and see where CNI
fits in Kubernetes.

## Network namespaces

> **Figure.** A container network namespace owns eth0 and its routes. A veth pair connects it to the host bridge docker0, which forwards traffic toward the host NIC.

A **network namespace** is an isolated view of network interfaces, addresses, routes, firewall rules, and sockets.
It is not a separate kernel. A process in the namespace sees its own `eth0` and route table, while the host has
another view of the connected virtual interface.

A **veth pair** is like a virtual cable with two ends. One end can live in the container namespace; the other can
attach to a Linux bridge on the host. The bridge forwards frames between its ports. The host then routes traffic
outside the bridge and may translate addresses. That last step depends on host configuration.

## veth pairs and bridges

Docker's bridge driver creates an isolated bridge network. Containers on the same user-defined network can reach
each other and use network aliases. Published ports create a path from a host port to a container port; peer
containers do not need published ports just to communicate on that network.

```text
$ getent hosts api
172.19.0.2        api  api
$ ip -br addr
lo               UNKNOWN        127.0.0.1/8 ::1/128
eth0@if109       UP             172.19.0.3/16
$ ip route
default via 172.19.0.1 dev eth0
172.19.0.0/16 dev eth0 proto kernel scope link src 172.19.0.3
api service
```

These lines came from the temporary lab. The last request used the address returned by `getent`; the numeric
address changes between networks. A frequent mistake is publishing a database port to the host even though only
peer containers need it. Keep the service on a private bridge instead.

## Docker networks

> **Figure.** Two containers named web and api share a user-defined bridge network. Docker name lookup returns the peer address, and peers can connect without a published host port.

Use a named network to make the boundary visible and repeatable:

```sh
docker network create app-net
docker run -d --name api --network app-net --network-alias api nginx:alpine
docker network inspect app-net
```

This example starts a short-lived Nginx service. Remove it and the network after testing. Do not use the legacy
default bridge as a substitute for explicit service discovery; behavior differs from a user-defined network.

## Pod networking and CNI

> **Figure.** A container shares its Pod network namespace and Pod IP. A CNI plugin configures the Pod interface; the cluster network connects Pods, while a Kubernetes Service provides a stable virtual address.

Kubernetes groups containers into a **Pod**. Containers in one Pod share a network namespace and one Pod IP, so
they can reach each other over `localhost` using different ports. A **Container Network Interface (CNI)** plugin
sets up the Pod's interface and the cluster network. The implementation varies by plugin and environment.

A **Service** is a separate abstraction. It gives clients a stable virtual address and selects ready Pods behind
it. CNI provides connectivity; it does not, by itself, define the Service API. This lesson uses diagrams instead
of a cluster lab so the exercise stays local and lightweight.

## Mission: Inspect a Docker bridge

Create one user-defined network, resolve an alias, inspect the client's route, and request the service by its resolved address. Remove the container and network when done.

- You need: Docker
- You'll have: The resolved alias, the client route, and a response from the service

```sh
bash devops-academy-tools/networking-lab/11-capture.sh
```

The script records the client and service output in `devops-academy-tools/networking-lab/outputs/11-container-networking.txt`.
Its exit trap removes the service container and network.

## Try it

- Create a named Docker bridge network and attach two temporary containers.
- Inspect one container with `ip -br addr` and `ip route`.
- Resolve a network alias with `getent hosts` and request the resolved address.
- Remove every container and the network when finished.

## Quiz

1. What does a Linux network namespace give a container?
   - **A separate set of interfaces, routes and sockets** (answer)
   - A dedicated physical NIC
   - A public IP address by default
   - A separate kernel
   Why: A network namespace isolates network devices, routes, firewall rules and sockets while sharing the host kernel.

2. What does a veth pair do?
   - Encrypts a TCP connection
   - **Connects two virtual Ethernet interfaces, often across namespaces** (answer)
   - Resolves DNS names
   - Creates an IP route automatically on every host
   Why: A veth pair behaves like a virtual cable; each linked endpoint can live in a different network namespace.

3. What is the role of a Kubernetes CNI plugin?
   - **Configure Pod network interfaces and connectivity** (answer)
   - Store container images
   - Choose application log format
   - Replace Kubernetes Services
   Why: A CNI plugin implements the Pod network setup; Services provide a separate stable access abstraction.

## Resources

- [Bridge network driver](https://docs.docker.com/engine/network/drivers/bridge/) (Docker Docs)
- [Network Plugins](https://kubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/) (Kubernetes Docs)
- [Services, Load Balancing, and Networking](https://kubernetes.io/docs/concepts/services-networking/) (Kubernetes Docs)
