I'm trying to resolve the IP addresses of some docker compose services from another container within the docker compose network running a C/C++ socket program.
Here is my docker-compose.yml
version: "3.9"
services:
grad_calc_1:
image: worker:1
ports:
- "8080:8080"
environment:
- seed=10
grad_calc_2:
image: worker:1
ports:
- "8081:8080"
environment:
- seed=20
optimizer:
image: optimizer:1
depends_on:
- grad_calc_1
- grad_calc_2
Here is my code resolving the hostnames from the optimizer
service
char* resolve_host(const char* host_name) {
struct hostent *host_entry;
char *IPbuffer;
host_entry = gethostbyname(host_name);
IPbuffer = inet_ntoa(*((struct in_addr*)host_entry->h_addr_list[0]));
return IPbuffer;
}
int main() {
const char* hostname_1 = "grad_calc_1";
const char* hostname_2 = "grad_calc_2";
char* ip_1 = resolve_host(hostname_1);
char* ip_2 = resolve_host(hostname_2);
cout << "grad_calc_1 IP: " << ip_1 << endl;
cout << "grad_calc_2 IP: " << ip_2 << endl;
}
The output is 172.19.0.2
for both hostnames. I'm not sure what I'm doing wrong