Networking with Docker-Compose: A Simple Ping Example

Graham Bryan
2 min readSep 23, 2020

--

TL;DR The networking capabilities Docker-Compose abstracts for docker users has significantly helped my mental state and should help yours too! Stop using bash scripts and hardcoding IP addresses for your docker container network. It leads to hacky solutions and can be prone to errors.

Docker-Compose creates a single network for which all of its services can communicate over. This feature makes docker-compose an incredibly powerful tool. You will no longer have the self induced headache of remembering the IP address’ for all the containers you launched or creating your own docker network from scratch.

In this article, Ill show you a simple example of this awesome feature docker and docker-compose gives us.

For the example, I have put together a custom docker image based off the latest ubuntu with iputils-ping installed. The Dockerfile looks like this:

FROM ubuntu
RUN apt-get update && \
apt-get install -y iputils-ping
CMD bash

Below is a docker-compose.yml file I have put together in order to demonstrate the network. There are two services, service-1 and service-2, that both spawn from the image, my-image, based off the Dockerfile above.

The application of each service is to try and ping the other 3 times, ping <service name> -c 3.With docker-compose, it will automagically create a single network under the hood for you and map the containers IP directly to the service name so it can be resovled. Once you launch the services with docker-compose up the following is the result:

Results of running docker-compose up

As you can see, the ping was successful! Both services were able to resolve and ping the other by the service name itself. Otherwise, we would need to know what the IP was for each container that was launched in order to do this simple execution.

To see more about the networking services docker-compose has see their documentation. This feature can easily be applied to more complex systems where messaging between services is essential. This makes the complicated network setup and tracking much easier.

--

--

Graham Bryan
Graham Bryan

Written by Graham Bryan

I love applying software engineering and devops to modernize space applications!

No responses yet