firestore local emulator を docker-composeで動かす


firestore-emulator-dokcerを使ってやろうとしたけどうまく行かなかったので下記の対応で済ませた話。

Dockerfileの準備

Javaが必要なのでapk add --no-cache openjdk8-jreでopenjdk8-jreを追加。

npm i -g firebase-tools && firebase setup:emulators:firestoreでfirestore local emulatorを追加。

FROM node:10-alpine

WORKDIR /usr/src/app

RUN apk add --no-cache openjdk8-jre

RUN npm i -g firebase-tools && firebase setup:emulators:firestore

docker-composeの準備

version: '3'

services:
  app:
    build: .
    volumes:
      - .:/usr/src/app:cached
      - node_modules-data:/usr/src/app/node_modules

volumes:
  node_modules-data:
    driver: local

package.jsonに適当にscriptsを追記

自分は、下記のような感じにしました。

  "scripts": {
    "start-firestore": "firebase serve --only firestore",
    "test": "npm run start-firestore & jest",
  }

Let's TDD

$ docker-compose run --rm app npm test -- --watch 

CI(CircleCI)

CircleCIの設定は下記のような形で対応

一部抜粋

      - run:
          name: npm install
          command: docker-compose run --rm app npm install

      - run:
          name: test
          command: docker-compose exec app npm test