4. Docker network

  • Create a new docker network and connect both containers to that network

  • Containers on the same network can use the others container name to communicate with each other

4.1. Adapters

../../_images/docker-networking.png

Figure 48. Docker network

4.1.1. Bridge

4.1.2. Host

  • Best when the network stack should not be isolated from the Docker host, but you want other aspects of the container to be isolated

4.1.3. Overlay

  • Best when you need containers running on different Docker hosts to communicate, or when multiple applications work together using swarm services

4.1.4. Macvlan

  • Best when you are migrating from a VM setup or need your containers to look like physical hosts on your network, each with a unique MAC address

4.2. Create network

4.2.1. Automatic address

$ docker network create mynetwork

4.2.2. Specific address

$ docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 mynetwork

4.2.2.1. List networks

$ docker network ls

4.2.2.2. Delete network

$ docker network rm mynetwork

4.2.2.3. Connect running container to network

$ docker run -d --name host1 -it alpine sh
$ docker run -d --name host2 -it alpine sh

$ docker network create mynetwork
$ docker network connect mynetwork host1
$ docker network connect mynetwork host2

4.2.2.4. Connect new container to network

$ docker network create mynetwork
$ docker run -d --net mynetwork --name host1 -it alpine sh
$ docker run -d --net mynetwork --name host2 -it alpine sh
$ docker exec host1 ping -c4 host2
$ docker exec host1 ping -c4 host2

4.2.2.5. Inspect network

$ docker network inspect