title: Axios in a Node.js container on Kubernetes is returning "ECONNREFUSED 127.0.0.1:30561"?
body: <p>Full error message: <code>connect ECONNREFUSED 127.0.0.1:30561 at TCPConnectWrap.afterConnect</code></p>
  
  <p>The axios request is running in a Node.js environment (Next.js), which is where the error occurs, strangely the axios request works perfectly fine when it is being run in the browser. </p>
  
  <p>My component (running in Node.js) that calls axios:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import axios from 'axios'
  import Router from 'next/router'
  import React, { Component } from 'react'
  import { initializeStore } from '~/reducers'
  import { authenticate } from '~/actions/auth'
  import { getCookieByName } from '~/helpers/cookie'
  
  const isServer = typeof window === 'undefined'
  const __NEXT_REDUX_STORE__ = '__NEXT_REDUX_STORE__'
  
  function getOrCreateStore(initialState) {
      // Always make a new store if server, otherwise state is shared between requests
      if (isServer) {
          return initializeStore(initialState)
      }
      // Create store if unavailable on the client and set it on the window object
      if (!window[__NEXT_REDUX_STORE__]) {
          window[__NEXT_REDUX_STORE__] = initializeStore(initialState)
      }
      return window[__NEXT_REDUX_STORE__]
  }
  
  export default App =&gt; {
      return class AppWithRedux extends Component {
          static async getInitialProps(appContext) {
  
              const reduxStore = getOrCreateStore()
              
              appContext.ctx.reduxStore = reduxStore
  
              let appProps = {}
  
              if (typeof App.getInitialProps === 'function') {
                  appProps = await App.getInitialProps(appContext)
              }
  
              const JWT = (isServer ? getCookieByName('JWT', appContext.ctx.req.headers.cookie) : getCookieByName('JWT', document.cookie))
  
              const pathname = appContext.ctx.pathname
  
              //set axios baseURL
              axios.defaults.baseURL = (isServer ? `${appContext.ctx.req.headers['x-forwarded-proto']}://${appContext.ctx.req.headers.host}` : window.location.origin)
  
              //if user has a JWT
              if(JWT){
                  axios.defaults.headers.common['Authorization'] = `Bearer ${JWT}`
                  //get user from API layer
                  reduxStore.dispatch(authenticate())
              } 
  
              
              return {
                  ...appProps,
                  initialReduxState: reduxStore.getState()
              }
          }
  
          constructor(props) {
              super(props)
              this.reduxStore = getOrCreateStore(props.initialReduxState)
          }
  
          render() {
              return &lt;App {...this.props} reduxStore={this.reduxStore} /&gt;
          }
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Specifically <code>reduxStore.dispatch(authenticate())</code></p>
  
  <p>And my actual axios call (using redux thunk), looking at the <code>authenticate</code> method: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import axios from 'axios'
  import { setCookieByName } from '~/helpers/cookie'
  
  const BASE_URL = '/api/auth'
  export const TYPE_REGISTER = 'TYPE_REGISTER'
  export const TYPE_AUTHENTICATE = 'TYPE_AUTHENTICATE'
  
  export const register = values =&gt; (dispatch) =&gt; {
      return axios.post(`${BASE_URL}/register`, values)
          .then(function({data: {token, user}}){
              setCookieByName('JWT', token, 365)
              dispatch({
                  type: TYPE_REGISTER,
                  payload: user
              })
          })
  }
  
  export const authenticate = () =&gt; (dispatch) =&gt; {
      return axios.post(`${BASE_URL}/me`)
          .then(function({data: {user}}){
              dispatch({
                  type: TYPE_AUTHENTICATE,
                  payload: user
              })
          })
          .catch(function(err){
              console.log(err)
              dispatch({
                  type: TYPE_AUTHENTICATE,
                  payload: {}
              })
          })
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm running my local Kubernetes cluster using Docker for Mac, and my Ingress controller is being accessed on <code>http://kludge.info:30561</code>. My domain is mapped from <code>127.0.0.1 kludge.info</code> locally to allow the Ingress controller to hit the container. My theory is that when I send a request to <code>http://kludge.info:30561/api/auth/me</code> for example, the docker container running the Node.js app thinks it is a request to localhost (inside the container), and results in a connection error. Please note that the Node.js app inside the container is running on <code>http://localhost:8080</code>. Basically I'm running localhost on my machine, and localhost inside the Node instance. How could I send a request outside to <code>http://kludge.info:30561/</code> where the Ingress controller is running.</p>
  
  <p>I've also configured the <code>baseURL</code>in axios, but it does not solve the problem. My ingress controller has a path <code>/api</code> that will point to a PHP instance, so I need my Node.js axios call to hit that inside it's container. Any help would be much appreciated.</p>
  
  <p>When I ran my K8 cluster on Minikube I did not have this problem, however minikube does provide you with the IP of the VM, while Docker for Desktop uses <code>localhost</code> directly on your machine.</p>