Question: I have a `docker-compose` file with several service-container definitions. One of the services communicates with `Apache Kafka` within the same docker-compose run.

So I have the kafka docker definition like this:

```
  kafka:
    image: spotify/kafka
    ports:
      - "2181:2181"
      - "9092:9092"
    environment:
      ADVERTISED_HOST: 127.0.0.1
      ADVERTISED_PORT: 9092

```

I have my service definition in the same docker-compose file. In the startup script of the service I have to figure out somehow the IP address of the Kafka instance.

I know, I can use something like `docker inspect` to find out which IP address is used by a container. 

But how can I do it dynamically in a docker-compose environment?

**EDIT**

So, the right configuration should be (thank you, @nwinkler):

```
kafka:
image: spotify/kafka
ports:
  - "2181:2181"
  - "9092:9092"
environment:
  ADVERTISED_HOST: kafka
  ADVERTISED_PORT: 9092

myservice:
image: foo
links:
  - kafka:kafka

```

Don't forget to set the `ADVERTISED_HOST` to `kafka` (or how you named your kafka container within `docker-compose`).

Answer: 
You can use the [Docker Compose Links](https://docs.docker.com/compose/compose-file/#/links) feature for this. If you provide a link to the `kafka` container from your other container, Docker Compose will ensure that your other container can access the Kafka container through its hostname - you will not have to know its IP address.

Example:

```
kafka:
    image: spotify/kafka
    ports:
      - "2181:2181"
      - "9092:9092"
    environment:
      ADVERTISED_HOST: 127.0.0.1
      ADVERTISED_PORT: 9092

myservice:
    image: foo
    links:
      - kafka:kafka

```

This will allow your `myservice` container to access the Kafka container through the `kafka` hostname. So from your `myservice` container, you can do something like `curl http://kafka:9092` to access the service on the Kafka container.

Docker-Compose does this through DNS, it creates a hostname/IP mapping in your container allowing you to access the container without knowing its IP address.
The ip of your container will be the ip you are looking for.

Append the port number (9092 in your case) to the ip of the container to get whatever kafka is serving.