Skip to content

Instantly share code, notes, and snippets.

@thaJeztah
Last active August 22, 2019 09:50
Show Gist options
  • Save thaJeztah/e4f6adff5bb1d9678557611ea1ff5afc to your computer and use it in GitHub Desktop.
Save thaJeztah/e4f6adff5bb1d9678557611ea1ff5afc to your computer and use it in GitHub Desktop.
Connecting to services in different compose projects

Cross-compose network connections

When running a compose project, services are accessible both through their full name (including the project-name prefix, for example, myproject_web_1), and through their service name (as specified in the compose-file), for example web. The short name is a network-scoped alias, which means that any container connected to the same network can access the container through this name.

By default, docker-compose creates a network for each compose project (projectname_default) so that all services in the compose project can communicate. Because that network is created for each project individually, two compose project don't share the same network, and their services are isolated from other compose projects.

It's possible, however, to make compose projects (or individual services in a compose project) share the same network.

Example 1 - using a shared network for the whole project

The following compose files specify a custom name for the default network; both compose-files use the sharednet network as the default, which means that services for both compose project will be connected to the same network:

Compose file 1 (compose1.yml):

version: '3.5'
services:
  compose1service:
    image: busybox
    tty: true

networks:
  default:
    name: sharednet

Compose file 2 (compose2.yml):

version: '3.5'
services:
  compose2service:
    image: busybox
    tty: true

networks:
  default:
    name: sharednet

To illustrate this:

Start both compose files:

docker-compose -f compose1.yml --project-name=compose1 up -d
docker-compose -f compose2.yml --project-name=compose2 up -d

Using the short (compose2service) name to ping the compose2service from inside the service in the compose1service container works;

docker exec compose1_compose1service_1 ping -c1 compose2service
PING compose2service (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: seq=0 ttl=64 time=0.134 ms

--- compose2service ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.134/0.134/0.134 ms

And vice-versa:

docker exec compose2_compose2service_1 ping -c1 compose1service
PING compose1service (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.151 ms

--- compose1service ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.151/0.151/0.151 ms

As does the full name (including the project prefix);

docker exec compose2_compose2service_1 ping -c1 compose1_compose1service_1
PING compose1_compose1service_1 (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.151 ms

--- compose1_compose1service_1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.151/0.151/0.151 ms

Example 2 - using a shared network for some service in the project

In this example, both compose projects have their own ("default") network, but in addition, a shared network is added to allow some services to connect to services in the other compose project.

Compose file 1 (compose3.yml):

version: '3.5'
services:
  compose3service:
    image: busybox
    tty: true
    networks:
      - default
      - sharednet

  compose3otherservice:
    image: busybox
    tty: true



networks:
  sharednet:
    name: mysharednetwork

Note: if you specify which networks a service should be connected to, you override the defaults, which means that the service is no longer automatically connected to the default network. Include the default network in the list of networks to allow the service to communicate with other services in the compose project.

Compose file 2 (compose4.yml):

version: '3.5'
services:
  compose4service:
    image: busybox
    tty: true
    networks:
      - default
      - sharednet

  compose4otherservice:
    image: busybox
    tty: true


networks:
  sharednet:
    name: mysharednetwork

Start both compose files:

docker-compose -f compose3.yml --project-name=compose3 up -d
docker-compose -f compose4.yml --project-name=compose4 up -d

Again, pinging from the compose3service to the compose4service service in the other compose project works (either by short name or full name);

docker exec compose3_compose3service_1 ping -c1 compose4service
PING compose4service (172.22.0.3): 56 data bytes
64 bytes from 172.22.0.3: seq=0 ttl=64 time=0.110 ms

--- compose4service ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.110/0.110/0.110 ms


docker exec compose3_compose3service_1 ping -c1 compose4_compose4service_1
PING compose4_compose4service_1 (172.22.0.3): 56 data bytes
64 bytes from 172.22.0.3: seq=0 ttl=64 time=0.093 ms

--- compose4_compose4service_1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.093/0.093/0.093 ms

However, trying the same to connect to the compose4otherservice will fail, because that service is not connected to the mysharednetwork network:

docker exec compose3_compose3service_1 ping -c1 compose4otherservice
ping: bad address 'compose4otherservice'

docker exec compose3_compose3service_1 ping -c1 compose4_compose4otherservice_1
ping: bad address 'compose4_compose4otherservice_1'

Note:

Care should be taken if two compose projects have a service with the same name (for example, both have a service named web). In such situations, web may at random resolve to the web service from either project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment