LoginSignup
1
1

More than 1 year has passed since last update.

DockerでFirebase Local Emulator Suiteを使う

Last updated at Posted at 2021-06-08

はじめに

Firestoreを使ったwebアプリケーションを作る際、Dockerでローカル用のFirestoreが必要になった
mtlynch/firestore-emulatorも選択肢としてはあったが、調べるうちにFirebaseのエミュレータ入れるほうが無難そうに感じた

以上が本記事を書くことになった背景

環境

  • macOS: Catalina 10.15.7
  • docker: 19.03.8
  • firebase-cli: 9.12.1

事前準備としてFirebaseにプロジェクト作成

先にFirebaseにプロジェクトを作成しておくとスムーズに進むので作成しておく
→ここは難しくないので詳細は割愛

今回はFirestoreを使用するため、プロジェクト作成後にFirestoreも作成する
(リージョンはasia-northeastを選択)

Dockerfile

Dockerfile
FROM node:14-buster-slim

RUN apt-get update -y \
  && mkdir -p /usr/share/man/man1 \
  && apt-get install -y curl openjdk-11-jre-headless \
  && npm install -g firebase-tools

どうやらopenjdkインストール前にmanディレクトリが必要らしい
→たしかにmkdir -p /usr/share/man/man1なしだとエラーなった

docker-compose.yaml

docker-compose.yaml
version: "3"

services:
  my-firebase:
    container_name: firebase
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - ../firebase/emulators/:/opt/workspace:cached
      - ../firebase/bin/:/root.cached:cached
      - ../firebase/config/:/root/.config:cached
    ports:
      - 4000:4000 # Emulator Suite UI
      - 5000:5000 # Firebase Hosting
      - 5001:5001 # Clound Functions
      - 9000:9000 # Realtime Database
      - 8081:8081 # Cloud Firestore
      - 8085:8085 # Cloud Pub/Sub
    working_dir: /opt/workspace
    command: bash
    tty: true

ポート番号など、細かい部分はお好みで

Emulatorを起動する

コンテナ入ってfirebaseにログイン
no-localhostオプション忘れずに!

$ firebase login --no-localhost

下記の質問にはYesで答える
→Noでもいけるかも!

? Allow Firebase to collect CLI usage and error reporting information? [Y/n]

下記のようにURLが表示されるので、ブラウザで表示確認
→Firebaseを使うGoogleアカウントを選択
→認証コードが表示されるのでコピー

Visit this URL on any device to log in:
https://accounts.google.com/hogehoge...

下記で、上記で取得した認証コードをペースト

? Paste authorization code here: 

以下が表示されれば成功!

✔  Success! Logged in as [Firebaseを使うGoogleアカウント]

あとは、firebase initコマンドを打ち、質問に答えて必要なFirebaseのサービスをセットアップすれば環境構築完了!

firebase initコマンドでの質問

プロジェクトは既存のものを選ぶのが楽
→何もプロジェクト選択しないと、後のエミュレータ起動時にエラーになる
→新規プロジェクトを作る選択肢もある

エミュレータ起動してみる

$ firebase emulators:start

さいごに

取り急ぎ備忘録としてまとめたので、抜け漏れあれば追加していく

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1