spring-bootdocker-composekeycloak-connect

Docker Compose Connection Refused For Keycloak and Spring Boot Application


I am fairly new to docker. So I have a docker compose.yml file which helps me dockerize keycloak and a Spring boot Application. Here is my application-docker.yml file


spring:
  security:
    oauth2:
      client:
        registration:
          MyRealm:
            client-id: myclient
            client-secret: mysecrete
            authorization-grant-type: authorization_code
            redirect-url: "{baseUrl}/login/oauth2/code/myclient"
            scope:
              - openid
              - profile
              - email
              - roles
        provider:
          MyRealm:
            issuer-uri: "http://localhost:8090/realms/myRealm"

And here is my docker.yml file

  keycloak:
    container_name: keycloak
    image: quay.io/keycloak/keycloak:23.0.4
    command: ["start-dev","--import-realm"]
    environment:
      KEYCLOAK_LOGLEVEL: DEBUG
      KC_DB: postgres
      KC_DB_URL_HOST: keycloak-postgres
      KC_DB_URL_DATABASE: keycloak
      KC_DB_PASSWORD: mypassword
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: mypassword
    ports:
      - "8090:8080"
    expose:
      - "8090"
    healthcheck:
      test: "exit 0"
    depends_on:
      keycloak-postgres:
        condition: service_healthy
    networks:
      - astro_network


  astro-orb:
    image: astromyllc/astro-orb:0.001
    container_name: astro-orb
    pull_policy: always
    ports:
      - "7013:7013"
    expose:
      - "7013"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    healthcheck:
      test: "exit 0"
    depends_on:
      keycloak:
        condition: service_healthy
      discovery-server:
        condition: service_healthy
      astro-api-gateway:
        condition: service_healthy
      zipkin:
        condition: service_healthy
    networks:
      - astro_network



networks:
  astro_network:
    driver: bridge

This works when I have keycloak running from docker and the spring boot application running from my IDE. But after deploying both to docker I get this error


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'authorizedClientService': Error creating bean with name 'authorizedClientService' defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2WebSecurityConfiguration.class]: Unsatisfied dependency expressed through method 'authorizedClientService' parameter 0: Error creating bean with name 'clientRegistrationRepository' defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2ClientRegistrationRepositoryConfiguration.class]: Failed to instantiate [org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository]: Factory method 'clientRegistrationRepository' threw exception with message: Unable to resolve Configuration with the provided Issuer of "http://localhost:8090/realms/ShootingStar"
.
.
.
.
... 47 common frames omitted
2024-01-22 01:13:14 Caused by: java.lang.IllegalArgumentException: Unable to resolve Configuration with the provided Issuer of "http://localhost:8090/realms/ShootingStar"
.
.
.
... 48 common frames omitted
2024-01-22 01:13:14 Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8090/realms/ShootingStar/.well-known/openid-configuration": Connection refused

how do I fix this please. Its been days now and I am not making any headway

I tried using the docker IP of the keycloak. When I use the docker port (8080) of keycloak, the spring app runs but I cants access it from the local browser. I tried using the container name(ie keycloak) in he issuer-uri.


Solution

    1. When you specify localhost in your Spring Boot application's configuration, it tries to connect to itself (since within its container, localhost refers to the container itself, not your host machine). Instead, use the service name defined in the Docker Compose file (in your case, it seems to be keycloak) as the hostname.
    1. You've mapped the Keycloak port to 8090 on your host. This is correct for accessing Keycloak from your host browser. However, within the Docker network (between containers), you should use the internal port which Keycloak listens on, typically 8080.

    2. You have defined a custom network astro_network. Make sure all relevant services are attached to this network for them to communicate internally.