【发布时间】:2019-04-21 01:41:41
【问题描述】:
我有 3 个应用程序。网络、订单和支付,都在单独的 docker 容器中运行
例如在我的订单应用中。我有这条调用支付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)
})
})
那么它总是会返回这个错误
Error: connect ECONNREFUSED 127.0.0.1:3003
我的假设是 docker 容器无法使用 localhost 相互通信
我的 docker-compose 文件
version: "3"
services:
web:
build: "./web"
ports:
- "3000:3000"
order:
build: "./order"
ports:
- "3001:3000"
payment:
build: "./payment"
ports:
- "3003:3000"
用于订单和付款的 docker 文件(它们共享相同的代码库)
#-- Build
FROM node:8-alpine
COPY . /src
WORKDIR /src
RUN npm install --production
EXPOSE 3000
CMD npm start
我该如何解决这个问题?
【问题讨论】:
-
要调用支付应用程序,您的路线应该类似于
http://payment:3000/payment-test。因为在 docker-compose network app 内部可以通过服务名看到对方。