你好,我在这个 docker-compose 文件中运行 4 个服务:
所有服务都运行良好,但只有名为“repnjs”的服务有问题。
version: "3"
volumes:
local_mongo_data: {}
local_mongo_data_backups: {}
services:
tomcat: &tomcat
build:
context: .
dockerfile: compose/local/tomcat/Dockerfile
image: oneuni_local_tomcat
depends_on:
- mongo
volumes:
- .:/app
env_file:
- .envs/.local/.main
- .envs/.local/.mongo
ports:
- "8080:8080"
repnjs:
build:
context: .
dockerfile: compose/local/repnjs/Dockerfile
image: oneuni_local_repnjs
restart: always
depends_on:
- mongo
volumes:
- ./oneuni-student-report:/home/node/app
- ./oneuni-student-report/node_modules:/home/node/app/node_modules
- ./compose/local/repnjs/mongotest.js:/app/mongotest.js
env_file:
- .envs/.local/.main
- .envs/.local/.mongo
ports:
- 3500:3500
command: npm run startDev
web-client:
build:
context: .
dockerfile: ./compose/local/web-client/Dockerfile
image: local_web-client
depends_on:
- tomcat
- repnjs
restart: always
volumes:
- .:/home/node/app
- ./one-uni-web-client/node_modules:/home/node/app/node_modules
env_file:
- .envs/.local/.main
ports:
- 4200:4200
mongo:
build:
context: .
dockerfile: compose/production/mongo/Dockerfile
image: oneuni_production_mongo
container_name: mongo
volumes:
- ./compose/production/mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
- ./compose/production/mongo/conf/mongod.conf:/etc/mongod.conf
- local_mongo_data:/data/db
- local_mongo_data_backups:/backups
env_file:
- .envs/.local/.mongo
ports:
- '27017-27019:27017-27019'
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
hostname: mongo
它没有连接到 localhost:3500我试过了
curl -i localhost:3500
总是给我这个错误curl: (56) Recv failure: Connection reset by peer
我试过 curl 其他 docker 端口都工作正常,我已经进入每个容器以在同一个网络上 curl 另一个容器,服务主机名也都工作只有“repnjs”不工作这是“repnjs”的 docker 文件
FROM node:12.19.0
COPY ./oneuni-student-report/ /app
COPY ./compose/local/tomcat/oneuni/ /etc/oneuni/
WORKDIR /app
RUN npm install
RUN npm install -g nodemon
CMD ["npm", "run", "startDev"]
这是 express 应用程序的 package.json 文件
{
"name": "nodejs-project",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"startDev": "nodemon ./bin/www"
},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"debug": "~2.6.9",
"express": "~4.15.5",
"jade": "~1.11.0",
"mongodb": "^3.0.3",
"morgan": "~1.9.0",
"properties-reader": "0.0.16",
"serve-favicon": "~2.4.5"
}
}
这是服务器/bin/www 的入口点
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require("../app");
var debug = require("debug")("nodejs-project:server");
var http = require("http");
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || "3500");
app.set("port", port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port, "localhost", function () {
console.log("started to listen on" + port + " localhost");
});
server.on("error", onError);
server.on("listening", onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== "listen") {
throw error;
}
var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
还要注意的是,nodejs 的代码在本地机器上完美运行,只是在 docker-compose 运行时无法访问如果有人能在这个问题上帮助我,我将不胜感激,我已经花了一周的时间试图解决它,但不确定它为什么会发生。
谢谢
最佳答案
任何基于 TCP 的服务器都有一个绑定(bind)地址,该地址说明了它想要监听的接口(interface)。在您的示例代码中,您 server.listen(port, "localhost")
这导致服务器只能通过 127.0.0.1 特殊 IP 地址在环回接口(interface)上访问。但是,在 Docker 中,每个容器都有自己的私有(private)环回接口(interface),因此此设置使容器无法从其他任何地方访问。
绑定(bind)地址有一个特殊的值,0.0.0.0,意思是“监听所有接口(interface)”。这通常是可配置的默认值(实际上它是 Node server.listen
函数的默认值)。您需要更改 localhost
将地址绑定(bind)到 0.0.0.0,或者只是删除它,或者使其可配置。
// Just remove the bind address parameter entirely
server.listen(port, function() { ... });
// Make it configurable, if you need to
const bindAddress = process.env.BIND_ADDRESS || "0.0.0.0";
server.listen(port, bindAddress, function() { ... });
关于node.js - Docker无法连接到同一网络上的链接容器-所有其他容器都在工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64412622/