How to connect to spanner emulator from a docker container

998 Views Asked by At

I am able to connect to the spanner emulator directly from my local machine but I am having trouble when trying to connect to it from a docker container.

I have the following two services in my compose file:

version: '3.7'

services:
  serviceA:
    image: "test
    depends_on:
      - spanner-emulator
    environment:
      SPANNER_EMULATOR_HOST: localhost:9010

  spanner-emulator:
    image: spanner_image
    ports:
      - 9010:9010
      - 9020:9020
      - 9515:9515

When I spin up the serviceA, I am able to use gcloud to run queries on my local spanner emulator. But when I try to run commands from within the serviceA container I get last exception: 503 failed to connect to all addresses.

The commands that I am trying to run (which work outside the container on my machine directly)

spanner_client = spanner.Client(project="my-proj")
instance = spanner_client.instance("Emulator")
database = instance.database("my-db")
with database.snapshot() as snapshot:
   results = snapshot.execute_sql("SELECT Name, Version FROM test1")
   for row in results:
      print(u"Name: {}, Version: {}".format(*row))

Help appreciated!

1

There are 1 best solutions below

1
On

Dumb mistake. I am able to connect to the emulator from the docker container when I changed SPANNER_EMULATOR_HOST to spanner-emulator:9010