1

I have two docker compose files, for example backend and monitoring. I need to create communication between them. It is possible using same network, and docs telling me to create network in one file and then use

external: true 

in another. But I want possibility to run only backend or only monitoring. And of course I want them to communicate if they both are running.

I can create the network with

docker network create some_network

and then run one or another, or both of them but are there some better ways?

DISCO
  • 135
  • 12
  • It sounds like you're aware of [Communication between multiple docker-compose projects](https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects); what are you asking that's different from that question? Some of the answers there suggest that, if both Compose files declare the same `networks: { default: { name: } }` then neither needs to be flagged "external"; does that work? – David Maze Jul 12 '23 at 11:12
  • Then I have the warning message tells that something like one network has already been created, consider to use ```external: true``` – DISCO Jul 12 '23 at 17:55

1 Answers1

0

You should:

  1. Create a network.
  2. Assign both containers to it.
  3. Use the container name/id as hostname to mutual reference them.

Edit:

Due your comment, I propose you the following scenario:

  1. You have 3 yaml files: network creation, container A creation, container B creation.
  2. Container A and B yaml files address the network defined in network creation yaml
  3. Invoke docker compose by docker-compose build -f network.yaml -f container-a.yaml -f container-b.yaml . .

That's all!

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • So creating network by hand is the only way? I am aware, that developers will forget about it. I have the option of writing script ofc to start app with network creation, but I am looking for easier ways, if they exist. – DISCO Jul 12 '23 at 08:15
  • By the YAML you can create a network in a simple way, don't warry: https://docs.docker.com/compose/networking/ . – Antonio Petricca Jul 12 '23 at 08:16
  • yeah, but the doc tells to create network in one of two compose files, I have not the two containers but two compose files. – DISCO Jul 12 '23 at 08:21
  • In this case I edit the post for you. – Antonio Petricca Jul 12 '23 at 08:22