skip to Main Content

I have B application which calls A application.

Here are the source code for them:

A application :

@SpringBootApplication
@RestController
@Slf4j
public class AApplication {

    public static void main(String[] args) {
        SpringApplication.run(AApplication.class, args);
    }

    @GetMapping("call_a")
    public double callA() {
        log.info("calling a");
        return Math.random();
    }
}

with the content of application.properties being :

server.port=6601

B application :

@SpringBootApplication
@RestController
@Slf4j
public class BApplication {

    public static void main(String[] args) {
        SpringApplication.run(BApplication.class, args);
    }

    @GetMapping("call_b")
    public String callB() {
        RestTemplate restTemplate = new RestTemplate();

        log.info("calling b");
        log.info("calling inner a");
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:6601/call_a", String.class);
        log.info("called inner a and got {}", responseEntity.getBody());
        return responseEntity.getBody();
    }
}

with the content of application.properties being :

server.port=6602

Not introducing docker, if I call call_b endpoint from B application it returns successfully.

$ curl http://localhost:6602/call_b
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    18  100    18    0     0     61      0 --:--:-- --:--:-- --:--:--    610.7818520533385139

But now if I introduce the application inside docker I cannot access them:

Here it is the Dockerfile for both applications:

FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Here it is the docker-compose.yaml to start them both using docker-compose up.

version: '3.7'
services:

  cA:
    image: gigi/a:latest
    ports:
      - "6601:6601"

  cB:
    image: gigi/b:latest
    extra_hosts:
      - "localhost:host-gateway"
      - "127.0.0.1:host-gateway"
      - "0.0.0.0:host-gateway"
    ports:
      - "6602:6602"

It starts them both.

If I enter the B container and just call the call_a endpoint it works:

$ curl http://localhost:6601/call_a
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    18    0    18    0     0    135      0 --:--:-- --:--:-- --:--:--   1350.7802319051705794

But if I call the call_b endpoint, either inside the B container or outside it will give me:

$ curl http://localhost:6602/call_b
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   107    0   107    0     0    548      0 --:--:-- --:--:-- --:--:--   551{"timestamp":"2022-07-13T14:38:01.395+00:00","status":500,"error":"Internal Server Error","path":"/call_b"}

And If I look at the logs for the B application I see:

cB_1  | 2022-07-13 14:58:24.787  INFO 1 --- [nio-6602-exec-1] com.example.b.BApplication               : calling b
cB_1  | 2022-07-13 14:58:24.787  INFO 1 --- [nio-6602-exec-1] com.example.b.BApplication               : calling inner a
cB_1  | 2022-07-13 14:58:24.829 ERROR 1 --- [nio-6602-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:6601/call_a": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)] with root cause
cB_1  |
cB_1  | java.net.ConnectException: Connection refused (Connection refused)
cB_1  |         at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
cB_1  |         at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) ~[na:na]
cB_1  |         at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
cB_1  |         at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
cB_1  |         at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
cB_1  |         at java.base/java.net.Socket.connect(Socket.java:558) ~[na:na]
cB_1  |         at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182) ~[na:na]
cB_1  |         at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474) ~[na:na]
cB_1  |         at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569) ~[na:na]
cB_1  |         at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:na]
cB_1  |         at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341) ~[na:na]
cB_1  |         at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362) ~[na:na]
cB_1  |         at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1253) ~[na:na]
cB_1  |         at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187) ~[na:na]
cB_1  |         at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081) ~[na:na]
cB_1  |         at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1015) ~[na:na]
cB_1  |         at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:361) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at com.example.b.BApplication.callB(BApplication.java:27) ~[classes!/:0.0.1-SNAPSHOT]
cB_1  |         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
cB_1  |         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
cB_1  |         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
cB_1  |         at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
cB_1  |         at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.21.jar!/:5.3.21]
cB_1  |         at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar!/:5.3.21]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1787) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.64.jar!/:na]
cB_1  |         at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]

If you could point me to some documentation to find out, why I couldn’t access the other docker container, it would be great?

I do not have the liberty to replace localhost with something else.


Edit 1

I can’t modify the source code for the callB application, meaning the localhost:6601/call_a shouldn’t be modified.

2

Answers


  1. Chosen as BEST ANSWER

    I was looking for this answer:

    The cA application does not expose any port, but it is being set to use the network_mode and the cB application has access to the cA application using localhost.

    version: '3.7'
    services:
    
      cA:
        image: gigi/a:latest
        network_mode: "service:cB"
    #    ports:
    #      - "6601:6601"
    
      cB:
        image: gigi/b:latest
        ports:
          - "6602:6602"
    

  2. Because you use localhost inside container b, it means you try to send this rest call inside environment of container b.

    I saw you are using docker-compose, so container b is in the same network of container a.

    Instead of use localhost, try to use the name of the container A (restTemplate.getForEntity("http://cA:6601/call_a&quot;, String.class);)

    Let me know if you can reach it.

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