-1

I'm scaling up several Redis containers using Docker compose and want to find a way to get all of the IP addresses that correspond to a specific hostname in bash. I've got 5 Redis containers up, and all of them have the hostname redis. I tried the command getent hosts redis | awk '{ print $1 }' to hopefully get all of the IP addresses of the redis Docker images, but every time it would return only one of them. Example output of running the command without awk inside one of the Redis containers a few times is below:

bash-4.4# getent hosts redis
172.16.0.7        redis  redis
bash-4.4# getent hosts redis
172.16.0.6        redis  redis
bash-4.4# getent hosts redis
172.16.0.5        redis  redis
bash-4.4# getent hosts redis
172.16.0.6        redis  redis

I want a way to get all of the IP addresses in one command. Does anyone know how to do that in bash?

Edit: My question is different because I'm not trying to get the IP address of the host, I'm trying to get all of the IP addresses that match the hostname by running a bash script inside the container myself.

AndreasKralj
  • 463
  • 4
  • 23
  • Possible duplicate of [How to get a Docker container's IP address from the host?](https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host) – nullPointer Feb 22 '19 at 15:46
  • Nope, mine's different because I'm not trying to get the IP address of the host, I'm trying to get all of the IP addresses that match the hostname by running a bash script inside the container myself. – AndreasKralj Feb 22 '19 at 15:53

1 Answers1

0

I was able to do it using dig redis A | grep redis inside the container. It output all of the IP addresses in addition to some additional information, so now I'll be able to parse them from the output and add them into a bash array.

AndreasKralj
  • 463
  • 4
  • 23