簡體   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