Running Wildfly Swarm with KeyCloak on docker image

I created a small Wildfly Swarm application with a KeyCloak server using the WildFly Swarm Project Generator. I added the code, built and launched my thick jar using:

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

After the application looked, I created new users added to the area, etc. Then I noticed that 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 the local docker. So I created a 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"] 

Inline image using:

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

And the image appears 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 with:

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

And it looked fine, but when I go to localhost: 8180 / auth and try to log in, I get the wrong username and password, so I can not enter keycloak. So I wonder why this is? Because I included the keycloak database files in the docker image, and if I ran the following commands, you will see that all the 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?

+7
docker dockerfile keycloak wildfly-swarm
source share
1 answer

It seems that the Swarm Keycloak server reads keycloak * .db in the executable jar file (means user.dir ) by default. The swarm process in the container does not read / opt / keycloak * .db because java runs on / .

You can change the data directory using 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 try in the Dockerfile;

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

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

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

The following command is also expected to work.

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

PS

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

+5
source share

All Articles