top of page
  • Writer's pictureIvan Brko

Local Development of Applications that connect to GCP Pub/Sub using Docker Compose

Updated: Nov 16, 2023

Being able to run and test applications locally simplifies the development process and decreases cloud costs. This tutorial shows how to setup a GCP Pub/Sub Emulator locally using Docker Compose and also how to configure it to use a push subscription that targets a web server that's running on your computer.

Note that we will be using the Google-provided Pub/Sub Emulator.


Initialization script

Once our docker container starts running, it's going to run the script init_pubsub.sh to initialize the local Pub/Sub Emulator.

Without Pub/Sub push subscription

If you don't need to configure a Pub/Sub push subscription, this is all you need in the script.

With Pub/Sub push subscription

If you want to create a Pub/Sub push subscription, modify the given script to look like this:



Notice that the given script has hard-coded the following values, update them to match your preferences:

  • Pub/Sub Emulator is running at port 8085 (I suggest not changing this, you can modify the mapped exposed port later in the docker-compose file)

  • GCP Project ID set to local-project (this is not a real GCP project you have to create, just a dummy value used for connecting to the emulator)

  • Topic name set to local-test-topic

  • Subscription name set to local-test-subscription

  • On any new Pub/Sub message, the emulator will notify our application that will be running locally on our machine by calling the endpoint http://host.docker.internal:8080/pubsub/message (in other words, we expect that our web server is running on port 8080 and will handle the new message at path pubsub/message)

The DockerCompose file

This is all we need in the docker-compose file (let's assume we store it as dev.docker-compose.yaml, in the same directory where we stored the init_pubsub.sh script).


Again, notice that:

  • we expose the Pub/Sub Emulator to the outside using the port 8085

  • we use local-project as the project name (you don't have to have a real GCP project with this name, this is just a name that's used in this tutorial)


Local .env file

In order to connect your application to the emulator that's running, create a file called .local.env and save the following inside:



Wrapping everything up

We have all the pieces, now let's run the emulator by running:




In another terminal session, source the .local.env file (by running source .local.env) to set the needed env vars, run your application and it should connect to the emulator (and if you configured the push subscription, the emulator should be calling the endpoint you defined for any new messages).


To quickly send some messages to the emulator and verify that your subscriber is connected to it, connect to the running container and execute the following commands:



34 views0 comments

Comentários


bottom of page