I tried this in my test environment I also facing the same issue, where keycloak failed to connect to the database.
After troubleshooting I got the main issue was incorrect service dependencies and connection setup b/w keycloak and mysql in docker on azure, especially when the containers start.
I have used a simple docker-compose.yml
(instead of dockerfile) and setup with the official images and proper healthchecks, make sure the keycloak only starts after mysql is ready.
docker-compose.yml
file like this:
version: "3.8"
services:
mysql:
image: mysql:8.0
container_name: keycloak-mysql
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: keycloak_db
MYSQL_USER: keycloak
MYSQL_PASSWORD: password
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 5s
timeout: 5s
retries: 5
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
keycloak:
image: bitnami/keycloak:24.0.4
container_name: keycloak
depends_on:
mysql:
condition: service_healthy
environment:
KEYCLOAK_DATABASE_VENDOR: mysql
KEYCLOAK_DATABASE_HOST: mysql
KEYCLOAK_DATABASE_PORT: 3306
KEYCLOAK_DATABASE_NAME: keycloak_db
KEYCLOAK_DATABASE_USER: keycloak
KEYCLOAK_DATABASE_PASSWORD: password
KEYCLOAK_ADMIN_USER: admin
KEYCLOAK_ADMIN_PASSWORD: admin
ports:
- "8080:8080"
volumes:
mysql_data:
Save the file and then > docker-compose up -d
the above process is working correctly it will also connect when MySQL database is ready before connecting to keycloak, i also added the health check to MySQL container the docker understand when MySQL is up and running. using the depends_on setting docker waits to start keycloak until MySQL is connections this process avoids making custom dockerfiles, if something is missing ot misconfigured it will be tricky and may cause errors. finally, by using PV for MySQL we make sure the database keeps its data even if the container restarts.
Make sure azure vm firewall and network security groups allow inbound traffic on port 8080.
Ensure docker is properly installed and running on your vm, you can view logs with docker-compose logs -f
to check container startup progress