0
votes

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
Could you please share your Dockerfile?Nibrass H
As per Cloud Spanner Emulator Official Documentation, you will have two local endpoints: localhost:9010 for gRPC requests and localhost:9020 for REST requests. Could you please explain what are you doing on port 9515? Have you try to remove that port and verify if that works?Nibrass H

1 Answers

0
votes

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