Reputation: 14393
A web-app in Node.js connects to an api with axios.
import axios from 'axios'
export function fetchList() {
return axios.get('https://api.website.dev/v1/posts')
}
The api works well, all is fine.
Now, I use docker-compose
to run the exact same nodejs web app inside a container.
docker-compose.yml:
version: "3.1"
services:
nodejs:
image: node:alpine
working_dir: /home/app
restart: always
ports:
- "8000:8080"
volumes:
- ../nodejs:/home/app
command: ["npm", "start"]
The axios call to the Rest API returns an error:
error { Error: getaddrinfo EAI_AGAIN api.domain.dev:443
at Object.exports._errnoException (util.js:1024:11)
at errnoException (dns.js:55:15)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
code: 'EAI_AGAIN',
errno: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'api.domain.dev',
host: 'api.domain.dev',
port: 443,
config:
{ adapter
…
How to make the nodejs app connect to the api from a docker container?
Upvotes: 1
Views: 2337
Reputation: 10185
Dns server inside docker cluster does not know anyting about host api.website.dev
You can explicetelly set Ip address of this host. Try to add extra_hosts
to the service definition in docker-compose.yml
nodejs:
image: node:alpine
working_dir: /home/app
restart: always
ports:
- "8000:8080"
volumes:
- ../nodejs:/home/app
command: ["npm", "start"]
extra_hosts:
- "api.website.dev:<IP_ADDRESS>
Or if you have external DNS server which knows something about website.dev
you can add it to docker cluster as described here
Upvotes: 1