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


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 --gateway mynetwork List networks

$ docker network ls Delete network

$ docker network rm mynetwork 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 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 Inspect network

$ docker network inspect