Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between multiple docker-compose projects

I have two separate docker-compose.yml files in two different folders:

  • ~/front/docker-compose.yml
  • ~/api/docker-compose.yml

How can I make sure that a container in front can send requests to a container in api?

I know that --default-gateway option can be set using docker run for an individual container, so that a specific IP address can be assigned to this container, but it seems that this option is not available when using docker-compose.

Currently I end up doing a docker inspect my_api_container_id and look at the gateway in the output. It works but the problem is that this IP is randomly attributed, so I can't rely on it.

Another form of this question might thus be:

  • Can I attribute a fixed IP address to a particular container using docker-compose?

But in the end what I'm looking after is:

  • How can two different docker-compose projects communicate with each other?
like image 652
Jivan Avatar asked Jun 29 '16 00:06

Jivan


People also ask

How do you handle multiple Docker compose?

Running Multiple Copies of a Single Compose Project For times when you need multiple copies of environments with the same composition (or docker-compose. yml file), simply run docker-compose up -p new_project_name .

How multiple containers communicate with each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.


Video Answer


1 Answers

You just need to make sure that the containers you want to talk to each other are on the same network. Networks are a first-class docker construct, and not specific to compose.

# front/docker-compose.yml version: '2' services:   front:     ...     networks:       - some-net networks:   some-net:     driver: bridge 

...

# api/docker-compose.yml version: '2' services:   api:     ...     networks:       - front_some-net networks:   front_some-net:     external: true 

Note: Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in, in this case a prefix front_ was added

They can then talk to each other using the service name. From front you can do ping api and vice versa.

like image 149
johnharris85 Avatar answered Oct 31 '22 01:10

johnharris85