API app cannot connect to Firebase Auth Emulator inside of Docker container on the default network

34 Views Asked by At

I'm attempting to containerize my firebase emulator data:

Dockerfile.emulator

FROM node:alpine

RUN apk add --no-cache \
  openjdk11-jre-headless \
  nodejs \
  npm \
  && npm install -g firebase-tools


WORKDIR /app

COPY ./firebase.json firebase.json
# ... I copy other config files too, clipped for brevity

COPY ./emulator-data emulator-data

CMD [ "firebase", "--project=my-project", "emulators:start", "--only", "firestore,auth,storage", "--import", "emulator-data" ]

EXPOSE 4000
EXPOSE 8080
EXPOSE 9099
# I exposed all emulator ports, clipped for brevity

I also added host to each process in the emulators section from my firebase.json.


  "emulators": {
    "singleProjectMode": false,
    "auth": {
      "port": 9099,
      "host": "0.0.0.0"
    },
    "firestore": {
      "port": 8080,
      "host": "0.0.0.0"
    },
    "ui": {
      "enabled": true,
      "host": "0.0.0.0",
      "port": 4000
    }
  },

This runs successfully after executing my compose file

docker-compose.yml (relevant sections)

version: '3.8'

services:
  emulator:
    container_name: emulator
    image: firestore
    build:
      context: .
      dockerfile: ./dockerfiles/Dockerfile.emulator
    ports:
      - 4000:4000
      - 8080:8080
      - 9099:9099
      # clipped for brevity, I exposed all emulator ports 

  data-api:
    container_name: data-api
    build:
      context: .
      dockerfile: ./apps/data-api/Dockerfile
    ports:
      - 3334:3334
    depends_on:
      - emulator
    env_file:
      - .env
    restart: always

Once running

  • I can access the emulator UI on my local browser
  • the Authentication emulator shows "ON" on port 9099
  • the imported emulator-data is available.
  • I'm able to login and receive a firebase token from an angular application serving locally (network tab shows '127.0.0.1:9099')

However, my API application (running in a docker container) needs to verify tokens and is having trouble connecting to the auth emulator.

Error while making request: connect ECONNREFUSED 127.0.0.1:9099. Error code: ECONNREFUSED

I also confirmed that I set the following environment variables (I also tried 0.0.0.0)

  • process.env['FIRESTORE_EMULATOR_HOST'] = '127.0.0.1:8080'
  • process.env['FIREBASE_AUTH_EMULATOR_HOST'] = '127.0.0.1:9099'

This same configuration works when I run the emulator and API applications locally, so I think it has something to with containers and not the connection information in my API application.

Is there a reason why my data-api container cannot talk to the emulator container even when my locally run frontend can? Have I configured the ports incorrectly? Aren't these apps both on the same, default network?

1

There are 1 best solutions below

0
Nate May On

For a docker container to connect to another container one needs to refer to the container's name rather than a numeric host address. In my case the API container needed to refer to the host emulator (the name of the emulator container)

  • process.env['FIRESTORE_EMULATOR_HOST'] = 'emulator:8080'
  • process.env['FIREBASE_AUTH_EMULATOR_HOST'] = 'emulator:9099'