Home > Net >  java.net.UnknownHostException: Name or service not known
java.net.UnknownHostException: Name or service not known

Time:03-19

My docker-compose file:

webapp:
    container_name: webapp
    hostname: webservice-server
    build:
      context: .
      dockerfile: ServerDockerfile // ServerDockerfile starts tomcat
    ports:
      - "8080:8080"

webservices_test_client:
    build:
      context: .
      dockerfile: TestsDockerfile
    depends_on:
      - webapp

When I try to send request to "http://webservice-server:8080/" via Apache HttpClient from tests in container webservices_test_client I am getting UnknownHostException.

HttpGet httpGet = new HttpGet("http://webservice-server:8080/");
HttpResponse httpResponse = httpClient.execute(httpGet);

What's wrong? Thanks for answers.

CodePudding user response:

Your docker-compose.yml file is missing a version: line and a services: wrapper. This makes it an obsolete version 1 Compose file that doesn't set up Docker networking for you. Very current tools will have trouble running it because they'll interpret the file as the fourth-generation Compose specification.

At the top level, add a version: declaring what version to use (I tend to use the most recent non-"specification" version 3.8) and wrapping the declarations you have now in services::

version: '3.8'
services:
  webapp:
    build:
      context: .
      dockerfile: ServerDockerfile
    ports:
      - "8080:8080"

  webservices_test_client:
    build:
      context: .
      dockerfile: TestsDockerfile
    depends_on:
      - webapp

You do not need to declare networks:, container_name:, or hostname:. In a version 2 or 3 Compose file, Compose sets these things up for you, and the Compose service names like webapp will be usable as host names to call between containers. Networking in Compose in the Docker documentation has further details.

CodePudding user response:

You didnt specify the network. With hostname: webservice-server you just setting the host name inside the webapp container. You should have a network so that diffrent container could see each other. If you don't want to create network for each container, just use networks: default aliases

  • Related