How to connect two container from different compose files

docker
Reading Time: 3 minutes

Working with Docker containers allows us to create encapsulate applications that are independent of the host machine and contain all the necessary libraries and dependencies. Docker-compose is often use to configure and manage containers. When several containers are build in a docker-compose file, they are automatically connect to a common network and can communicate with each other. 

In most cases, each project will have its own docker-compose file. For 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 docker networking in compose. In this blog, we’ll take a look at a sample example, how to set up networks in different docker-compose files. So that individual projects and the containers in them can be connect in a single network when running on the local machine.

How to connect a docker-compose network ?

Let’s look at the following scenario: we have two projects that are containerise. We have separate docker-compose files for building the projects. Our goal is to create a docker-compose network in which both projects are linked, with all the containers in them.

To achieve this goal, we propose three main steps to be perform:

  • 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 connect.

Code:

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 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

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:

File 2: secondary_app/docker-compose.yml

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

Step 1: To create a custom network in docker-compose, you need to add it to compose file of the database_app project.

After building with docker-compose, a network will create with the following name:  database_app_common_network. This is important because, in order 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. Here, it is important to mention that this network is external to the current docker-compose. To connect to the network from step 1.

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 a common network, all services defined in docker-compose files of both projects must know which one it is. Therefore, to each service from the first project, the following commands must add:

networks:
- common_network

Thus the defined services in the compose file will connect to our network. This must happen for each container we want to connect to the network, otherwise, it will only be connect 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. 

networks:
- database_app_common_network

Conclusion

In this blog, we look at some features of docker-composer networking. Docker bridge network for connecting different projects and containers of docker compose

Reach out to this link for more DevOps blogs: blog.knoldus.com

knoldus

Written by 

I am reliable, hard-working with strong attention to detail and eager to learn about new technologies and business issues. I am able to work well both on my own initiative and as part of a team.

1 thought on “How to connect two container from different compose files4 min read

Comments are closed.