skip to Main Content

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.

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search