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

Command

Purpose

docker network ls

Show all the available networks. Check that the bridge network is running

Start your containers as normal, with docker run. When you start each container, Docker will add it to the bridge network.

(If you prefer, you can be explicit about the network connection by adding --net=bridge to the docker run command.)

docker inspect <container_id> | grep IPAddress

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

docker network inspect bridge

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.

  1. Create a UDF bridge network

  2. docker network create <network name>

  3. Start a container and connect it to the bridge

  4. docker run --rm --net <network name> --name <container name> -d <image name>

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