Skip to content

Commit

Permalink
create fake pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielR-7d committed Jun 8, 2022
1 parent 5dacd44 commit c6a9167
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM google/cloud-sdk:alpine
MAINTAINER contentdev

RUN apk --update add openjdk8-jre
RUN gcloud components install pubsub-emulator beta --quiet

VOLUME /opt/data

COPY start-pubsub.sh .

EXPOSE 8432

ENTRYPOINT ["./start-pubsub.sh"]
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,52 @@
# pubsub-emulator
pub-sub emulator running in docker

# Google Cloud Pub/Sub Emulator

A [Google Cloud Pub/Sub Emulator](https://cloud.google.com/pubsub/docs/emulator) container image. The image is meant to be used for creating an standalone emulator for testing.
Copied from: [pubsub-emulator-docker](https://github.com/SingularitiesCR/pubsub-emulator-docker) and updated the google sdk version since that repository has been archived

## Environment

The following environment variables must be set:

- `PUBSUB_LISTEN_ADDRESS`: The address should refer to a listen address, meaning that `0.0.0.0` can be used. The address must use the syntax `HOST:PORT`, for example `0.0.0.0:8432`. The container must expose the port used by the Pub/Sub emulator.
- `PUBSUB_PROJECT_ID`: The ID of the Google Cloud project for the emulator.

## Connect application with the emulator

The following environment variables need to be set so your application connects to the emulator instead of the production Cloud Pub/Sub environment:

- `PUBSUB_EMULATOR_HOST`: The listen address used by the emulator.
- `PUBSUB_PROJECT_ID`: The ID of the Google Cloud project used by the emulator.

## Custom commands

This image contains a script named `start-pubsub` (included in the PATH). This script is used to initialize the Pub/Sub emulator.

### Starting an emulator

By default, the following command is called:

```sh
start-pubsub
```

## Creating a Pub/Sub emulator with Docker Compose

The easiest way to create an emulator with this image is by using [Docker Compose](https://docs.docker.com/compose). The following snippet can be used as a `docker-compose.yml` for a Pub/Sub emulator:

```YAML
version: "2"

services:
pubsub:
image: singularities/pubsub-emulator
environment:
- PUBSUB_PROJECT_ID=project-test
- PUBSUB_LISTEN_ADDRESS=0.0.0.0:8432
ports:
- "8432:8432"
```
### Persistence
The image has a volume mounted at `/opt/data`. To maintain states between restarts, mount a volume at this location.
17 changes: 17 additions & 0 deletions start-pubsub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

if [[ -z "${PUBSUB_PROJECT_ID}" ]]; then
echo "Missing PUBSUB_PROJECT_ID environment variable" >&2
exit 1
fi

if [[ -z "${PUBSUB_LISTEN_ADDRESS}" ]]; then
echo "Missing PUBSUB_LISTEN_ADDRESS environment variable" >&2
exit 1
fi

gcloud config set project ${PUBSUB_PROJECT_ID}

gcloud beta emulators pubsub start \
--data-dir=/opt/data \
--host-port=${PUBSUB_LISTEN_ADDRESS}

0 comments on commit c6a9167

Please sign in to comment.