# My keycloak integration with spring worked perfectly when keycloak is in docker-compose and project running locally ! but got error 401 when both worked on dokcer compose

**URL:** <https://forum.keycloak.org/t/my-keycloak-integration-with-spring-worked-perfectly-when-keycloak-is-in-docker-compose-and-project-running-locally-but-got-error-401-when-both-worked-on-dokcer-compose/24884>\
**Category:** Getting advice\
**Created:** [March 5, 2024, 12:14pm UTC](https://forum.keycloak.org/t/my-keycloak-integration-with-spring-worked-perfectly-when-keycloak-is-in-docker-compose-and-project-running-locally-but-got-error-401-when-both-worked-on-dokcer-compose/24884 "2024-03-05T12:14:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IslemGharbi](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/islemgharbi/32/9936_2.png) [@IslemGharbi](https://forum.keycloak.org/u/IslemGharbi)\
**Post date:** [March 5, 2024, 12:14pm UTC](https://forum.keycloak.org/t/my-keycloak-integration-with-spring-worked-perfectly-when-keycloak-is-in-docker-compose-and-project-running-locally-but-got-error-401-when-both-worked-on-dokcer-compose/24884/1 "2024-03-05T12:14:46Z")

</div>

`````auto
 crm: # Corrected capitalization for consistency
  image: "crm"
  build: ../CRM
  ports:
   - "8084:8084"
  environment:
   - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://discovery:8761/eureka/
   - spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://keycloak:8080/realms/projectManagement/protocol/openid-connect/certs
   - spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8080/realms/projectManagement
   - JWT_AUTH_CONVERTER_RESOURCE-ID=crm-service
   - spring.datasource.url=jdbc:mysql://crm-mysql-service:3306/crm
   - spring.datasource.password=root
   - JWT_AUTH_CONVERTER_PRINCIPLE-ATTRIBUTE=preferred_username
  depends_on:
   - discovery
   - gateway
   - keycloak
  networks:
   - app-network

 keycloak-mysql-service:
  image: mysql:latest
  environment:
   - spring.datasource.username=root
   - MYSQL_ROOT_PASSWORD=root
   - MYSQL_DATABASE=keycloak
  ports:
   - "3309:3306"
  networks:
   - app-network

 keycloak:
  image: quay.io/keycloak/keycloak:21.1.1
  hostname: keycloak
  ports:
   - "8080:8080"
  environment:
   DB_VENDOR: "mysql"
   DB_ADDR: "keycloak-mysql-service" # Name of the mysql service
   DB_USER: "root"
   DB_PASSWORD: "root" # Use the same password as in mysql config
   DB_DATABASE: "keycloak"
   KEYCLOAK_ADMIN: admin
   KEYCLOAK_ADMIN_PASSWORD: admin
  command:
   - "start-dev"
  networks:
   - app-network
````Preformatted text`

this is a sample of my docker compose `Preformatted text`
`````

---

<div class="post-metadata">

**Author:** ![ThoreKr](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/thorekr/32/7713_2.png) [@ThoreKr](https://forum.keycloak.org/u/ThoreKr)\
**Post date:** [March 6, 2024, 1:55pm UTC](https://forum.keycloak.org/t/my-keycloak-integration-with-spring-worked-perfectly-when-keycloak-is-in-docker-compose-and-project-running-locally-but-got-error-401-when-both-worked-on-dokcer-compose/24884/2 "2024-03-06T13:55:17Z")

</div>

See my answer in [Keycloak does not work with Spring Boot in docker compose - #2 by ThoreKr](https://forum.keycloak.org/t/keycloak-does-not-work-with-spring-boot-in-docker-compose/24910/2) the environment variables used to configure keycloak are no longer valid.

Additionally, do you have any logs from keycloak or your app?

---

<div class="post-metadata">

**Author:** ![nass09](https://avatars.discourse-cdn.com/v4/letter/n/c2a13f/32.png) [@nass09](https://forum.keycloak.org/u/nass09)\
**Post date:** [May 2, 2024, 5:22pm UTC](https://forum.keycloak.org/t/my-keycloak-integration-with-spring-worked-perfectly-when-keycloak-is-in-docker-compose-and-project-running-locally-but-got-error-401-when-both-worked-on-dokcer-compose/24884/3 "2024-05-02T17:22:31Z")

</div>

Hi, I have the exact same problem, if I run a keycloak in a container and my spring boot on localhost without docker it’s working but if I run both from my docker-compose file it’s not working, seems like the environment variable with the service name is changing the issuer uri : [http://keycloak:8080/realms/realmName](http://keycloak:8080/realms/realmName)

Here is my error :

**Bearer error=“invalid\_token”, error\_description=“An error occurred while attempting to decode the Jwt: The iss claim is not valid”, error\_uri=“[RFC 6750 - The OAuth 2.0 Authorization Framework: Bearer Token Usage](https://tools.ietf.org/html/rfc6750#section-3.1)”**

I tried to set KC\_HOSTNAME to localhost but nothing changed.

Any update on the solution I have to implement to resolve the issue ?
