1

I got two Containers defined in a docker-compose file:

 
 tomcat_webserver_api:
  image: tomcat:8
  volumes:
   - ./API/Docker/API.war:/usr/local/tomcat/webapps/API.war
  ports:
    - "8080:8080"
  depends_on:
   - mysql_database

 tomcat_webserver_anwendung:
  image: tomcat:8
  ports:
   - "8081:8080"
  volumes:
   - ./Anwendung/Docker/Anwendung.war:/usr/local/tomcat/webapps/Anwendung.war
  depends_on:
   - tomcat_webserver_api
  environment:
    API_HOST: tomcat_webserver_api
    API_PORT: 8080

Now i want to access the URL http://tomcat_webserver_api:8080/API/restaurants/Wochentag from Inside the Java Web Application with an HttpURLConnection.

Issue: It returns an 400 Error

java.io.IOException: Server returned HTTP response code: 400 for URL: http://tomcat_webserver_api:8080/API/restaurants/Wochentag

The Code is like that (The Headers are nearly the same when i try to connect to the URL via curl - this works inside the container huh):


    URL api = UriBuilder.fromUri("http://" + "tomcat_webserver_api" + ":" + "8080" +"/API/restaurants/RestaurantSpeisen").build().toURL();

    System.setProperty("http.agent", "curl/7.52.1");

    HttpURLConnection connection = (HttpURLConnection) api.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Host", "localhost");
    connection.setRequestProperty("User-Agent", "curl/7.52.1");
    connection.connect();

    BufferedReader in = new BufferedReader(new             InputStreamReader(connection.getInputStream(), "UTF-8"));

If i try to connect to http://172.20.0.3:8080/API/restaurants/Wochentag i got an 200-ok HTTP Response Code and the JSON-Data.

If i exec the API Container and inspect the logs i can see the 400 GET-Request.

Why is this happen?

http://172.20.0.3:8080/API/restaurants/Wochentag - Works http://tomcat_webserver_api:8080/API/restaurants/Wochentag - Won't Work but not with an 404 Error

Sven Guthe
  • 15
  • 1
  • 6

2 Answers2

4

I have had the same issue as you, apparently underscore are not allowed as virtualhost, try to remove it, for example, use just tomcatwebserverapi, that should fix your problem.

See Can (domain name) subdomains have an underscore "_" in it? for more information about valid letters in hostnames.

Alexander
  • 2,925
  • 3
  • 33
  • 36
0

Please give explicit container names a try:

tomcat_webserver_api:
  image: tomcat:8
  container_name: tomcat_webserver_api
  volumes:
   - ./API/Docker/API.war:/usr/local/tomcat/webapps/API.war
  ports:
    - "8080:8080"
  depends_on:
   - mysql_database

tomcat_webserver_anwendung:
  container_name: tomcat_webserver_app
  image: tomcat:8
  ports:
   - "8081:8080"
  volumes:
   - ./Anwendung/Docker/Anwendung.war:/usr/local/tomcat/webapps/Anwendung.war
  depends_on:
   - tomcat_webserver_api
  environment:
    API_HOST: tomcat_webserver_api
    API_PORT: 8080

The "local only" configuration needs explicit container names to activate Docker's name lookup mechanism. In Swarm mode you wouldn't need to set container names.

gesellix
  • 3,024
  • 28
  • 31