0

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
  • I'm having the same issue, did you manage to find a solution? I've tried localhost, 127.0.0.1 and emulator as host, all of them are giving me ECONNREFUSED. I'm strugling to find where the error lies Commented Dec 26, 2024 at 16:56

1 Answer 1

0

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.