How to configure Port Mapping and Docker Networking
A Docker host comprises multiple Docker containers and hence the networking has become a crucial component for realizing composite containerized applications. Docker containers also need to interact and collaborate with as remote ones to come out with distributed applications. In this guide, you will learn how to configure Networking and Port Mapping in Docker.
Docker has three default networks:
- Bridge
- Host
- None
$ docker network ls$ docker network inspect bridge
Docker Networking : Bridge
When you start Docker, a default bridge network (also called bridge) is created automatically, and newly started containers connect to it unless otherwise specified.
Bridge networking provides private internal IPs to all containers and they are isolated from host. Port forwarding forwards outside traffic to the containers. On the default bridge network, containers can only access each other by IP addresses, unless you use the link option, which is considered legacy.
You can also create user defined custom bridge network. User defined bridge networks are superior to the default bridge network. On a user defined bridge network, containers can resolve each other by name or alias(DNS).
You can create a bridge network with below command,
$ docker network create --driver bridge my_net
A container can be attached to newly created network.
$ docker run -d --name web --net my_net nginx
Understanding DNS resolution in bridge network
When containers run in default bridge network they cannot find each other using their container names. Simply putting DNS resolution through container names will not work under default bridge network.
In the below example, 2 containers are created under the default bridge network. If we try to ping to second container from the first container using the second container’s name didn’t resolve (DNS server is not available under default bridge network).
$ docker run -d -it --name container1 centos
$ docker run -d -it --name container2 centos
$ docker exec -it container1 bash Now you are inside container1, you can try to ping container2. # ping container2
ping: container2: Name or service not know
Here, both containers are unable to ping each other.
Now we will create a new bridge network and will attach containers to this network. In this case, containers find each other using their container names( DNS resolution through container names).
$ docker network create -d bridge my_network
$ docker run -d -it --net my_network --name container1 centos
$ docker run -d -it --net my_network --name container2 centos
$ docker ps $ docker exec -it container1 bash Now you are inside container1, you can try to ping container2. # ping container2
Here, containers are able to ping each other.
Docker Networking : Host
In host network, all containers directly get connected to host. Multiple containers cannot run on same hosts because of
port conflicts on host side.
$ docker run -d --name web --net host nginx
Docker Networking : None
This offers a container specific network stack that lacks a network interface. Containers run in pure isolation. This container only has a local loopback interface (i.e., no external network interface).
$ docker run -d --name web --net none nginx
Docker Networking: Port Mapping
Port forwarding enables access to applications running inside containers from outside world.
$ docker -p <host-port>:<docker-port> image
Here, we pulled nginx image from the docker hub. We will try to access nginx on host port.
$ docker run -d -p 8080:80 nginx
Docker Networking: Overlay
Bridge networks apply to containers running on the same Docker daemon host. For communication among containers running on different Docker daemon hosts, we should use an overlay network which spans across the entire cluster.
Hence, in this guide you learned how to configure Port Mapping in Docker Networking.
Read Also : An Advanced Overview of Docker
Originally published at https://thecodecloud.in on October 17, 2020.