1

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?

François Romain
  • 13,617
  • 17
  • 89
  • 123

1 Answers1

1

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

Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54
  • Thank you. I tried that with `api.website.dev:127.0.0.1` (as the api. runs on localhost) but now it does an `Error: connect ECONNREFUSED 127.0.0.1:443` – François Romain Aug 03 '17 at 17:49
  • it happens because 127.0.0.1 it is a localhost. nodejs try to connect to nodejs container, not to your host. Try to add `extra host` with another IP address of you host machine like `192.168.1.33`. – Bukharov Sergey Aug 03 '17 at 17:59
  • 1
    [this answer](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) may help you – Bukharov Sergey Aug 03 '17 at 18:05
  • It works, thank you so much! Still, it's weird because it removes the portability of a docker project. A specific IP has to be defined for local, test and prod environments. Is there a way to make that dynamic? (I'm going to read the other answer) – François Romain Aug 03 '17 at 18:14
  • Also you can add network with driver `host` to `docker-compose.yml`. Then your docker network will be the same host network. I didnt try this before but it will work. – Bukharov Sergey Aug 03 '17 at 18:24
  • Do you have a link about the `host` driver? – François Romain Aug 03 '17 at 18:31
  • Yes.first answer on [this question](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – Bukharov Sergey Aug 03 '17 at 18:34