Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Wildfly Swarm with KeyCloak on docker image

I created small Wildfly Swarm application with KeyCloak server using WildFly Swarm Project Generator I Added some code, built and started my fat jar using:

java -jar -Dswarm.port.offset=100 login-service-swarm.jar

After app stared i created new realm added users etc. Then I noticed keycloak created 3 files in my target folders. Those files where:

  • keycloak.h2.db
  • keycloak.lock.db
  • keycloak.trace.db

Then I decided to create docker images and run it in local docker enviroment. So I created docker file:

FROM java:openjdk-8-jdk
ADD login-service-swarm.jar /opt/login-service-swarm.jar
ADD keycloak.h2.db /opt/keycloak.h2.db
ADD keycloak.lock.db /opt/keycloak.lock.db
ADD keycloak.trace.db /opt/keycloak.trace.db

EXPOSE 8180
ENTRYPOINT ["java", "-jar", "-Dswarm.port.offset=100", "/opt/login-service-swarm.jar"]

Built image using:

docker build -f Dockerfile -t login-service-swarm-v1 .

And image is visible in my docker image list:

   C:\Work\Java\login-service\docker>docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED              SIZE
login-service-swarm-v1       latest              710cddc59623        About a minute ago   790 MB
<none>                       <none>              100c0ee60f25        3 hours ago          779 MB
demo                         latest              03d12d49ba5e        4 hours ago          760 MB
java                         openjdk-8-jdk       d23bdf5b1b1b        5 months ago         643 MB

So I started it using:

docker run -p 8180:8180 login-service-swarm-v1

And it stared ok but when i go to localhost:8180/auth and try to login i get wrong username and password message so I'm unable to login to keycloak. So I wonder why is this? Because I manuayl included keycloak database files in docker image and if I run following commands you can see all files are there as expected.

PS C:\> docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                    NAMES
8bb4bdb3945e        login-service-swarm-v1       "java -jar -Dswarm..."   2 minutes ago       Up 2 minutes        0.0.0.0:8180->8180/tcp   blissful_knuth
PS C:\> docker exec -it 8bb4bdb3945e bash
root@8bb4bdb3945e:/# ls
bin  boot  dev  etc  home  keycloak.h2.db  keycloak.lock.db  keycloak.trace.db  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@8bb4bdb3945e:/# cd opt
root@8bb4bdb3945e:/opt# ls
keycloak.h2.db  keycloak.lock.db  keycloak.trace.db  login-service-swarm.jar

So where is the catch??

like image 424
Kiki Avatar asked Jun 27 '17 10:06

Kiki


1 Answers

It looks like the Swarm Keycloak Server reads the keycloak*.db in the dir executed java(means user.dir) in default. The swarm process in container doesn't read /opt/keycloak*.db because java runs on /.

You can change the data dir with wildfly.swarm.keycloak.server.db sysprop. https://github.com/wildfly-swarm/wildfly-swarm/blob/2017.6.1/fractions/keycloak-server/src/main/java/org/wildfly/swarm/keycloak/server/runtime/KeycloakDatasourceCustomizer.java#L52

Please give it a try in Dockerfile;

ENTRYPOINT ["java", "-jar", "/opt/login-service-swarm.jar", "-Dwildfly.swarm.keycloak.server.db=/opt/keycloak"]

Or, you can also use -w option with docker run.

$ docker run --help
-w, --workdir string              Working directory inside the container

The following command is supposed to work as well.

docker run -p 8180:8180 -w /opt login-service-swarm-v1

P.S.

I recommend using Volume or Volume Container instead of adding the data files in Dockerfile. https://docs.docker.com/engine/tutorials/dockervolumes/

like image 85
emag Avatar answered Oct 19 '22 08:10

emag