How to Connect Containers in Different Docker-Compose Files
Working with Docker enables developers to create isolated application environments that are easy to manage and deploy. Often, projects require multiple containers to interact, and Docker Compose simplifies container management. However, when containers are defined in separate docker-compose files, they cannot communicate by default. In this guide, we’ll show you how to connect containers from different Docker Compose files by setting up a shared network.

Why Containers from Different Docker-Compose Files Need a Shared Network
Each Docker Compose project typically creates its own isolated network for containers to communicate within that specific project. But when containers are defined in separate Compose files, they can’t connect unless they are part of the same network. By creating and defining a custom shared network, you allow containers from different Compose files to communicate effectively, just like they would in a single project.
Steps to Connect Containers from Different Docker-Compose Files
Let’s walk through the process of connecting containers across two separate Docker Compose files. In this example, one project runs a PostgreSQL database, and the other project runs a web application that connects to this database.
Step 1: Define a Common Network in the First Docker Compose File
The first docker-compose.yml file will host the PostgreSQL database and define the custom network. This network will later be used by other projects to allow communication between containers.
Here’s how you define the network in the first file:
version: "3.3"
services:
db_service:
image: db_svc_image
ports:
- "5001:5001"
networks:
- common_network
postgres_db:
image: postgres
ports:
- "5432:5432"
networks:
- common_network
networks:
common_network:
The crucial part is the networks section, where we define the common_network. This network will be available for use in other projects after Docker Compose builds it.
Step 2: Connect the Second Project to the Shared Network
In the second docker-compose.yml file, which contains the web application, you need to reference the shared network defined in the first Compose file. This network is external to the current Compose setup, so it must be declared as external.
Here’s how the second Compose file should look:
version: "3.3"
services:
flask_service:
image: flask_svc_image
ports:
- "5000:5000"
networks:
- database_app_common_network
networks:
database_app_common_network:
external: true
By setting external: true, Docker Compose knows to connect this project to the common_network defined in the first project.
Step 3: Ensure All Services Use the Shared Network
At this point, both projects are aware of the shared network, but you still need to ensure that all services within both Compose files are connected to it. In the first project, you must explicitly define the network for each service that should connect to it:
services:
db_service:
networks:
- common_network
postgres_db:
networks:
- common_network
Similarly, in the second project’s services (like the flask_service), make sure the network is also added:
services:
flask_service:
networks:
- database_app_common_network
This guarantees that all containers from both projects are connected to the shared network and can communicate.
Troubleshooting Tips
- Ensure Network Consistency: Double-check that the network names match exactly in both Compose files. Docker Compose is case-sensitive, and a small typo can break the connection.
- Container Accessibility: Ensure that the required ports are exposed for each container. For example, the PostgreSQL database must expose port
5432for the web application to connect to it. - Leverage ZippyOPS Expertise: If you’re looking for assistance in optimizing your Docker environments, ZippyOPS offers expert consulting and managed services. We specialize in DevOps, Cloud, Automated Ops, Microservices, and Security, helping organizations scale their infrastructure with efficiency and security. Learn more about our services and solutions.
Conclusion
Connecting containers from different Docker Compose files is a simple yet powerful way to enable communication between isolated projects. By creating a shared network and ensuring each project knows how to connect to it, you can link containers seamlessly. This method works for everything from simple databases to complex microservices architectures.
For more insights and tools to manage your Docker and Kubernetes environments, check out our products. If you’re ready to optimize your infrastructure and automate your operations, reach out to us at sales@zippyops.com.



