Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

We usually refer to Docker Compose using a single docker-compose.yml file. However, we might need to use more than one YAML file and still be able to have the running containers be part of the same network.

In this short tutorial, we'll see how to use a network to connect multiple Docker Compose projects with some docker-compose.yml examples.

2. Using a Network for Multiple Docker Compose Projects

Since Docker Compose introduces networking, we can make our containers aware of an existing network and let them join it.

For example, suppose we want our Redis cache and web application to be part of the same network, but they are two different YAML files.

2.1. Join an Existing Network

First, let's create a network:

docker network create network-example

Then, we can define a reference to that existing network in our YAML templates.

Let's see our Redis definition:

services:
  db:
    image: redis:latest
    container_name: redis
    ports:
      - '6379:6379'
    networks:
      - network-example

networks:
  network-example:
    external: true

Let's also define it for our web application in a different file:

services:
  my_app:
    image: "web-app:latest"
    container_name: web-app
    ports:
      - "8080:8080"
    networks:
      - network-example

networks:
  network-example:
    external: true

2.2. Define a Network in a YAML Template

Similarly, we can also define a network inside a template, for example, our redis_network:

services:
  db:
    image: redis:latest
    container_name: redis
    ports:
      - '6379:6379'
    networks:
      - network

networks:
  network:
    driver: bridge
    name: redis_network

This time, when we set our web application template, we need to refer to the Redis existing network:

services:
  my_app:
    image: "web-app:latest"
    container_name: web-app
    ports:
      - "8080:8080"
    networks:
      - my-app

networks:
  my-app:
    name: redis_network
    external: true

3. Run and Inspect Containers

Finally, we can start or stop our services using up or down commands.

If we create a network within a service definition, we need to start that service first, like the Redis one in our case:

docker-compose -f docker-compose-redis-service.yaml up -d && docker-compose -f docker-compose-my-app-service.yaml up -d

Let's inspect a running container to see the network definition, for example, our Redis service:

docker inspect 5c7f8b28480b

We'll see an entry for the redis_network. We'll get the same output for the web-app inspection:

"Networks": {
    "redis_network": {
        "IPAMConfig": null,
        "Links": null,
        "Aliases": [
            "redis",
            "4d23d918eb2c"
        ],
        "NetworkID": "e122aa15d5ad150a66d87f3145084520bde540447a14a73f446ec6ea0603aba9",
        "EndpointID": "8904a3389d0b20c6785884c702cb6ae1101522af1f99c079067171cbc9ca97e5",
        "Gateway": "172.28.0.1",
        "IPAddress": "172.28.0.2",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "02:42:ac:1c:00:02",
        "DriverOpts": null
    }
}

Similarly, we can inspect the redis_network:

docker network inspect redis_network

Both our services belong to the same network:

"Containers": {
    "5c7f8b28480ba638ce993c6714841265c0a98d746b27205a756936bbe1850ac2": {
        "Name": "redis",
        "EndpointID": "238bb136d634100eccb7677e87ba07eb43f33be1dc320c795685230f04b809f9",
        "MacAddress": "02:42:ac:1c:00:02",
        "IPv4Address": "172.28.0.2/16",
        "IPv6Address": ""
    },
    "8584ce3bb2ab1cd3d182346c67179a3aa5e40c71e806c35cc4ce7ea91cae7902": {
        "Name": "web-app",
        "EndpointID": "9cf0e484e5af1baf968249c312489a83f57a194098a51652c3f6eac19ed0d557",
        "MacAddress": "02:42:ac:1c:00:03",
        "IPv4Address": "172.28.0.3/16",
        "IPv6Address": ""
    }
}

4. Conclusion

In this article, we've seen how to connect multiple Docker Compose services over the same network. We can let them join an existing network or create a network from a service definition.

As always, we can find the source docker-compose.yml files of our examples over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!