[keycloak-user] Using Keycloak within docker compose and spring boot application

Sebastian Eichholz gene at kultpower.de
Tue Aug 1 18:05:13 EDT 2017


I am trying to use Keycloak with my spring boot application.
It’s working nicely and now I want to use docker-compose to bundle keycloak-mysql with mysql and my spring boot app.

My docker-compose.yml file:

{{{
mysql:
 image: "mysql:5.7"
 container_name: "mysql-for-keycloak"
 environment:
  - MYSQL_DATABASE=keycloak
  - MYSQL_USER=keycloak
  - MYSQL_PASSWORD=keycloak
  - MYSQL_ROOT_PASSWORD=test
 volumes:
  - /Users/sebastian/git/oregami_docker_compose/mysql_data:/var/lib/mysql
keycloak:
 image: "jboss/keycloak-mysql:3.2.0.Final"
 container_name: "keycloak-with-mysql"
 ports:
  - "8087:8080"
 links:
  - "mysql:mysql"
 environment:
  - PROXY_ADDRESS_FORWARDING=true 
  - MYSQL_DATABASE=keycloak 
  - MYSQL_USERNAME=keycloak 
  - MYSQL_PASSWORD=keycloak
  - KEYCLOAK_LOGLEVEL=DEBUG
oregami:
 build: "./oregami-game-database-cqrs"
 ports:
  - "8080:8080"
 container_name: "oregami-docker"
 links:
  - "keycloak:keycloak"   
}}}


The Dockerfile for my spring boot application:

{{{
# Base Alpine Linux based image with OpenJDK JRE only
FROM openjdk:8-jre-alpine
# copy application WAR (with libraries inside)
COPY target/oregami-0.0.2-SNAPSHOT.jar /app.jar

EXPOSE 8080

# specify default command
CMD ["/usr/bin/java", "-jar", "-Dkeycloak.auth-server-url=http://keycloak:8087/auth", "/app.jar"]
}}}

Now when I navigate to a page of my app which needs authentication I am forwarded to http://keycloak/  (I edited my /etc/hosts file for this to work). But when I submit my user and password from the login form, I get this error:

{{{
java.net.ConnectException: Connection refused (Connection refused)
	at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_131]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_131]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_131]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_131]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_131]
	at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_131]
	at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:120) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:179) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:144) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:134) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:612) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:447) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) ~[httpclient-4.5.2.jar!/:4.5.2]
	at org.keycloak.adapters.ServerRequest.invokeAccessCodeToToken(ServerRequest.java:111) ~[keycloak-adapter-core-3.2.0.Final.jar!/:3.2.0.Final]
	at org.keycloak.adapters.OAuthRequestAuthenticator.resolveCode(OAuthRequestAuthenticator.java:330) ~[keycloak-adapter-core-3.2.0.Final.jar!/:3.2.0.Final]
	at org.keycloak.adapters.OAuthRequestAuthenticator.authenticate(OAuthRequestAuthenticator.java:275) ~[keycloak-adapter-core-3.2.0.Final.jar!/:3.2.0.Final]
	at org.keycloak.adapters.RequestAuthenticator.authenticate(RequestAuthenticator.java:139) ~[keycloak-adapter-core-3.2.0.Final.jar!/:3.2.0.Final]
}}}

Can anybody help?
Thanks!


Sebastian






More information about the keycloak-user mailing list