两个容器之间没有连接



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

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

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

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上的网络,但它现在对你有效。

相关内容

  • 没有找到相关文章

最新更新