0% found this document useful (0 votes)
8 views3 pages

Docker Networking Explained: Types & Use Cases

docker networking
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Docker Networking Explained: Types & Use Cases

docker networking
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Commands :

1. Docker network ls //list networks


When you create a network manually
1. docker network create my-network
2. docker run -d --name backend --network my-network backend-image
3. docker run -d --name frontend --network my-network frontend-image
Both containers are connected to the same custom bridge network (my-network).
They can talk using container names:
What happens if you don’t specify --network my-network
Then both containers go into the default Docker network called bridge (the one
automatically created by Docker when installed).
In the default bridge (the one literally named bridge),
containers cannot reach each other by name — only by IP address. because
Docker’s DNS name resolution doesn’t work in the default bridge network. which
is not practical — IPs change if container restarts).

1-Bridge network (default)


docker run ubuntu
This is the default when you run a container normally.
 Docker creates a private virtual network called bridge (usually docker0).
 Each container gets its own internal IP, like:
 [Link]
 [Link]
 Containers in the same bridge network can talk to each other using
these IPs or container names.
Use case
➡ Perfect for when you have multiple containers (frontend + backend +
database) on the same host and you want them to communicate securely
through Docker’s internal network.
Example:
Your React app container can call your backend using
[Link]
because both are on the same bridge network.

None network
1. Command: docker run ubuntu --network=none
How it works
 The container is isolated from all networks.
 It cannot reach the internet or other containers.
 Only has a loopback interface ([Link]).
Use case
➡ For security or testing — if you want a container that’s totally cut off from the
network.
Example: sandbox environments, malware analysis, etc.
❌ Not useful for frontend/backend communication.

Host network
1. Command: docker run ubuntu --network=host
How it works
 The container shares the same network interface as the host.
 There is no network isolation.
 Ports are shared — so if one container uses port 5000, another can’t use
the same port.
Use case
➡ Useful for performance optimization or when you want to avoid NAT.
But risky — no isolation and can cause port conflicts.
Example:
If your host machine is [Link],
then your container is also accessible directly at [Link]:5000.

What is “Embedded DNS”?


Every Docker container uses a built-in DNS server provided by Docker.
This DNS server:
 Runs at [Link] (inside the container’s network namespace)
 Automatically registers every container name
 Keeps track of each container’s IP inside the same Docker network
So if you have these containers:

Container IP
Name Address
web 172.17.0.
2
mysql 172.17.0.
3
Docker’s embedded DNS automatically maintains that mapping.
Now when web calls mysql, Docker’s DNS answers with the correct IP.
✅ No need to hardcode IPs
✅ Works even if container restarts (new IP)

What is a Network Namespace?


A network namespace is a feature of the Linux kernel.
It allows each container to have its own virtual network stack — completely
isolated.
That means:
 Each container has its own eth0 interface
 Its own IP addresses
 Its own routing table
 Its own iptables (firewall) rules

What happens when you start a container
docker run -d --name backend my-backend-image
Docker tells Linux to:
1. Create a new network namespace — a virtual copy of the Linux network
stack (like giving the container its own tiny network world).
2. Create a virtual Ethernet pair (veth pair):
o One end stays in the host namespace.
o The other end goes inside the container namespace.
3. Connect the host end of that veth to the docker0 bridge.
4. Assign the container an IP address (e.g. [Link]).
5. Start the container process with that network stack.

The result — two separate “worlds”


Each container gets its own isolated network namespace:

Ntwork Interfaces Loopba IP Connected to


namespace ck
Host (Azure VM) eth0, 127.0.0. public IP + Internet +
docker0, 1 [Link] containers
vethX
Backend eth0 127.0.0. [Link] docker0 bridge
container 1
Frontend eth0 127.0.0. [Link] docker0 bridge
container 1

You might also like