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:
But in the end what I'm looking after is:
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With