user3714967
user3714967

Reputation: 1645

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 network:

- 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!

Upvotes: 81

Views: 46700

Answers (15)

oruchkin
oruchkin

Reputation: 1295

Had same issue, solved it by changing two lines in my docker-compose

From this:

KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: admin

To this:

KEYCLOAK_USER: newadmin
KEYCLOAK_PASSWORD: newadmin

and restart containers

Upvotes: 0

mbreat
mbreat

Reputation: 381

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

Upvotes: 11

FLCL
FLCL

Reputation: 2514

In case you want to add user before server start or want it look like a classic migration, build custom image with admin parameters passed

FROM quay.io/keycloak/keycloak:latest

ARG ADMIN_USERNAME
ARG ADMIN_PASSWORD

RUN /opt/jboss/keycloak/bin/add-user-keycloak.sh -u $ADMIN_USERNAME -p $ADMIN_PASSWORD

docker-compose:

  auth_service:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        ADMIN_USERNAME: ${KEYCLOAK_USERNAME}
        ADMIN_PASSWORD: ${KEYCLOAK_PASSWORD}

(do not add KEYCLOAK_USERNAME/KEYCLOAK_PASSWORD to the environment section)

Upvotes: 0

Albert Hidalgo
Albert Hidalgo

Reputation: 113

For other users with this problem and none of the previous answers have helped, check your connection to the database, this error usually appears if keycloak cannot connect to the database.

Test in Keycloak 8 with Docker.

Upvotes: 1

Toms Code
Toms Code

Reputation: 1699

This was happening to me when I used to shut down the Keycloak containers in Portainer and tried to get them up and running again.

I can prevent the error by also 'removing' the container after I've shut it down (both in Portainer) and then running docker-compose up. Make sure not to remove any volumes attached to your containers else you may lose data.

Upvotes: 0

Werner Raath
Werner Raath

Reputation: 1502

According to my findings, the best way to set this default user is NOT by adding it via environment variables, but via the following command:

docker exec <CONTAINER> /opt/jboss/keycloak/bin/add-user-keycloak.sh -u <USERNAME> -p <PASSWORD>

As per the official documentation.

Upvotes: 7

Ernesto Casanova
Ernesto Casanova

Reputation: 149

I was facing this issue with Keycloak "jboss/keycloak:11.0.3" running in Docker, error:

User with username 'admin' already added to '/opt/jboss/keycloak/standalone/configuration/keycloak-add-user.json'

Adicional info, was running with PostgreSQL v13.2 also in Docker. I create some schemas for other resources but I wasn't creating the schema for the Keycloak, so the solution was for my case, run in postgres the create schema command:

CREATE SCHEMA IF NOT EXISTS keycloak AUTHORIZATION postgres;

NOTE: Hope this helps, none of other solutions shared in this post solved my issue.

Upvotes: -1

Jay
Jay

Reputation: 41

I use Keycloak 12 where I still see this problem when the startup is interrupted. I could see that removing the file "keycloak-add-user.json" and restarting the container works.

Idea is to integrate this logic into container startup. I developed a simple custom-entrypoint script.

#!/bin/bash
set -e

echo "executing the custom entry point script"

FILE=/opt/jboss/keycloak/standalone/configuration/keycloak-add-user.json
if [ -f "$FILE" ]; then
    echo "keycloak-add-user.json exist, hence deleting it"
    rm $FILE
fi
echo "executing the entry point script from original image"
source  "/opt/jboss/tools/docker-entrypoint.sh"

And I ensured to rebuild the keycloak image with appropriate adaptations to Entrypoint in Dockerfile during the initial deployment.

ARG DEFAULT_IMAGE_BASEURL_APPS

FROM "${DEFAULT_IMAGE_BASEURL_APPS}/jboss/keycloak:12.0.1"

COPY custom-entrypoint.sh /opt/jboss/tools/custom-entrypoint.sh

ENTRYPOINT [ "/opt/jboss/tools/custom-entrypoint.sh" ]

As our deployment is on-premise, the access to the development team is not that easy. All that our first line support could do is try giving a restart of the server where we deployed. Hence the idea of this workaround.

Upvotes: 4

ferozpuri
ferozpuri

Reputation: 386

Thomas Solutions is good but restarting all containers and start again is worthless because my docker-compose file has 7 services.

I resolved the issue in two steps.

  1. first I commend these two lines of code like other fellows did
 #- KEYCLOAK_USER=admin
 #- KEYCLOAK_PASSWORD=admin
  1. Then new terminal I run this command and it works.

docker-compose up keycloak

keycloak is a ServiceName

Upvotes: 1

ged
ged

Reputation: 33

The way I got past this was to replace set -eou pipefail with # set -eou pipefail within the container file systems.

Logged in as root on my docker host and then edited each of the files returned by this search:

find /var/lib/docker/overlay2 | grep /opt/jboss/tools/docker-entrypoint.sh

Upvotes: 1

You can also stop the containers and simply remove associated volumes.

If you don't know wiwh volume is associated to your keycloak container, run:

docker-compose down
for vol in $(docker volume ls --format {{.Name}}); do
    docker volume rm $vol
done

Upvotes: -7

Dimitris Samaras
Dimitris Samaras

Reputation: 116

I have tried the solution by Thomas as but it sometimes works sometimes does not.

The issue is that Keycloak on boot does not find the db required, so it gets interrupted as Zmey mentions. Have you tried in the second ansible job to add depends_on: - postgres ?

Having the same issue but with docker-compose, i first started with the postgres container in order to create the necessary dbs (manual step) docker-compose up postgres and the i booted the entire setup docker-compose up.

Upvotes: 0

Thomas
Thomas

Reputation: 2193

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.

Upvotes: 97

Zmey
Zmey

Reputation: 2558

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

Upvotes: 14

Merlin Becker
Merlin Becker

Reputation: 99

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

Upvotes: 9

Related Questions