繁体   English   中英

Docker 无法连接到同一网络上的链接容器 - 所有其他容器都在工作

[英]Docker Can't connect to linked container on the same network - all other containers working

你好,我在这个 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 的服务器都有一个绑定地址,用于说明它想要侦听的接口。 在您的示例代码中, server.listen(port, "localhost")导致服务器只能通过 127.0.0.1 特殊 IP 地址在环回接口上访问。 但是,在 Docker 中,每个容器都有自己的私有环回接口,因此此设置使容器无法从其他任何地方访问。

绑定地址有一个特殊值 0.0.0.0,意思是“监听所有接口”。 这通常是可配置时的默认值(实际上它是 Node server.listen函数的默认值)。 您需要将localhost绑定地址更改为 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() { ... });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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