Prerequisite Docker Networking
- Example is a server with
Dockerand aneth0interface of192.168.1.10. - When a container is
run, different networking options are available, such as:docker run --network none nginx - With the
nonenetwork, the container is not attached to any network and cannot reach the outside world. Likewise no one from the outside world can reach the container. - If you run multiple containers (for example running the above command multiple times), they cannot talk to each other or the outside world.
- Next is the
Hostnetwork - the container is attached to theHost’s network.- No network isolation between the Host and the container.
- Can make a web application available on port 80.
- The command for that is
docker run --network host nginx. - No need for additional port mapping.
- If you try to bring up another container that uses the same port, this won’t work (for example running the above command again).
- Third networking option is the
Bridgenetwork - an internal private network on the host is created. TheDockerhost and containers attach to this.- By default, the network has an IP range of
172.17.0.0 - Each device that connects receives its own internal private address.
- How does
Dockercreate and manage the network?- This
bridgenetwork (intentionally has a lowercaseb) is created by default whenDockeris installed on the host. - On the host itself, this
bridgenetwork is seen asdocker0. This can be seen from the output of theip linkcommand. - When installing
Docker, it essentially runsip link add docker0 type bridgeon the host OS. - When checking the network on the host with
ip link, you’ll see that thedocker0interface is in aDOWNstate. - The
bridgenetwork is like an interface to the host - but a switch to the namespaces or containers within the host. - The interface
docker0on the host is assigned an IP of172.17.0.1/24 - When a container is created,
Dockercreates a network namespace for it. - To list the namespace, run the
ip netnscommand. - There is a hack required to get the
ip netnscommand to list the namespaces created byDocker. - For example,
ip netnsoutputs the following:b3123<numbers-letters> - If you inspect a
Dockercontainer, for exampledocker inspect 324ghj, you would see the above namespace as part of a larger string of alphanumeric charactersin theSandboxIDandSandboxKeysections. - You can see this if you run
docker network ls
- This
- By default, the network has an IP range of
- How does
Dockerattach the container from its network namespace to the bridge network? - As said before, the container has two network interfaces on each end.
- Running the
ip linkcommand shows the interface attached to thedocker0interface. - Run the same command again with
ip -n b3123<numbers-letters> link(-nis for showing namespaces).- The output then shows the other end of the interface in the container namespace, for example
eth0@if8. - The container is also assigned an IP, which can be viewed with the
ip -n b3123<numbers-letters> addr. In this example, the container is assigned an IP of172.17.0.3.- Can also view this same information by attaching to the container and seeing the information that way.
- The output then shows the other end of the interface in the container namespace, for example
- Running the
- Essentially:
- Docker creates a namespace and a pair of interfaces.
- One interface is attached to the container.
- The other interface is connected to the
bridgenetwork. - The interface pairs can be identified by their numbers - odd and even.
9 and 10,7 and 8,11 and 12as examples of paired interfaces.
- Now to focus on Port Mapping
- For example I want to bring up an
nginxweb server on port 80.- In the current
bridgesetup, only other containers on the host or the host itself, can access thenginxcontainer. - For example, using a simple
curl http://172.17.0.3:80will view the webpage - Trying the same
curlcommand outside of the host will not work.
- In the current
- For example I want to bring up an
- In order to allow external users access,
Dockerprovides a port publishing option.- When running containers, map port
8080on the host to port80on the container. - Example command:
docker run -p 8080:80 nginx- Any traffic to port
8080on theDockerhost, will be forwarded to port80on the container.
- Any traffic to port
curl http://192.168.1.10:8080on theDockerhost’s external interface will then provide access to the container on port 80.
- When running containers, map port
- How does
Dockerforward traffic from one port to another?- A NAT rule is required, an example of how this can be utilised on the host with
iptables:iptables \ -t nat \ -A PREROUTING \ -j DNAT --dport 8080 \ --to-destination 80
- A NAT rule is required, an example of how this can be utilised on the host with
- The rules are appended to the
PREROUTINGchain. - Docker does it the same way:
iptables \ -t nat \ -A DOCKER \ -j DNAT --dport 8080 \ --to-destination 172.17.0.3:80 - Sets the destination IP to include the container as well.
- You can see the rules that
Dockercreates, when you list the rules iniptableswithiptables -nvL -t nat