title: How to access localhost from within docker image
body: <p>I have B application which calls A application.</p>
  <p>Here are the source code for them:</p>
  <p><strong>A application</strong> :</p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootApplication
  @RestController
  @Slf4j
  public class AApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(AApplication.class, args);
      }
  
      @GetMapping(&quot;call_a&quot;)
      public double callA() {
          log.info(&quot;calling a&quot;);
          return Math.random();
      }
  }
  </code></pre>
  <p>with the content of <code>application.properties</code> being :</p>
  <pre><code>server.port=6601
  </code></pre>
  <p><strong>B application</strong> :</p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootApplication
  @RestController
  @Slf4j
  public class BApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(BApplication.class, args);
      }
  
      @GetMapping(&quot;call_b&quot;)
      public String callB() {
          RestTemplate restTemplate = new RestTemplate();
  
          log.info(&quot;calling b&quot;);
          log.info(&quot;calling inner a&quot;);
          ResponseEntity&lt;String&gt; responseEntity = restTemplate.getForEntity(&quot;http://localhost:6601/call_a&quot;, String.class);
          log.info(&quot;called inner a and got {}&quot;, responseEntity.getBody());
          return responseEntity.getBody();
      }
  }
  
  </code></pre>
  <p>with the content of <code>application.properties</code> being :</p>
  <pre><code>server.port=6602
  </code></pre>
  <p>Not introducing <code>docker</code>, if I call <code>call_b</code> endpoint from B application it returns successfully.</p>
  <pre><code>$ 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
  </code></pre>
  <p>But now if I introduce the application inside docker I cannot access them:</p>
  <p>Here it is the <code>Dockerfile</code> for both applications:</p>
  <pre><code>FROM openjdk:11
  ARG JAR_FILE=target/*.jar
  COPY ${JAR_FILE} app.jar
  ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;/app.jar&quot;]
  </code></pre>
  <p>Here it is the <code>docker-compose.yaml</code> to start them both using <code>docker-compose up</code>.</p>
  <pre><code>version: '3.7'
  services:
  
    cA:
      image: gigi/a:latest
      ports:
        - &quot;6601:6601&quot;
  
    cB:
      image: gigi/b:latest
      extra_hosts:
        - &quot;localhost:host-gateway&quot;
        - &quot;127.0.0.1:host-gateway&quot;
        - &quot;0.0.0.0:host-gateway&quot;
      ports:
        - &quot;6602:6602&quot;
  </code></pre>
  <p>It starts them both.</p>
  <p>If I enter the <code>B container</code> and just call the <code>call_a</code> endpoint it works:</p>
  <pre><code>$ 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
  </code></pre>
  <p>But if I call the <code>call_b</code> endpoint, either inside the <code>B container</code> or outside it will give me:</p>
  <pre><code>$ 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{&quot;timestamp&quot;:&quot;2022-07-13T14:38:01.395+00:00&quot;,&quot;status&quot;:500,&quot;error&quot;:&quot;Internal Server Error&quot;,&quot;path&quot;:&quot;/call_b&quot;}
  
  </code></pre>
  <p>And If I look at the logs for the <code>B application</code> I see:</p>
  <pre><code>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 &quot;http://localhost:6601/call_a&quot;: 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.&lt;init&gt;(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]
  
  </code></pre>
  <p><strong>If you could point me to some documentation to find out, why I couldn't access the other <code>docker container</code>, it would be great</strong>?</p>
  <p>I do not have the liberty to replace <code>localhost</code> with something else.</p>
  <hr />
  <p><strong>Edit 1</strong></p>
  <p>I can't modify the source code for the <code>callB</code> application, meaning the <code>localhost:6601/call_a</code> shouldn't be modified.</p>