How to connect two containers from different docker-compose files
Introduction and Problem Setting
Working with Docker containers allows developers to create encapsulated applications that are independent of the host machine and contain all the necessary libraries and dependencies. In practice, docker-compose is often used to configure and manage containers. When several containers are built in a docker-compose file, they are automatically connected to a common network (which is created by default) and can communicate with each other.
In most cases, however, each project will have its own docker-compose file. In such configurations, the containers from one docker-compose will not be able to connect to those from the other unless we have previously created and configured a shared network. In such cases it is necessary to use a docker networking in compose. In this article, we’ll take a look at a sample method/example, how to set up networks in different docker-compose files, so that individual projects and the containers/services in them can be connected in a single network, when running on a local machine.
Let’s look at the following scenario: we have two projects that are containerized. We have separate docker-compose files for building the projects. Our goal is to create a docker-compose network in which both projects are connected, with all the containers in them.
To achieve this goal, we propose three main steps to be performed:
- defining a common network in the docker-compose of one project;
- setting the network to connect to in the other project’s docker-compose;
- setting up the network for all services we want to be connected.
We will now explain in detail what each of the steps is and give examples of how to implement them.
Let’s look at two projects, one is containing a PostgreSQL database and the other is an application that uses that database. The folder name of the first project is database_app and the other is user_app. Docker-compose files have the following code.
File 1: database_app/docker-compose.yml
File 2: secondary_app/docker-compose.yml
Step 1 To create a custom network in the docker-compose, you need to add it to the compose file of the database_app project. The creation of the network is on lines 15 and 16.
After building with docker-compose, a network will be created with following name: database_app_common_network. This is a very important point, because in order to be able to connect other containers to this network, they need to know its whole name.
So we already have our custom network to which we can connect other compose projects.
Step 2 To connect the other project to the first one we need to enter the name of the network to which to connect to. Here, it is important to mention that this network is external to the current docker-compose. To connect to the network from step 1, we add lines 9,10 and 11.
In this way the second project will connect to the network of the first, but the last step remains.
Step 3 To be able to connect to the common network, all services defined in the docker-compose files of both projects must know which one it is. Therefore, to each service from the first project, the following commands must be added:
Thus the defined services in the compose file will connect to our network . This must be done for each container we want to connect to the network, otherwise it will only be connected to the default network for the current docker-compose file.
For the second project in the docker services we also enter the network separately, using its full name, as in step 2.
In this post we looked at some features of docker-composer networking and showed how we can configure a network so that different projects can be linked.
If you have any questions about the article or want to discuss the topic, do not hesitate to contact us at
— — —
We put a lot of effort in the content creation in our blog. Multiple information sources are used, we do our own analysis and always double check what we have written down. However, it is still possible that factual or other mistakes occur. If you choose to use what is written on our blog in your own business or personal activities, you do so at your own risk. Be aware that Perelik Soft Ltd. is not liable for any direct or indirect damages you may suffer regarding the use of the content of our blog.
Author: Denis Chikurtev