我正在运行一个简单的Node.js应用程序,其中包含2个容器:服务器容器和客户端容器。我已经设置了这样一种情况,当客户端向服务器发出请求时,服务器将一些数据流式传输回客户端。然而,当我尝试从客户端发起此请求时,我收到以下错误信息:
connect ECONNREFUSED 127.0.0.1:8080
这也适用于我使用容器IP地址的情况。两个容器在同一个网络上。我通过docker network inspect进行了确认。当我通过Postman发出相同的请求时,它可以正常工作。
这是我的设置:
服务器设置
服务器Dockerfile
FROM node:12
EXPOSE 8080
WORKDIR /home/node/server
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "app.js"]
服务器http函数
http.createServer(function(request, response){
filePath = path.join("./serverdata", 'filename.txt');
var stat = fs.statSync(filePath);
response.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': stat.size
});
var readStream = fs.createReadStream(filePath);
readStream.pipe(response);
})
.listen(port);
客户端设置
客户端Dockerfile
FROM node:12
WORKDIR /home/node/client
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "capp.js"]
客户端获取函数 使用服务器容器 IP 没有产生不同的结果
http.get('http://localhost:8080', (resp)=>{
let data = '';
resp.on('data', (chunk)=>{
data += chunk;
});
resp.end('end', ()=>{
console.log(data);
});
}).on("error", (err)=>{
console.log(err.message);
});
Docker Compose:
version: "3.2"
services:
server-app:
build: ./serverClient
ports:
- 8080:8080
volumes:
- servervol:/serverdata
client-app:
build: ./clientClient
ports:
- 8081:8081
volumes:
- clientvol:/clientdata
depends_on:
- "server-app"
volumes:
servervol:
driver: local
clientvol:
driver: local
我看过一些帖子声称问题出在使用 localhost 而不是容器 IP 上,但我已经尝试了两种方法都没有成功。如上所述,在 Postman 中进行时可以正常工作;这使我认为问题出在客户端部分。非常感谢任何指导。
谢谢
server-app:8080
而不是localhost
吗?最好使用容器名称而不是 IP 地址。 - Stefan Golubovićresp.on('end', () => { ... })
可以替代resp.end('end', () => { ... })
。 - Stefan Golubović