Skip to content

Instantly share code, notes, and snippets.

@tkhk
Last active September 26, 2022 17:45
Show Gist options
  • Save tkhk/aa713e3ba069ad918d5191a6741f424a to your computer and use it in GitHub Desktop.
Save tkhk/aa713e3ba069ad918d5191a6741f424a to your computer and use it in GitHub Desktop.

Docker で Cloud Pub/Sub エミュレーター

Cloud Pub/Sub エミュレーターの起動

基本的に docker-compose で使うと思うので、以下 docker-compose.yml のサンプル。

version: "3.8"

services:
  cloud_pubsub:
    # https://github.com/GoogleCloudPlatform/cloud-sdk-docker
    image: gcr.io/google.com/cloudsdktool/cloud-sdk:emulators
    ports:
      - 8085:8085
    command: ["gcloud", "beta", "emulators", "pubsub", "start", "--project", "myproject", "--host-port", "0.0.0.0:8085"]

注意点は --host-port の箇所。

Cloud Pub/Sub エミュレーターはデフォルトだとローカル以外の通信を受け付けないので、--host-port で外からのアクセスを許可させる必要がある。

ref. GoogleCloudPlatform/cloud-sdk-docker#104

エミュレーターと実環境の切り替え

クライアントのコードには何も設定は必要ない。

クライアント側で環境変数 PUBSUB_EMULATOR_HOST を設定することで、ライブラリ側で自動で認証のオフなどの切り替えをしてくれる。

例えば上の docker-compose.yml の場合は、expose PUBSUB_EMULATOR_HOST=localhost:8085 すれば良い。

エミュレーターでできないこと

IAM 関連はともかく、「プロトコル バッファのスキーマ サポート」に対応して欲しい。

ref. https://cloud.google.com/pubsub/docs/emulator?hl=ja#known_limitations

参照

ドキュメント: https://cloud.google.com/pubsub/docs/emulator?hl=ja

gcloud CLI の Docker イメージ: https://cloud.google.com/sdk/docs/downloads-docker?hl=ja

Docker イメージリポジトリ: https://github.com/GoogleCloudPlatform/cloud-sdk-docker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment