Azure Container App KeyCloak setup failing with MySQL database error

I’m having trouble setting up KeyCloak in Azure Container App for testing. Every time I try, I get an error saying Table 'databasechangeloglock' already exists. This happens even when I create a new database or clear out the existing one.

Here’s my current Dockerfile configuration:

FROM bitnami/keycloak:latest
USER root

RUN mkdir -p /opt/bitnami/keycloak/data/import /opt/bitnami/keycloak/data/tmp \
    && chown -R 1001:1001 /opt/bitnami/keycloak/data

USER 1001

ENV KEYCLOAK_DATABASE_TYPE=mysql \
    KC_DB=mysql \
    KC_HTTP_ENABLED="true" \
    KC_HOSTNAME_STRICT="false" \
    KC_PROXY=edge \
    KEYCLOAK_CREATE_ADMIN_USER="true" \
    KEYCLOAK_ADMIN_USER="admin" \
    KEYCLOAK_ADMIN_PASSWORD="admin" \
    KC_BOOTSTRAP_ADMIN_USERNAME="admin" \
    KC_BOOTSTRAP_ADMIN_PASSWORD="admin"

EXPOSE 8080

ENTRYPOINT [ "/opt/bitnami/scripts/keycloak/entrypoint.sh" ]
CMD [ "/opt/bitnami/scripts/keycloak/run.sh" ]

The logs indicate the error occurs during the creation of the databasechangeloglock table. Does anyone have suggestions on how to resolve this issue?

hey jackw, i’ve run into similar issues before. have you tried clearing the database completely and then running keycloak with the ‘KC_DB_URL_HOST’ env variable set to your mysql host? also, double-check your mysql user has proper permissions. sometimes that can cause weird errors like this. good luck!

I’ve dealt with this exact issue before when setting up KeyCloak in Azure. The problem often stems from incomplete database cleanup between deployment attempts. Here’s what worked for me:

First, ensure you’re completely dropping the database before each new deployment. Simply clearing tables isn’t always enough. Then, add these environment variables to your configuration:

KC_DB_URL_HOST=your_mysql_host
KC_DB_USERNAME=your_db_user
KC_DB_PASSWORD=your_db_password
KC_DB_URL_DATABASE=keycloak

Also, consider adding ‘KC_DB_URL_PROPERTIES=?useSSL=false’ if you’re not using SSL for the database connection.

If the issue persists, try running KeyCloak with the ‘build’ option first to initialize the schema:

./kc.sh build
./kc.sh start-dev

This approach has resolved similar problems for me in the past. Let me know if it helps!