Docker : Bridge & Host Networking using Spring Boot Apps

Mohit Talniya
3 min readFeb 4, 2018

Part -1 https://medium.com/@mohittalniya/overview-of-docker-networking-lets-connect-the-containers-39b34249ad91

docker run — net=”bridge” (default)

In the previous post, we saw that Docker has Bridge Network enabled by default. By default all the containers connect to Bridge network.

Let’s inspect the network interfaces on the host machine. Docker creates a bridge named docker0 by default.

$ sudo ip addr show docker0
inspect docker0
inspect all interfaces

As we can see, Docker Host has the IP address 169.254.123.1 on the docker0 network interface.

Let’s download one Docker image of Spring Boot Application from Docker Hub.

$ docker pull mohittalniya/time-server:latest
pull docker image

Run this image by using following command and by mapping host port to the container port as shown.

-d runs the container in the detached mode.

$ docker run -d -p 9080:9080 mohittalniya/time-server

Get the container id by executing docker ps command.

$ docker ps
P.S. I love you!

Get the shell on the running docker container.

$ docker exec -t -i e1327ec010403 bash

Let’s inspect the network interfaces inside this container.

/# ip addr show eth0
container address

As we can see, the container has been allocated 169.254.123.2 IP address on the bridge.

Let’s pull another Docker Image called time-client ($docker pull mohittalniya/time-client) from the Docker Hub. So, that we can witness the communication happening between the two containers over the docker0 bridge. Repeat the above steps and let’s again check the allocated IP address for the container.

The container has been allocated 169.254.123.3 IP address on the bridge.

Let’s try to send the request($curl http://localhost:9090/time) from the time-client container to time-server container using the allocated IP address. time-client requests Date from the time-server. As we can see response is a success, with the returned Date.

docker run — net=”host”

We can run a docker container with network settings set to host. Such a container will share the network stack with the docker host.

Any port opened in the container would be opened on the docker host without requiring the -p or -P docker run option.

Let’s start our time-server in the — host networking mode.

$ docker run -d mohittalniya/time-server — net=host

Inspect the Network interface on the docker and the docker container. We can see that the loopback address in the container is mapped to the loopback address of the host. We can inspect other interfaces to check that the container and the host share same interfaces.

inspect on host
inspect on the container

References:

--

--