Container to Container Communications
Default bridge
Containers within the same network can communicate with each other. Docker creates a bridge network called the bridge.
default bridge means you have to use IP address. These can change each time an image is run. This is a brittle architecture
Command | Purpose |
---|---|
| Show all the available networks. Check that the bridge network is running Start your containers as normal, with (If you prefer, you can be explicit about the network connection by adding |
| How do you find out the IP address of a Docker container? Once you have the container IP address, one container can Address another container by its IP address |
| How to check if a container is in the bridge network All the containers listed can communicate with each other |
User-defined bridge network
Allows Docker containers to communicate with each other by name.
In a user-defined bridge network, you can be more explicit about who joins the network, and you get an added bonus:
…containers can be addressed by their name or alias.
Create a UDF bridge network
docker network create <network name>
Start a container and connect it to the bridge
docker run --rm --net <network name> --name <container name> -d <image name>
Address another container using its name as the hostname and its port number (the one inside the container, NOT the one exposed to the host)
Can you connect an existing container to a network?
Yes, you can! If you already have a container running, you can to connect it to your new user-defined bridge network without having to restart the container. Use the docker network connect
command.
For example: docker network connect tulip-net mongodb