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?
The previous posts information is correct, but it does not have details on how to link containers, which should be connected as "external_links".
Hope this example make more clear to you:
Suppose you have app1/docker-compose.yml, with two services (svc11 and svc12), and app2/docker-compose.yml with two more services (svc21 and svc22) and suppose you need to connect in a crossed fashion:
svc11 needs to connect to svc22's container
So the configuration should be like this:
this is app1/docker-compose.yml:
this is app2/docker-compose.yml:
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.
...
They can then talk to each other using the service name. From
front
you can doping api
and vice versa.Since Compose 1.18 (spec 3.5), you can just override the default network using your own custom name for all Compose YAML files you need. It is as simple as appending the following to them:
Other answers have pointed the same; this is a simplified summary.
Just a small adittion to @johnharris85's great answer, when you are running a docker compose file, a "
default
" network is created so you can just add it to the other compose file as an external network:...
For me this approach was more suited because I did not own the first docker-compose file and wanted to communicate with it.
All containers from
api
can join thefront
default network with following config:See docker compose guide: using a pre existing network (see at the bottom)
I would ensure all containers are
docker-compose
'd to the same network by composing them together at the same time, using: