Home > Mobile >  docker-compose: services in a network can ping each other but API requests return as empty responses
docker-compose: services in a network can ping each other but API requests return as empty responses

Time:10-04

This post is linked to my previous post but I thought I would make a new one because the problem has shifted.

So, I am trying to get services in a docker compose file to talk to each other. Here is the docker-compose.yml:

version: '3.9'

services:
  bk-api-gateway:
    image: perrywinkle/api-gateway:latest
    ports:
      - '8080:8080'
    build:
      context: BackEnd/bk_api_gateway
      dockerfile: Dockerfile

  frontend:
    image: perrywinkle/bookeroo-frontend
    ports:
      - '3000:3000'
    build:
      context: FrontEnd/myfirstapp
      dockerfile: Dockerfile
    stdin_open: true
    command: npm start

  books-ms:
    image: perrywinkle/books-ms:latest
    ports:
      - '8000:8000'
    build:
      context: BackEnd/bookmicroservices
      dockerfile: Dockerfile
    volumes:
      - /data/bookeroo-db

  login-ms:
    image: perrywinkle/login-ms:latest
    ports:
      - '8001:8001'
    build:
      context: BackEnd/loginmicroservices
      dockerfile: Dockerfile
    volumes:
      - /data/bookeroo-db

  payment-ms:
    image: perrywinkle/payment-ms:latest
    ports:
      - '8002:8002'
    build:
      context: BackEnd/paymentmicroservices
      dockerfile: Dockerfile
    volumes:
      - /data/bookeroo-db

There are a lot of services here so I am just trying to solve the problem between the api gateway service and the books service at the moment. These 2 have the following Dockerfiles:

FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} api-gateway.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","api-gateway.jar"]
FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} books-ms.jar
EXPOSE 8000
ENTRYPOINT ["java","-jar","books-ms.jar"]

Nothing of note here to be honest but I thought I would include them anyways.

When I try to do a get request from my browser with localhost:8080/api/books it will return an empty 200 OK response, when a list of books should indeed be returned which leads me to believe that there may be some problem with my api gateway, which has some basic routing in the form of the following application.yml (no Eureka servers or anything like that):

server:
  port:8080
spring:
  cloud:
    gateway:
      routes:
        - id: user_service
          uri: 'login-ms:8001'
          predicates:
            - Path=/api/users/**
        - id: user_service
          uri: 'login-ms:8001'
          predicates:
            - Path=/api/business/**
        - id: book_service
          uri: 'books-ms:8000'
          predicates:
            - Path=/api/books/**
        - id: payment_service
          uri: 'payment-ms:8002'
          predicates:
            - Path=/api/payment/**

If I access the CLI of either one of these services and ping them from there (using ping books-ms or vice-versa), they will recognise each other. If I do a curl books-ms:8000/api/books in the api gateway, it will be able to get the expected response, meaning there should not be a network issue between the 2 services.

I am pretty new to docker in general so any guidance would be much appreciated.

CodePudding user response:

I'm no expert on spring cloud gateway but I would try exposing the ports for each service in your Dockerfiles.

# In each service's dockerfile
EXPOSE 8000
EXPOSE 8001
EXPOSE 8002
EXPOSE 8003

I also reckon chucking a schema in the routing URIs is also a good idea too. Something like

- id: user_service
  uri: 'http://login-ms:8001' # Added 'http://' here
  predicates:
     - Path=/api/users/**
  • Related