skip to Main Content

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?

2

Answers


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

    Login or Signup to reply.
  2. A network is the best way to restrict its communication to the containers itself. You could also open up the ports but this solution is inferior in terms of security to the previous one.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search