两个容器之间没有连接

发布于 2025-02-10 03:46:39 字数 2271 浏览 1 评论 0原文

我有两个必须相互连接的容器, 但是,当我想从另一个容器中获取数据时enotfound错误! 此代码可在我的本地系统上工作,但在容器中不起作用!

错误日志

FetchError: request to http://user.localhost/accessticketing failed, reason: 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.yaml

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;
    }
  }

I have two containers that must connect to each other,
but when I want to fetch data from another container get ENOTFOUND error!
this code works on my local system but does not work in the container!

error log

FetchError: request to http://user.localhost/accessticketing failed, reason: 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.yaml

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`)"

my code

 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;
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2025-02-17 03:46:39

两个容器彼此不认识,
因此,如果您使用了同一网络!

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上告诉您更多有关网络的信息,但现在对您有用。

The two containers do not know each other,
so it would help if you used the same network!

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

Soon I will edit this answer and tell you more about networking on Docker, but it works for you right now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文