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

7.1. Adapters


Figure 7.1. Docker network

7.1.1. Bridge

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

7.1.3. Overlay

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

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

7.2. Create network

7.2.1. Automatic address

$ docker network create mynetwork

7.2.2. Specific address

$ docker network create -d bridge --subnet --gateway mynetwork List networks

$ docker network ls Delete network

$ docker network rm mynetwork 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 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
$ docker exec host1 ping -c4 host2 Inspect network

$ docker network inspect