2

I have 3 docker containers:

Backend (Spring boot rest api) Frontend (Js and html in the apache image) Mongodb

I'm orchestrating them through docker-compose and works nicely.

However I don't know how to let my frontend javascript client know the backend container's host/ip in order to reach it.

This is my docker-compose.yml:

version: '3.1'

services:
  project-server:
    build: .
    restart: always
    container_name: project-server
    ports:
      - 8200:8200
    working_dir: /opt/app
    depends_on:
      - mongo

  httpd:
    image: project-ui
    container_name: project-ui
    ports:
      - 8201:80


  mongo:
    image: project-mongo
    container_name: project-mongo
    ports:
      - 27018:27017
    volumes:
      - $HOME/data/mongo-data:/data/db
      - $HOME/data/mongo-bkp:/data/bkp
    restart: always

So i've tried with this in my js client app:

export default {
  REMOTE_HOST: 'http://project-server:8200'
}

But it doesn't work. (Failed to load resource: net::ERR_NAME_NOT_RESOLVED)

And i'm pretty sure it's because JS runs locally on the browser so it has no way to resolve that.

What's the right way to do this? There is any way for the frontend service (apache) to pass/render the real host to Javascript and get it somehow?

Thanks a lot

magnoz
  • 1,939
  • 5
  • 22
  • 42
  • you have to use this url `http://project-server:8200` for reaching backend-service in ajax calls. Even if your ui code is running on the browser, it is rendered by some webserver, which would make the call to backend. so it shud work – pvpkiran Jun 14 '18 at 10:49
  • That's the first thing i've tried but it doesn't work, and It shouldn't because JS runs on the client locally and it's not trying to reach any real IP or host. So I guess it should be a way for the host to be renderer in the apache which hosts the js client and get it from there somehow, but I don't know how to. – magnoz Jun 14 '18 at 10:51
  • first thing is, are your sure you have connectivity to the host server where you have your docker container – pvpkiran Jun 14 '18 at 10:55
  • I do because it's on the same machine, If I set REMOTE_HOST to localhost it will work. But if I push it like that to production I doubt it'll work... or should it? – magnoz Jun 14 '18 at 11:01
  • you must use: https://docs.docker.com/compose/compose-file/#links with that you can use the hostname linked in the docker-compose.yml – Gianmarco Carrieri Jun 14 '18 at 13:40

1 Answers1

1

project-server can be resolved only within the network created by docker-compose. As you mentioned, to connect from the outside world you need to export the IP of your host instead of project-server. The problem is the guest container doesn't know the IP of the guest. Here is a detailed discussion about that: How to get the IP address of the docker host from inside a docker container

What you probably need in your situation is to run the container passing the IP of the host as an environment variable:

run --env <IP>=<value>

Then in node you can just read that variable. Hope it helps

b0gusb
  • 4,283
  • 2
  • 14
  • 33