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;
}
}
CodePudding user response:
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.