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
You didnt specify the network. With
hostname: webservice-server
you just setting the host name inside thewebapp
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 usenetworks: default aliases
Your
docker-compose.yml
file is missing aversion:
line and aservices:
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 inservices:
:You do not need to declare
networks:
,container_name:
, orhostname:
. In a version 2 or 3 Compose file, Compose sets these things up for you, and the Compose service names likewebapp
will be usable as host names to call between containers. Networking in Compose in the Docker documentation has further details.