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