getting UnknownHostException when calling an API on another container using the container name

i’ve no idea why this isn’t working so very much need your help.

this is my docker-compose:

version: "3.3"

services:
  service1:
    restart: always
  service2:
    restart: always
    expose:
      - "8080"
    ports:
      - "8080:8080"

service1 has integration tests that needs to call the API on service2. and i’m using com.avast.gradle.docker-compose plugin to make sure docker is initialized before the tests are run. so this is what i have in my build.gradle for service1:

dockerCompose {
    useComposeFiles = ["$project.rootDir/docker-compose.yml"]
    forceRecreate = true
    isRequiredBy(project.tasks.getByPath(':service1:test'))
}

the integration test is something like this:

String url = String.format("http://%s:8080/status", "service2");
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json");

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
    HttpResponse response = httpClient.execute(httpGet);
}

but this is the exception i’m getting:

java.net.UnknownHostException: service2
        at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)

any idea what is going on?

The forum software queued your post for review. Did you also ask this elsewhere? Then please at least add links to the other post and back here.

hello! i did post on stackoverflow here: docker: getting UnknownHostException when calling an API on another container using the container name - Stack Overflow

but no luck yet.