16

I run keycloak standalone using a command for docker docker run -p 8080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:15.0.2

How to mount a volume to save the data, after container is stopped?

4 Answers 4

14

working docker-compose.yml

version: "3.7"

volumes:
  keycloak:

services:

  keycloak:
    image: quay.io/keycloak/keycloak:18.0.2
    ports:
      - 8080:8080
    environment:
      - KEYCLOAK_ADMIN=admin
      - KEYCLOAK_ADMIN_PASSWORD=admin
    volumes:
      - keycloak:/opt/keycloak/data/
    restart: always
    command:
      - "start-dev"
1
  • Worked for keycloak:23.0.4.
    – fabpico
    Commented Jul 5 at 12:24
8

For the production:

In the production environment Keycloak should be connected to a real database (mariadb, mssql, mysql, oracle, postgres). The data is stored there, thus there is no need to store anything running in the container.

For the development:

Keycloak comes with its own embedded Java-based relational database called H2. The data is stored in /opt/jboss/keycloak/standalone/data/ inside the container.

To start the container with a mounted volume you need:

1. Create a folder with read-write permissions:

mkdir -m 777 ./keycloak_data

2. Start the container with the mounted volume:

docker run -v ./keycloak_data:/opt/jboss/keycloak/standalone/data/ -p 8080:8080  -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:15.0.2

Note that if you mount the volume, the KEYCLOAK_USER and KEYCLOAK_PASSWORD will only be considered by the first start of the container, so to start the container again just use:

docker run -v ./keycloak_data:/opt/jboss/keycloak/standalone/data/ -p 8080:8080 quay.io/keycloak/keycloak:15.0.2

Note: Starting from Keycloak 21, the data path has changed to /opt/keycloak/data/h2

2
  • 2
    From Keycloak doc The H2 database is not very viable in high concurrency situations. You should to use external supported DB in real production env, not H2. So this answer is OK only for "dev" env.
    – Jan Garaj
    Commented Nov 2, 2021 at 15:33
  • 4
    In keycloak 21 (not sure when it changed), the new path to map for development is: /opt/keycloak/data/h2
    – mdryden
    Commented Apr 26, 2023 at 15:57
6

update 2023:

based on @mdryden and @Dennis Meissel response, in order to make keycloak persistent for local development, just run the following commands:

mkdir -m 777 ./keycloak_data
docker run -p 8080:8080 -v ./keycloak_data:/opt/keycloak/data/h2 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:22.0.3 start-dev
0

Just checked, if you simply stop and start your keycloak container via "docker desktop", the data e.g. about new realms will be still there. Also you can add a specific name to your container to find it between randomly generated ones.

docker run --name mykeycloak

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.