I’m trying to configure Keycloak using Docker. The Keycloak is used for authentication in the angular frontend application, and also in the java spring backend application. My docker-compose configuration for these services, is as follows:
backend:
image: backend
container_name: backend
build:
context: ./backend
ports:
- 8080:8081
depends_on:
- db
networks:
- net
restart: always
frontend:
image: frontend
container_name: frontend
build:
context: ./frontend
ports:
- 80:80
depends_on:
- backend
networks:
- net
restart: always
keycloak:
image: quay.io/keycloak/keycloak:25.0.4
command: start
environment:
KC_HOSTNAME_PORT: 8080
KC_HTTP_ENABLED: true
KC_HOSTNAME_STRICT_HTTPS: false
KC_HEALTH_ENABLED: true
KEYCLOAK_ADMIN: ${KEYCLOAK_ADMIN_USER}
KEYCLOAK_ADMIN_PASSWORD: ${KEYCLOAK_ADMIN_PWD}
KC_DB: postgres
KC_DB_URL: ${KEYCLOAK_DB_URL}
KC_DB_USERNAME: ${KEYCLOAK_DB_USERNAME}
KC_DB_PASSWORD: ${KEYCLOAK_DB_PWD}
KC_HOSTNAME_STRICT: false
KC_PROXY: edge
KC_HOSTNAME: http://keycloak:8080
KC_HOSTNAME_BACKCHANNEL_DYNAMIC: true
ports:
- 8083:8080
restart: always
networks:
- net
db:
image: 'postgres'
container_name: db
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PWD}
ports:
- "5432:5432"
networks:
- net
The spring property is this:
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8080/realms/<realm-name>
And in the angular, I’m using the following configuration:
config: {
url: 'http://localhost:8083',
realm: "<realm-name>",
clientId: "<client-id>"
}
Authentication in the backend works well, but the problem is the authentication in the frontend application, because as the keycloak hostname is “keycloak”, in frontend application it doesn’t know this hostname. If I change it to localhost, the backend container cannot connect to the keycloak container.
Does anyone know how I can solve this problem?