Network Drivers are built-in or 3rd part extensions that give your virtual network features.
https://docs.docker.com/engine/reference/commandline/network_ls/
To see what docker networks are currently active on your system, use the following command:
docker network ls
https://docs.docker.com/engine/reference/commandline/network_inspect/
To display detailed information on one or more networks:
docker network inspect {name/id}
https://docs.docker.com/engine/reference/commandline/network_create/
To spawn a new virtual network for you to attach containers to:
docker network create {name}
By default, the new network will use the “bridge” driver, but you can change drivers using the
--driver
flag (-d
for short):
docker network create -d {driver}
https://docs.docker.com/engine/reference/commandline/network_connect/
If you already have a container created and you want to assign that container to an existing network:
docker network connect {network_id} {container_id}
This dynamically creates a NIC in this container on an existing virtual network.
https://docs.docker.com/engine/reference/commandline/network_disconnect/
To remove a container from a network:
docker network disconnect {network_id} {container_id}
docker network rm {name/id}
This removes a network from your docker engine.
Docker daemon has a built-in DNS server that containers use by default. The container names are equivalent to host names when containers communicate to each other. This eliminates the frustration of having dynamic IP addresses, because the host names will always stay the same.
Lets say you have two containers:
CONTAINER_ID | IMAGE | PORTS | NAMES |
---|---|---|---|
946662f14859 | nginx | 80/tcp, 443/tcp | new_nginx |
d78dd2559775 | nginx | 0.0.0.0:80/tcp, 443/tcp | webhost |
And you have a custom network that you created:
docker network create my_network
And the two containers are both connected to that custom network:
docker network connect my_network new_nginx
docker network connect my_network webhost
You will be able to communicate between the two containers freely inside that network. You can check by pinging one container from another:
docker container exec -it new_nginx ping webhost
You can also check via the opposite direction:
docker container exec -it webhost ping new_nginx
Even when their IP addresses change, you will always be able to connect from one to the other using their container names.