Keycloak 8: User with username 'admin' already added

I cannot start keycloak container using ansible and docker-compose. I'am getting error: User with username 'admin' already added to '/opt/jboss/keycloak/standalone/configuration/keycloak-add-user.json'

I have 3 ansible jobs:

Create netwrok:

- name: Create a internal network
  docker_network:
    name: internal

Setup postgres:

- name: "Install Postgres"
  docker_compose:
    project_name: posgressdb
    restarted: true
    pull: yes
    definition:
      version: '2'
      services:
        postgres:
          image: postgres:12.1
          container_name: postgres
          restart: always
          env_file:
            - /etc/app/db.env
          networks:
            - internal
          volumes:
            - postgres-data:/var/lib/postgresql/data
            - /etc/app/createdb.sh:/docker-entrypoint-initdb.d/init-app-db.sh
          ports:
            - "5432:5432"
      volumes:
        postgres-data:
      networks:
        internal:
          external:
            name: internal

Create keycloak container:

- name: Install keycloak
  docker_compose:
    project_name: appauth
    restarted: true
    pull: yes
    definition:
      version: '2'
      services:
        keycloak:
          image: jboss/keycloak:8.0.1
          container_name: keycloak
          restart: always
          environment:
            - DB_VENDOR=POSTGRES
            - DB_ADDR=postgres
            - DB_PORT=5432
            - DB_SCHEMA=public
            - DB_DATABASE=keycloak
            - DB_USER=keycloak
            - DB_PASSWORD=keycloak
            - KEYCLOAK_USER=admin
            - KEYCLOAK_PASSWORD=admin
          networks:
            - internal
      networks:
        internal:
          external:
            name: internal

Does anyone have any idea why I get this error?

EDIT

If I downgrade keycloak to version 7 it starts normally!


Just to clarify the other answers. I had the same issue. What helped for me was:

  1. stop all containers
  2. comment out the two relevant lines

    version: "3"
    
    services:
      keycloak:
        image: quay.io/keycloak/keycloak:latest
        environment:
          # KEYCLOAK_USER: admin
          # KEYCLOAK_PASSWORD: pass
          ...
    
  3. start all containers;

  4. wait until keycloak container has successfully started
  5. stop all containers, again
  6. comment back in the two lines from above

    version: "3"
    
    services:
      keycloak:
        image: quay.io/keycloak/keycloak:latest
        environment:
          KEYCLOAK_USER: admin
          KEYCLOAK_PASSWORD: pass
          ...
    
  7. start all containers

This time (and subsequent times) it worked. Keycloak was running and the admin user was registered and working as expected.


This happens when Keycloak is interrupted during boot. After this, command which attempts to add admin user starts to fail. In Keycloak 7 this wasn't fatal, but in 8.0.1 this line was added to /opt/jboss/tools/docker-entrypoint.sh which aborts the entire startup script:

set -eou pipefail

Related issue: https://issues.redhat.com/browse/KEYCLOAK-12896


I had the same issue. After commenting out the KEYCLOAK_USER env variables in docker-compose and updating the stack, the container started again.

docker_compose:
project_name: appauth
restarted: true
pull: yes
definition:
  version: '2'
  services:
    keycloak:
      image: jboss/keycloak:8.0.1
      container_name: keycloak
      restart: always
      environment:
        - DB_VENDOR=POSTGRES
        - DB_ADDR=postgres
        - DB_PORT=5432
        - DB_SCHEMA=public
        - DB_DATABASE=keycloak
        - DB_USER=keycloak
        - DB_PASSWORD=keycloak
        #- KEYCLOAK_USER=admin
        #- KEYCLOAK_PASSWORD=admin
      networks:
        - internal
  networks:
    internal:
      external:
        name: internal

The reason commenting out the KEYCLOAK_USER works is it forces a recreation of the container. The same can be accomplished with:

docker rm -f keycloak
docker compose up keycloak