简体   繁体   中英

Error: connect ECONNREFUSED 127.0.0.1:3003 Docker containers

I have 3 apps. web, order and payment and all are running in separate docker container

For example in my order app. I have this route that calls payment api

router.post('/order-test', (req, res) => {
   let data = "Hello"

   // Call the payment api by using Axios
   axios.post('http://localhost:3003/payment-test', { data: data } )
     .then((response) => {
        console.log(response)
      })
     .catch((error) {
         console.log(error)
      })

})

Then it will always return this error

Error: connect ECONNREFUSED 127.0.0.1:3003

My assumption would be that docker container cannot communicate with each other using localhost

My docker-compose file

version: "3"
services:
  web:
    build: "./web"
    ports:
      - "3000:3000"

  order:
    build: "./order"
    ports:
      - "3001:3000"

  payment:
    build: "./payment"
    ports:
      - "3003:3000"

My docker file for both order and payment (they share the same code base)

#-- Build
FROM node:8-alpine

COPY . /src

WORKDIR /src

RUN npm install --production

EXPOSE 3000

CMD npm start

How do i resolve this problem?

As mentioned in the comment you ought to replace localhost with the service name eg payment , and then you can point directly to the port being exposed payment:3000 . I think in that case you should use the expose keyword instead of ports . eg

expose:
  - 3000

If you are prone to changing service names, and you also just want to bind to the host ports you can also get away with 0.0.0.0:3003 which is the broadcast address. This requires no change to your current configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM