DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Developer's Guide to Mastering Docker Networking Concepts
  • Auto Remediation of GuardDuty Findings for a Compromised ECS Cluster in AWSVPC Network Mode
  • Manage Microservices With Docker Compose
  • Smart Deployment Strategies for Modern Applications

Trending

  • LLM Integration in Enterprise Applications: A Practical Guide
  • Dear Micromanager: Your Distrust Has a Job; It’s Just Not the One You’re Doing
  • What Is Lambda Architecture? Ultimate Guide to Getting Started
  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Connect Two Containers From Different docker-compose Files

How to Connect Two Containers From Different docker-compose Files

Containers from different docker-compose files can be in the same network, as long as you have the correct settings. Here's how to set up your network properly.

By 
Denis Chikurtev user avatar
Denis Chikurtev
·
Jan. 12, 22 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
7.0K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Code Sample

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

YAML
 
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: user_app/docker-compose.yml

YAML
 
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 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 the following name:  database_app_common_network. This is a very important point —  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. 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:

networks:
- common_network

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. 

networks:
- database_app_common_network


Conclusion

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.

Docker (software) Network

Published at DZone with permission of Denis Chikurtev. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Developer's Guide to Mastering Docker Networking Concepts
  • Auto Remediation of GuardDuty Findings for a Compromised ECS Cluster in AWSVPC Network Mode
  • Manage Microservices With Docker Compose
  • Smart Deployment Strategies for Modern Applications

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook