Networking Between Multiple Docker-Compose Projects
Mục Lục
Networking Between Multiple Docker-Compose Projects
An app of microservices across repos and docker-compose files can get messy when you want to run the project locally.
Networking Between Multiple Docker-Compose Projects
Setting up a development environment can always be challenging, especially with microservice architectures. In my last project, I split a large monolith into microservices across different code repositories and wound up with several isolated docker-compose files.
I wanted to test the full app on my local machine by routing everything within docker’s network. How can I get the different services to connect all in docker but running in their separate docker-compose project directories?
I am no expert, but I discovered some interesting concepts by playing with docker-compose files and services.
Docker Networks
Docker has its subsystem of networks. When starting Docker, it creates a bridge network. This network has every service connected but does not directly connect the containers unless specified. You can also create and specify your own bridge network.
Docker-compose services outlined in the same YAML file will always be on the same bridge network created and reachable by their service name. I wanted to use this so my different services across multiple docker-compose files could reach each other by their service name.
My test directory. Each project is in its own directory to be sure there are different networks created.
Manually Creating the Docker Network
We can set up our docker network and direct the docker-compose services to use the network we created.
We do this like:
create the network and check it exists
Now to tell both of them to use this docker network
We do this by declaring the network in the docker-compose files as external
Declare the network as external
This tells docker-compose not to create a default network and use our pre-existing network instead. Let’s ping the other project’s IP address — we can retrieve this using docker inspect [container]
. Then let’s send it a request using the IP address and service name to prove Docker is still resolving it on the same network.
pinging and sending requests to the other docker-compose project
Making it Easier
Docker usually makes the default network using the naming scheme of the directory and _default
. We can declare it external in the second project and not touch the docker-compose file of the first.
just use the name of the default network from the first project
Here are the StackOverflow posts that helped a ton!
Tools To Help
docker network create
docker network ls
docker inspect [container name]
netcat
TL;DR
- connecting multiple docker-networks can get tricky
- by either creating a network or using the default network created, we can connect them using
external
in the filename
If you liked what you read, follow me on LinkedIn or right here on Medium 🙂