Installing Keycloak on Alpine Linux docker image

(Optional) Export Keycloak Database

If you already have a prime Keycloak database and need to clone it to docker image, you can export it by starting keycloak in export mode. I found that it doesn’t like relative path, so you need to run the script specifying filename in the folder you are exporting to:

bin/ -Dkeycloak.migration.action=export
-Dkeycloak.migration.provider=singleFile -Dkeycloak.migration.file=backup.json

This exports all the realms, groups etc. including users with their passwords - awesome!

Dockerising Keycloak

FROM alpine:3.13

# refresh
RUN apk update && apk upgrade

# create a user keycloak will run as
RUN adduser keycloak -D

# need curl to download keycloak
# Java is required by keycloak (todo: check if JRE is enough, may save space)
RUN apk add curl openjdk11

USER keycloak
RUN cd ~/ && curl -L -o ~/keycloak.tar.gz
RUN cd ~/ && mkdir keycloak && cd keycloak && tar -xzf ~/keycloak.tar.gz --strip-components=1
RUN rm ~/keycloak.tar.gz
COPY backup.json /home/keycloak/backup.json

USER keycloak

This is how looks like:

~/keycloak/bin/ -b -Dkeycloak.migration.action=import -Dkeycloak.migration.provider=singleFile -Dkeycloak.migration.file=/home/rd/backup.json -Dkeycloak.migration.strategy=OVERWRITE_EXISTING

Checking Keycloak has started

I couldn’t find a good way to check that, other than checking http response on port 8080:

while [ -z "$(curl -s localhost:8080)" ]
  echo "waiting...""
  sleep 1s

echo "Keycloak started!"

To contact me, send an email anytime or leave a comment below.