Keycloak-x docker startup-scripts not being executed

Hello

I’m currently trying to execute a bash script after keycloak-x starts but the script is not being executed.

Here’s my Dockerfile:

FROM Quay

COPY providers /opt/jboss/keycloak/providers/

COPY startup-scripts /opt/jboss/startup-scripts/

WORKDIR /opt/jboss/keycloak

# Creating a self-signed for testing purposes
RUN keytool -genkeypair -storepass password -storetype PKCS12 -keyalg RSA -keysize 2048 -dname “CN=server” -alias server -ext “SAN:c=DNS:localhost,IP:127.0.0.1” -keystore conf/server.keystore

# Run the config command to install custom providers
RUN ./bin/kc.sh config

As you can see, i’m copying my scripts to the /opt/jboss/startup-scripts/ folder.

I’m building the image like so:

docker build . --tag keycloak_test_13:latest

The build works

I’m executing the following run command:

docker run --name keycloak_test_13 -p 8080:8080 --net keycloak-network -e KEYCLOAK_ADMIN= -e KEYCLOAK_ADMIN_PASSWORD= keycloak_test_13:latest --auto-config --db=postgres -Dkc.db.url.host=<DB_IP> --db-username= --db-password= --http-enabled=true

Here’s the output:

Updating the configuration and installing your custom providers, if any. Please wait.
Server configuration updated and persisted. Run the following command to review the configuration:

kc.sh show-config

2021-03-19 18:53:46,644 WARN [org.key.qua.KeycloakRecorder] (main) New property [kc.db.url.host] set with value [172.18.0.2] in [SysPropConfigSource]. This property is not persisted into the server image.
2021-03-19 18:53:46,649 WARN [org.key.qua.KeycloakRecorder] (main) Please, run the ‘config’ command if you want to persist the new configuration into the server image:

kc.sh config --db=postgres --db-username=DBUSER --db-password=DBPASSWORD --http-enabled=true --db-url-host=DBIP

2021-03-19 18:53:47,906 WARN [io.qua.run.ConfigChangeRecorder] (main) Build time property cannot be changed at runtime. quarkus.package.type was fast-jar at build time and is now mutable-jar
2021-03-19 18:53:48,928 INFO [org.key.url.DefaultHostnameProviderFactory] (main) Frontend: , Admin: , Backend:
2021-03-19 18:53:49,008 INFO [org.key.pro.qua.QuarkusCacheManagerProvider] (main) Loading cluster configuration from /opt/jboss/keycloak/bin/…/conf/cluster-default.xml
2021-03-19 18:53:49,426 INFO [org.inf.CONTAINER] (main) ISPN000128: Infinispan version: Infinispan ‘Corona Extra’ 11.0.4.Final
2021-03-19 18:53:49,564 INFO [org.inf.CLUSTER] (main) ISPN000078: Starting JGroups channel ISPN
2021-03-19 18:53:49,564 INFO [org.inf.CLUSTER] (main) ISPN000088: Unable to use any JGroups configuration mechanisms provided in properties {}. Using default JGroups configuration!
2021-03-19 18:53:49,644 WARN [org.jgr.pro.UDP] (main) JGRP000015: the send buffer of socket MulticastSocket was set to 1.00MB, but the OS only allocated 212.99KB
2021-03-19 18:53:49,644 WARN [org.jgr.pro.UDP] (main) JGRP000015: the receive buffer of socket MulticastSocket was set to 20.00MB, but the OS only allocated 212.99KB
2021-03-19 18:53:49,645 WARN [org.jgr.pro.UDP] (main) JGRP000015: the send buffer of socket MulticastSocket was set to 1.00MB, but the OS only allocated 212.99KB
2021-03-19 18:53:49,645 WARN [org.jgr.pro.UDP] (main) JGRP000015: the receive buffer of socket MulticastSocket was set to 25.00MB, but the OS only allocated 212.99KB
2021-03-19 18:53:51,655 INFO [org.jgr.pro.pbc.GMS] (main) 38890acb2278-34196: no members discovered after 2003 ms: creating cluster as coordinator
2021-03-19 18:53:51,686 INFO [org.inf.CLUSTER] (main) ISPN000094: Received new cluster view for channel ISPN: [38890acb2278-34196|0] (1) [38890acb2278-34196]
2021-03-19 18:53:51,700 INFO [org.inf.CLUSTER] (main) ISPN000079: Channel ISPN local address is 38890acb2278-34196, physical addresses are [172.18.0.3:33266]
2021-03-19 18:53:52,235 INFO [org.key.con.inf.DefaultInfinispanConnectionProviderFactory] (main) Node name: 38890acb2278-34196, Site name: null
2021-03-19 18:53:52,428 INFO [org.key.con.jpa.QuarkusJpaConnectionProviderFactory] (main) Database info: {databaseUrl=jdbc:postgresql://DBIP:5432/keycloak?allowEncodingChanges=false&ApplicationName=PostgreSQL+JDBC+Driver&autosave=never&binaryTransfer=true&binaryTransferDisable=&binaryTransferEnable=&cancelSignalTimeout=10&cleanupSavepoints=false&connectTimeout=10&databaseMetadataCacheFields=65536&databaseMetadataCacheFieldsMiB=5&defaultRowFetchSize=0&disableColumnSanitiser=false&escapeSyntaxCallMode=select&gssEncMode=allow&gsslib=auto&hideUnprivilegedObjects=false&hostRecheckSeconds=10&jaasLogin=true&loadBalanceHosts=false&loginTimeout=0&logServerErrorDetail=true&logUnclosedConnections=false&preferQueryMode=extended&preparedStatementCacheQueries=256&preparedStatementCacheSizeMiB=5&prepareThreshold=5&readOnly=false&readOnlyMode=transaction&receiveBufferSize=-1&reWriteBatchedInserts=false&sendBufferSize=-1&socketTimeout=0&sspiServiceClass=POSTGRES&targetServerType=any&tcpKeepAlive=false&unknownLength=2147483647&useSpnego=false&xmlFactoryFactory=, databaseUser=DBUSER, databaseProduct=PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1), databaseDriver=PostgreSQL JDBC Driver 42.2.18}
2021-03-19 18:53:53,351 ERROR [org.key.services] (main) KC-SERVICES0010: Failed to add user ‘admin’ to realm ‘master’: user with username exists
2021-03-19 18:53:54,044 INFO [io.quarkus] (main) Keycloak 12.0.4 on JVM (powered by Quarkus 1.10.0.CR1) started in 7.790s. Listening on: h ttp://0.0.0.0:8080 and h ttps://0.0.0.0:8443
2021-03-19 18:53:54,045 INFO [io.quarkus] (main) Profile prod activated.
2021-03-19 18:53:54,045 INFO [io.quarkus] (main) Installed features: [agroal, cdi, hibernate-orm, jdbc-h2, jdbc-mariadb, jdbc-mysql, jdbc-postgresql, keycloak, mutiny, narayana-jta, resteasy, resteasy-jackson, smallrye-context-propagation, smallrye-health, smallrye-metrics, vertx, vertx-web]

The output looks like a mess, but the container starts, it says it failed to create the admin user because the user is already created in the DB.

I ssh’d to the container and my sh scripts are in the /opt/jboss/startup-scripts/ folder:

bash-4.4$ cd /opt/jboss/startup-scripts/
bash-4.4$ ls -la
total 104
drwxr-xr-x 2 root root 4096 Mar 19 18:53 .
drwxrwxr-x 1 jboss root 4096 Mar 19 18:53 …
-rw-rw-r-- 1 root root 49 Mar 19 18:52 deploy.cli
-rwxrwxrwx 1 root root 85341 Mar 10 13:23 my_realm.json
-rwxrwxrwx 1 root root 386 Mar 19 17:22 testingfile.sh

The main file in question is testingfile.sh:

#!/bin/bash

echo “inside testingfile”;

for i in {1…10}; do
echo “test: $i”;
sleep 1s
done

echo “after loop”;

/opt/jboss/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080/ --realm master --user admin --client admin-cli --password PASSWORD;
/opt/jboss/keycloak/bin/kcadm.sh create realms -s realm=demorealm -s enabled=true;
echo “after create realms”;

The above script is never executed.

If i run the script manually it works fine and the realm gets created.

I checked everywhere and could not find another example where putting a script in the startup-scripts folder did not execute.

I made sure the file was 777 (overkill but just testing)

I can’t find anything else and getting lost at that point.

Anybody encountered that problem?

Thank you!