Search code examples
dockerdocker-composenetflix-eurekaspring-cloud-gateway

Dockerizing application with localhost leads to connection refused


So when I start the application locally, it works. The moment I use docker-compose up it starts up, it's going well, and than suddenly connection refused. I used Spring Cloud Gateway, didn't work. Tried adding Eureka, also didn't work. The problem might be is because I use localhost, but I wouldn't know what else to put there instead.

Things I've tried instead of localhost:

  • 0.0.0.0
  • host.docker.internal
  • eurekaserver
  • eureka-service
  • getting the docker ip with docker-machine ip, but it had no machine and no default machine ip

What I want it to do when it's dockerized is basically, make api call from port 8080 to the user service which runs on port 8081. Which works with local startup, but not when dockerized.

The first error log:

user-ms           | 2022-04-04 08:22:52.331  INFO 1 --- [           main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http
://localhost:8087/eureka/}, exception=I/O error on GET request for "http://localhost:8087/eureka/apps/": Connect to localhost:8087 [localhost/127.0.0.1] failed: Connection refused (Con
nection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8087 [localhost/127.0.0.1] failed: Connection refused (Connection refused) sta
cktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8087/eureka/apps/": Connect to localhost:8087 [localhost/127.0.0.1] faile
d: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8087 [localhost/127.0.0.1] failed: Connection refuse
d (Connection refused)

API-Gateway.yml:

server:
  port: 8080

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8087/eureka

spring:
  application:
    name: api-gateway
  main:
    web-application-type: reactive
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: user-ms
          uri: lb://USER-MS
          predicates:
            - Path=/user/**

Eureka.yml:

spring:
    application:
        name: eureka-server

server:
    port: 8087

eureka:
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
            defaultZone: http://localhost:8087/eureka

User.yml:

spring:
  application:
    name: user-ms

server:
  port: 8081

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8087/eureka

Docker-compose.yml:

version: '3.8'

services:
  eureka-service:
    build: ./eureka-discovery-service
    restart: always
    container_name: eureka-service
    ports:
      - 8087:8087    

  user:
    build: ./user
    restart: unless-stopped
    container_name: user-ms
    ports:
    - 8081:8081
    depends_on:
      - eureka-service    

  api-gateway:
    build: ./api-gateway
    restart: always
    container_name: api-gateway
    depends_on:
      - eureka-service
    ports:
      - 8080:8080

  mongodb:
    image: mongo
    restart: always
    container_name: mongodb
    ports:
      - 27017:27017

Where am I going wrong? It feels like docker internally needs a different url instead of localhost, but I wouldn't know what it could be.


Solution

  • Okay so basically what I found out. Using eureka-service did work, but I had to run a command. I had to run mvn clean package to refresh all the jar-files etc and now it works. I had been struggling for 3 weeks and now it finally works holy smokas.