skip to Main Content

We have created spring application which is secured using keycloak and it is working fine when we are trying to run it outside docker containers but when we deploy Spring application and Keycloak in two separate docker containers it is not working:

our docker compose file is as follow:

keycloak:
    image: quay.io/keycloak/keycloak:18.0
    hostname: keycloak
    environment:
      KC_DB: postgres
      KC_DB_URL: jdbc:postgresql://db:5432/app
      KC_DB_DATABASE: app
      KC_DB_USERNAME: postgres
      KC_DB_SCHEMA: public
      KC_DB_PASSWORD: root
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: password
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: password
      KEYCLOAK_FRONTEND_URL: http://keycloak:8280
      KC_HOSTNAME_STRICT: "false"
      KC_EDGE: proxy
    ports:
      - 8280:8080
    command: 
      - start-dev
    networks:
      - network
security:
    container_name: security
    expose:
      - "9000"
    ports:
      - 9000:9000
    depends_on:
      - keycloak
    environment:
      - KEY_CLOAK=http://keycloak:8280
    networks:
      - network
networks:
  network:
    driver: bridge

And keycloak json file is as below:

{
  "realm": "Default",
  "auth-server-url": "${KEY_CLOAK}/",
  "ssl-required": "external",
  "resource": "Default",
  "verify-token-audience": true,
  "credentials": {
    "secret": "mlqYrlT2UBmKp2dcfgfdzi5xFnF35lJ"
  },
  "use-resource-role-mappings": true,
  "principal-attribute": "preferred_username"
}

After it gets deployed in docker containers it and we try to access it in browser it is giving below error in security app:

2023-02-20 15:08:50.934  WARN 1 --- [qtp993370665-30] o.keycloak.adapters.KeycloakDeployment   
: Failed to load URLs from http://keycloak:8280/realms/Default/.well-known/openid-configuration
org.apache.http.conn.HttpHostConnectException: Connect to keycloak:8280 [keycloak/172.31.0.5] failed: Connection refused
java.lang.NullPointerException: Cannot invoke "org.keycloak.common.util.KeycloakUriBuilder.clone()" because the return value of "org.keycloak.adapters.KeycloakDeployment.getAuthUrl()" is null

I know there some missing configuration but I am not able to find it. Please help anyone knows it.

2

Answers


  1. I think you can only use a url like http://keycloak:8280 when you are inside the docker network, created by docker compose. Outside (like the browser) you need to adress the localhost instead.

    Login or Signup to reply.
  2. The problem here is the port. You’re trying to access port 8280 of your keycloak inside your docker network. But 8280 is actually the port that is used to access the docker network from your host. If you want to access keycloak via the keycloak hostname, you need to use the port that is used inside the network, which is 8080.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search