Connection refused when connecting to keycloak container from backend container

Viewed 3933

I have two containers backend (spring boot application) and Keycloak. if I run keycloak in a container and backend locally : it works

If both of them are run in container the backend doesn't start and shows the following error :

Failed to instantiate [org.springframework.security.oauth2.jwt.JwtDecoder]: Factory method 'jwtDecoderByIssuerUri' threw exception; nested exception is java.lang.IllegalArgumentException: Unable to resolve the Configuration with the provided Issuer of "http://keycloak:8082/auth/realms/myrealm"

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://keycloak:8082/auth/realms/myrealm/.well-known/openid-configuration": Connection refused (Connection refused); 

following are my configs :

docker-compose :

services:

  keycloak:
    image: jboss/keycloak:8.0.1
    command:
      - " -b 0.0.0.0"
    container_name: "keycloak"
    networks:
      - myproject
    volumes:
      - "./keycloak/realm-export.json:/opt/jboss/keycloak/bin/keycloak_export_dir/realm-export.json"
    environment:
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: admin
      KEYCLOAK_IMPORT: /opt/jboss/keycloak/bin/keycloak_export_dir/realm-export.json
    ports:
      - "8082:8080"

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: "backend"
    environment:
      - spring.oauth2.resourceserver.jwt.issuer-uri= http://keycloak:8082/auth/realms/myrealm
    links:
      - keycloak
    networks:
      - myproject
    restart: on-failure
    ports:
      - "8080:8080"

networks:
  myproject:
    driver: bridge

application.yml:

  application:
    name: backend
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:8082/auth/realms/myrealm

do you have any Idea why do I get connection refused ? any help is appreciated :)

1 Answers

Your Keycloak container using the following port configuration

ports:
  - "8082:8080"

That mean:

Keycloak is reachable from Outside via Port 8082.

But internally (in this docker network), keycloak is only reachable via the exposed 8080 port. So your backend application need to connect (internally) to http://keycloak:8080

Related