Answers: <p>I think the <code>KC_DB_</code> environment variables are supposed to be just <code>DB_</code>. If I rewrite your <code>compose.yaml</code> like this...</p>
  <pre><code>  keycloak:
        container_name: keycloak_keycloak
        image: quay.io/keycloak/keycloak:22.0.3
        environment:
          DB: MYSQL
          DB_URL_HOST: mysql
          DB_SCHEMA: keycloak
          DB_USERNAME: keycloak
          DB_PASSWORD: MyKeycloakMySQLPassword
  </code></pre>
  <p>...then it starts up without errors and I can connect to the console at <a href="http://localhost:8080/keycloak/" rel="nofollow noreferrer">http://localhost:8080/keycloak/</a>.</p>
  <hr />
  <p>A couple of comments unrelated to your question:</p>
  <p>Your <code>depends_on</code> is effectively a no-op, because it just waits for successful container startup; it doesn't know whether or not mysql is ready to accept connections. To make it useful you need to (a) implement a health check on the mysql container and then (b) use the extended form of <code>depends_on</code>. Something like:</p>
  <pre><code>volumes:
    mysql_data2:
      driver: local
  
  services:
  
    mysql:
      image: mysql:5.7
      volumes:
        - mysql_data2:/var/lib/mysql
      environment:
        MYSQL_ROOT_PASSWORD: MyRootPassword
        MYSQL_DATABASE: keycloak
        MYSQL_USER: keycloak
        MYSQL_PASSWORD: MyKeycloakMySQLPassword
  
        # This provides the password to the healthcheck command
        MYSQL_PWD: MyRootPassword
      healthcheck:
        test:
          - CMD
          - mysql
          - -e
          - select 1
        interval: 10s
        start_period: 20s
        timeout: 5s
  
    keycloak:
      depends_on:
        mysql:
          condition: service_healthy
      image: quay.io/keycloak/keycloak:22.0.3
      environment:
        DB: MYSQL
        DB_URL_HOST: mysql
        DB_SCHEMA: keycloak
        DB_USERNAME: keycloak
        DB_PASSWORD: MyKeycloakMySQLPassword
        KEYCLOAK_ADMIN: admin
        KEYCLOAK_ADMIN_PASSWORD: MyKeycloakAdminPassword
        KC_HOSTNAME_STRICT: 'false'
        KC_HTTP_RELATIVE_PATH: /keycloak
        KC_PROXY: edge
      ports:
        - 8080:8080
        - 8443:8443
      command:
        - start
  </code></pre>
  <p>Lastly, on my system I was experiencing very long startup times for the mysql container. I was able to resolve this by constraining the number of open files:</p>
  <pre><code>  mysql:
      ulimits:
        nofile:
          soft: 1024
          hard: 1024
  </code></pre>
  <p>This is probably not appropriate for a production configuration, but it reduced the startup time from at least a minute to just a few seconds.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-09-18T06:29:46.170Z
Answers: Weird, that doesn't match the documentation. But if it works, it works. Thanks for the answer. I'll try this out later, right now I have it running using Postgres. To be honest, I don't really mind the database that is uses, all examples use Postgres. It's just that I only have used MySQL before. I should add a health check. Right now it's just a development environment. For the production, I should take more care to do things right.
Answers: Bas Visscher
Answers: CC BY-SA 4.0
Answers: 2024-02-29T20:59:04.363Z
Answers: This worked for me, too. I was running with `KC_DB_*` variables (per the documentation https://www.keycloak.org/server/containers), but removing the `KC_` prefix made things work. I think this should be the accepted answer.
Answers: thelr
Answers: CC BY-SA 4.0
Answers: 2023-09-17T00:30:24.473Z
Answers: 2024-02-29T21:02:44.007Z
Answers: 2024-02-29T21:02:44.007Z
Answers: larsks
Body: <p>I've been trying to get keycloak running for a bit now. I had it going using the legacy tag but it seems like bad practice to get started with a legacy version. So I wanted to get the most recent version running. This is my docker compose config, but somehow It wont work.
  It has something todo with the database connection but I cant figure out what's going wrong.</p>
  <pre><code>version: '3'
  volumes:
    mysql_data2:
        driver: local
  services:
    mysql:
        container_name: keycloak_mysql
        image: mysql:5.7
        volumes:
          - mysql_data2:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: MyRootPassword
          MYSQL_DATABASE: keycloak
          MYSQL_USER: keycloak
          MYSQL_PASSWORD: MyKeycloakMySQLPassword
    keycloak:
        container_name: keycloak_keycloak
        image: quay.io/keycloak/keycloak:22.0.3
        environment:
          KC_DB: MYSQL
          KC_DB_URL_HOST: mysql
          KC_DB_SCHEMA: keycloak
          KC_DB_USERNAME: keycloak
          KC_DB_PASSWORD: MyKeycloakMySQLPassword
          KEYCLOAK_ADMIN: admin
          KEYCLOAK_ADMIN_PASSWORD: MyKeycloakAdminPassword
          KC_HOSTNAME_STRICT: &quot;false&quot;
          KC_HTTP_RELATIVE_PATH: &quot;/keycloak&quot;
          KC_PROXY: &quot;edge&quot;
        ports:
          - 8080:8080
          - 8443:8443 
        depends_on:
          - mysql
        command:
          - &quot;start&quot;
  </code></pre>
  
Tags: <docker-compose><mysql><keycloak>
Title: Setup keycloak with mysql via docker compose
UserDisplayName: Bas Visscher
ThreadText: # Setup keycloak with mysql via docker compose
  
  (Asked by: username_0 on 2023-09-15)
  
  I've been trying to get keycloak running for a bit now. I had it going using the legacy tag but it seems like bad practice to get started with a legacy version. So I wanted to get the most recent version running. This is my docker compose config, but somehow It wont work.
  It has something todo with the database connection but I cant figure out what's going wrong.
  
  ```
  version: '3'
  volumes:
    mysql_data2:
        driver: local
  services:
    mysql:
        container_name: keycloak_mysql
        image: mysql:5.7
        volumes:
          - mysql_data2:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: MyRootPassword
          MYSQL_DATABASE: keycloak
          MYSQL_USER: keycloak
          MYSQL_PASSWORD: MyKeycloakMySQLPassword
    keycloak:
        container_name: keycloak_keycloak
        image: quay.io/keycloak/keycloak:22.0.3
        environment:
          KC_DB: MYSQL
          KC_DB_URL_HOST: mysql
          KC_DB_SCHEMA: keycloak
          KC_DB_USERNAME: keycloak
          KC_DB_PASSWORD: MyKeycloakMySQLPassword
          KEYCLOAK_ADMIN: admin
          KEYCLOAK_ADMIN_PASSWORD: MyKeycloakAdminPassword
          KC_HOSTNAME_STRICT: "false"
          KC_HTTP_RELATIVE_PATH: "/keycloak"
          KC_PROXY: "edge"
        ports:
          - 8080:8080
          - 8443:8443 
        depends_on:
          - mysql
        command:
          - "start"
  
  ```
  
  **Tags:** docker-compose, mysql, keycloak
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-09-17. Score: 1 | Accepted answer
  
  I think the `KC_DB_` environment variables are supposed to be just `DB_`. If I rewrite your `compose.yaml` like this...
  
  ```
    keycloak:
        container_name: keycloak_keycloak
        image: quay.io/keycloak/keycloak:22.0.3
        environment:
          DB: MYSQL
          DB_URL_HOST: mysql
          DB_SCHEMA: keycloak
          DB_USERNAME: keycloak
          DB_PASSWORD: MyKeycloakMySQLPassword
  
  ```
  
  ...then it starts up without errors and I can connect to the console at <http://localhost:8080/keycloak/>.
  
  ---
  
  A couple of comments unrelated to your question:
  
  Your `depends_on` is effectively a no-op, because it just waits for successful container startup; it doesn't know whether or not mysql is ready to accept connections. To make it useful you need to (a) implement a health check on the mysql container and then (b) use the extended form of `depends_on`. Something like:
  
  ```
  volumes:
    mysql_data2:
      driver: local
  
  services:
  
    mysql:
      image: mysql:5.7
      volumes:
        - mysql_data2:/var/lib/mysql
      environment:
        MYSQL_ROOT_PASSWORD: MyRootPassword
        MYSQL_DATABASE: keycloak
        MYSQL_USER: keycloak
        MYSQL_PASSWORD: MyKeycloakMySQLPassword
  
        # This provides the password to the healthcheck command
        MYSQL_PWD: MyRootPassword
      healthcheck:
        test:
          - CMD
          - mysql
          - -e
          - select 1
        interval: 10s
        start_period: 20s
        timeout: 5s
  
    keycloak:
      depends_on:
        mysql:
          condition: service_healthy
      image: quay.io/keycloak/keycloak:22.0.3
      environment:
        DB: MYSQL
        DB_URL_HOST: mysql
        DB_SCHEMA: keycloak
        DB_USERNAME: keycloak
        DB_PASSWORD: MyKeycloakMySQLPassword
        KEYCLOAK_ADMIN: admin
        KEYCLOAK_ADMIN_PASSWORD: MyKeycloakAdminPassword
        KC_HOSTNAME_STRICT: 'false'
        KC_HTTP_RELATIVE_PATH: /keycloak
        KC_PROXY: edge
      ports:
        - 8080:8080
        - 8443:8443
      command:
        - start
  
  ```
  
  Lastly, on my system I was experiencing very long startup times for the mysql container. I was able to resolve this by constraining the number of open files:
  
  ```
    mysql:
      ulimits:
        nofile:
          soft: 1024
          hard: 1024
  
  ```
  
  This is probably not appropriate for a production configuration, but it reduced the startup time from at least a minute to just a few seconds.
  
  > **Comments:**
  > **username_0**: Weird, that doesn't match the documentation. But if it works, it works. Thanks for the answer. I'll try this out later, right now I have it running using Postgres. To be honest, I don't really mind the database that is uses, all examples use Postgres. It's just that I only have used MySQL before. I should add a health check. Right now it's just a development environment. For the production, I should take more care to do things right.  
  > **username_2**: This worked for me, too. I was running with `KC_DB_*` variables (per the documentation https://www.keycloak.org/server/containers), but removing the `KC_` prefix made things work. I think this should be the accepted answer.  
  
  ---
  
  