Apollo Gateway not working inside docker-compose

Viewed 691

I am trying to setup a simple Federated Apollo Gateway using Docker and docker-compose but can't seem to get the gateway to connect to the schemas services.

Here is the docker-compose.yml file

version: "3.9"
services:
  gateway:
    build: ./api-gateway
    ports:
      - 4000:8080
    depends_on:
      - robots
      - sitemaps
    environment:
      APOLLO_KEY: ${APOLLO_KEY}
    volumes:
      - ../secrets/credentials.json:/tmp/key/credentials.json
      - ./api-gateway/index.js:/usr/src/app/index.js
  
  robots:
    build: ./api-robots
    ports:
      - 4001:8080
    environment:
      GOOGLE_APPLICATION_CREDENTIALS: /tmp/key/credentials.json
    volumes:
      - ../secrets/credentials.json:/tmp/key/credentials.json
      - ./api-robots/src:/usr/src/app/src


  sitemaps:
    build: ./api-sitemaps
    ports:
      - 4002:8080
    environment:
      GOOGLE_APPLICATION_CREDENTIALS: /tmp/key/credentials.json
    volumes:
      - ../secrets/credentials.json:/tmp/key/credentials.json
      - ./api-sitemaps/src:/usr/src/app/src

In the gateway code I am registering the robots and sitemaps services using the following:

const { ApolloServer } = require("apollo-server");
const { ApolloGateway } = require("@apollo/gateway");

// Initialize an ApolloGateway instance and pass it an array of
// the implementing service names and URLs
const gateway = new ApolloGateway({
  serviceList: [
    { name: "robots", url: "http://robots:4001" },
    { name: "sitemaps", url: "http://sitemaps:4002" },
  ],
});

const server = new ApolloServer({
  gateway,
  subscriptions: false,
});

server
  .listen(8080)
  .then(({ url }) => {
    console.log(` Gateway Server ready at ${url}`);
  })
  .catch((err) => {
    console.error("Failed to start Gateway");
  });

Yet, when it runs, I get the following error reported by the gateway service:

gateway_1   | Error checking for changes to service definitions: Couldn't load service definitions for "robots" at http://robots:4001: request to http://robots:4001/ failed, reason: connect ECONNREFUSED 172.19.0.3:4001
gateway_1   | This data graph is missing a valid configuration. Couldn't load service definitions for "robots" at http://robots:4001: request to http://robots:4001/ failed, reason: connect ECONNREFUSED 172.19.0.3:4001

I know that the services are working correctly because I am able to connect to the robots and sitemaps services from the host at http://localhost:4001 and http://localhost:4002 respectively; and both work without any issues.

I've read countless threads about this and the most common issue that I find is that others are incorrectly trying to connect on localhost instead of using the services name (e.g. robots and sitemaps) as the domain name. I am not making that mistake.

Here are some other things I have tried, but also did not work ...

  • creating a custom networks definition and assigning it to each service
  • connecting to http://robots:8080 and http://sitemaps:8080
  • connecting to http://localhost:4001 and http://localhost:4002

What am I doing wrong?

1 Answers

You should be requesting to port 8080 instead of 4001, that's all. It should be http://robots:8080 and http://sitemaps:8080

Related