Welcome back to Docker Bootcamp. In this post, we’ll revisit the topic of linking multiple containers. We’ll discover how to link containers using Docker’s network features that we’ve discussed in previous posts.
In part one, we used the link flag to connect two containers. The link flag is considered a legacy feature and could eventually be removed. The link flag was handy in that it did not require you to expose the source container to the network. All communication happened in the background. The source container shares all its environment variables with any linked containers.
This can lead to security issues if any environment variables contain secrets such as passwords. Linking containers with the link flag relies on the name of the containers and the order in which they are started. Source containers must be started before any containers that link to them. It becomes increasingly difficult to manage container links as the number of containers grows.
We can achieve similar results by connecting containers to the same network. Containers on the same network can communicate with each other while being isolated from containers on other networks. By default, all newly created containers are connected to Docker’s default bridge network. Therefore, it is best to create a user-defined network to have specific control over which containers can communicate. This is especially important in production environments.
Containers on a user-defined network are able to reference each other by container name. Containers on the default bridge network are only able to reference each other by IP address. Containers can be connected and disconnected from user-defined networks while running. To disconnect from the default bridge the container must be stopped and created with different network options. Containers linked in this way do not share environment variables, but you can achieve this by using volumes or Docker compose (which I will discuss in a future post).
Examples
- Create two user-defined networks with defined subnets
- docker network create –subnet 192.168.100.0/24 mynet1
- docker network create –subnet 192.168.200.0/24 mynet2
- Verify the networks are created
- docker network ls
- Create two containers attached to mynet1
- docker create -it –network mynet1 –name link1 busybox /bin/sh
- docker create -it –network mynet1 –name link2 busybox /bin/sh
- Create one container attached to mynet2
- docker create -it –network mynet2 –name link3 busybox /bin/sh
- Start all three containers
- docker start link1 link2 link3
- Inspect network details for mynet1
- docker network inspect mynet1
- Notice the Containers sections lists link1 and link2
- docker network inspect mynet1
- Inspect network details for mynet2
- docker network inspect mynet2
- Notice the Containers section lists link3
- docker network inspect mynet2
- Attach a terminal to link 1
- docker attach link1
- Ping the containers
- ping link1
- We get ping results from the container itself
- ping link2
- We get ping results from a container on the same network
- ping link3
- We do not get results from a container on another network
- ping link1
- Connect container link3 to network mynet1
- docker network connect mynet1 link3
- Inspect network details for mynet1
- docker network inspect mynet1
- Notice the Containers section now includes link3 as well as link1 and link2
- docker network inspect mynet1
- View container metadata for link3
- docker inspect link3
- Notice the Networks section lists mynet1 and mynet2
- docker inspect link3
- Ping the containers
- ping link3
- We are now able to get results from link3 as it is on the same network as link1
- ping link3
- Disconnect container link3 from mynet1
- docker network disconnect mynet1 link3
- Inspect network details for mynet1
- docker network inspect mynet1
- Notice the Containers section no longer lists link3
- docker network inspect mynet1
- View container metadata for link3
- docker inspect link3
- Notice the Networks section no longer lists mynet1
- docker inspect link3
- Ping the containers
- ping link3
- We do not get results from link3 as it is no longer on the same network as link1
- ping link3
- Stop the containers
- docker stop link1 link2 link3
- Remove the containers
- docker rm link1 link2 link3
- Remove the networks
- docker network rm mynet1 mynet2
Using the networking features of Docker gives us more flexibility, security, and isolation of our containers.
I am really learning a lot from this blogs of your. These are handpicked topics by you and finding really helpful for cheatsheet of docker commands.
Thanks Eric.
Thanks Rakesh!! Glad you are finding them helpful!