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.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.