【问题标题】:No connection between the two containers原因:getaddrinfo ENOTFOUND
【发布时间】:2022-06-23 02:28:05
【问题描述】:

我从一个容器中使用应用程序的每个容器,并从一个容器获取 fetch 到另一个容器以获取用户访问权限,但它给出了以下错误: FetchError: 对http://user.localhost/accessticketing 的请求失败,原因:getaddrinfo

ENOTFOUND user.localhost   
    at ClientRequest.<anonymous> (file:///E:/Programming/map/microservices/ticketing/node_modules/node-fetch/src/index.js:108:11)
    at ClientRequest.emit (node:events:539:35)
    at Socket.socketErrorListener (node:_http_client:454:9)
    at Socket.emit (node:events:527:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  type: 'system',
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  erroredSysCall: 'getaddrinfo'
}

文件 docker-compose.yml

version: '3.8'

services:
  traefik:
    image: traefik:v2.7
    container_name: 'Traefik'
    command: --api.insecure=true --providers.docker
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
  user:
    build: './microservices/User'
    container_name: 'User'
    volumes:
      - ./db/user:/db
    labels:
      - "traefik.http.routers.user.rule=Host(`user.localhost`)"
  pv:
    build: './microservices/pv'
    container_name: 'pv'
    volumes:
      - ./db/Project-vendee:/db
    labels:
      - "traefik.http.routers.pv.rule=Host(`pv.localhost`)"

代码:

 try {
    const response = await fetch("http://user.localhost/accessticketing", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      redirect: "follow",
      referrerPolicy: "no-referrer",
      body: JSON.stringify({ Token }),
    });
    const massage: any = await response.json();
    return massage;
  } catch (error: SmartError | undefined | any) {
    console.log(error);
    if (error.type === "system" && error.erroredSysCall === "connect") {
      throw { code: 501, maasage: "Unable to connect to the user section" };
    } else {
      throw error;
    }
  }

【问题讨论】:

    标签: node.js typescript fetch traefik


    【解决方案1】:

    两个容器互不认识, 因此,如果您使用相同的网络会有所帮助!

    docker-compose.yml

    version: '3.8'
    
    services:
      traefik:
        image: traefik:v2.7
        container_name: 'Traefik'
        command: --api.insecure=true --providers.docker
        ports:
          - "80:80"
          - "8080:8080"
        volumes:
          - '/var/run/docker.sock:/var/run/docker.sock'
      user:
        build: './microservices/User'
        container_name: 'User'
        volumes:
          - ./db/user:/db
        labels:
          - traefik.http.routers.user.rule=Host(`user.localhost`)
          - traefik.docker.network=my-network
      pv:
        build: './microservices/pv'
        container_name: 'pv'
        volumes:
          - ./db/Project-vendee:/db
        labels:
          - traefik.http.routers.pv.rule=Host(`pv.localhost`)
          - traefik.docker.network=my-network
    
    networks:
      my-network:
        external : true
    
    

    很快我将编辑此答案并告诉您有关 Docker 网络的更多信息,但它现在适合您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 2013-07-15
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      • 2019-03-14
      相关资源
      最近更新 更多