1

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.

4
  • is your port open for incoming connections? Commented Mar 18, 2022 at 10:01
  • @JAMSHAID yes, port is open Commented Mar 18, 2022 at 10:06
  • Is what you show here wrapped in a services: block, or is this the entire docker-compose.yml file? Commented Mar 18, 2022 at 10:22
  • @DavidMaze it's entire docker-compose.yml file Commented Mar 18, 2022 at 10:42

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Please, can you explain me? I know, that docker-compose sets up a network for app by default. Containers auto connected to this network. If I am right, my question is as follows: “Why we need explicit create a network, if we have network that was created by default?” Thanks
@Michael because This feature is for later versions of docker-compose. In the version, you are using(V1), you need to specify the network.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.