6. 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

  • Bridge - multiple containers to communicate on the same Docker host

  • Host - network stack should not be isolated from the Docker host

  • Overlay - containers running on different Docker hosts to communicate

  • Macvlan - your containers will look like physical hosts on your network

6.1. Adapters

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

Figure 6.1. Docker network

../../_images/docker-stack-05-kubernetes.png
../../_images/docker-stack-06-architecture.png

Bridge:

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

Overlay:

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

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

6.2. Create network

Automatic address:

$ docker network create mynetwork

Specific address:

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

6.3. List networks

$ docker network ls

6.4. Delete network

$ docker network rm mynetwork

6.5. 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

6.6. Connect new container to network

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

6.7. Inspect network

$ docker network inspect