I am having an issue when my Spring Boot app in Docker container tries to access Keycloak Docker container. I have created them using docker-compose. Here are the files:
version: "3.8"
services:
psql-db:
image: postgres
container_name: psql-db
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=xxx
- POSTGRES_DB=postgres
ports:
- '5432:5432'
remind-me:
container_name: remind-me-app
build:
context: .
dockerfile: Dockerfile
ports:
- '8080:8080'
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://psql-db:5432/postgres
- SPRING_DATASOURCE_USERNAME=postgres
- SPRING_DATASOURCE_PASSWORD=xxx
- KEYCLOAK_URL=http://keycloak-prod:8989/realms/remind-me
depends_on:
- psql-db
keycloak-prod:
image: quay.io/keycloak/keycloak:24.0.2
container_name: keycloak-prod
hostname: keycloak-prod
environment:
- KEYCLOAK_ADMIN=admin
- KEYCLOAK_ADMIN_PASSWORD=xxx
command: start-dev
ports:
- '8989:8080'
applrication.properties file in the Spring Boot app:
spring.application.name=remind-me
server.port=8080
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=xxx
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.defer-datasource-initialization=false
spring.sql.init.mode=always
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.orm.jdbc.bind=TRACE
schedule.frequency=0 0 9 * * *
#John Remindme
spring.mail.host=xxx
spring.mail.port=587
spring.mail.username=xxx
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
#keycloak
spring.security.oauth2.resourceserver.jwt.issuer-uri=${KEYCLOAK_URL}
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=${KEYCLOAK_URL}/protocol/openid-connect/certs
jwt.auth.converter.resource-id=remind-me
jwt.auth.converter.principal-attribute=preferred_username
#flyway
spring.flyway.baseline-on-migrate=true
spring.flyway.baseline-version=0
#ehcache, second level cache
spring.cache.jcache.config=classpath:ehcache.xml
The error i get is:
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://keycloak-prod:8989/realms/remind-me/protocol/openid-connect/certs": Connection refused
I can connect to Keycloak via the browser on localhost. Also i get connect to it via Postman and get the token. However the spring boot app in docker throws error.
Thanks!