1

How to get service IP in a dynamic fashion while starting using docker-compose. How to get the IP of service-a which is used as a configuration parameter while starting up docker-compose.yaml

service-a:

service-b:
  depends_on:
    service-a
Rpj
  • 5,348
  • 16
  • 62
  • 122

1 Answers1

0

The recommended way is not to use the IP address in docker-compose but use the service name instead. If you still want an IP for some reason then you can either set Static IP with your container (not recommended) Link: https://stackoverflow.com/a/45420160/970422

Or you get IP by using docker inspect commands. More details on this approach is mentioned here: https://www.freecodecamp.org/news/how-to-get-a-docker-container-ip-address-explained-with-examples/

Examples:

docker exec container-name cat /etc/hosts

docker exec -it container-name /bin/bash
ip -4 -o address

docker network inspect -f \
'{{json .Containers}}' 9f6bc3c15568 | \
jq '.[] | .Name + ":" + .IPv4Address'
Hussain Mansoor
  • 2,934
  • 2
  • 27
  • 40
  • Configuration for service-b expects a URL (say something like https://localhost:8087/api/v1/) to service-a, so running in a standalone environment works as expected. I am not sure how to pass this dynamically for service-b right now, so that I can get similar behavior – Rpj Apr 05 '21 at 04:17
  • 1
    Have you tried `service-a/api/v1`? "service-a" should work by default in docker-compose as all the networking and dns resolution will be handled by docker-compose out of the box. – Hussain Mansoor Apr 05 '21 at 04:21
  • No the above tip doesn't help @Husyn – Rpj Apr 05 '21 at 12:38