75

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!

dur
  • 15,689
  • 25
  • 79
  • 125
user3714967
  • 1,575
  • 3
  • 14
  • 29
  • That user is probably there from the previous run. Clean postgres-data volume = start db from the scratch. – Jan Garaj Jan 05 '20 at 15:12
  • 3
    @JanGaraj no it is not. I run ansible playbook on clean environment and still get the same error. – user3714967 Jan 05 '20 at 16:07
  • 1
    I had the same issue. Deleting all docker containers and images and redownloading them somehow resolved it. – cib Jan 08 '20 at 09:41
  • @cib It appears it crashes if container is stopped then started again. Created an issue: https://issues.redhat.com/browse/KEYCLOAK-12896 – Zmey Feb 05 '20 at 01:08

15 Answers15

95

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.

Thomas
  • 2,155
  • 16
  • 22
14

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

Zmey
  • 2,304
  • 1
  • 24
  • 40
  • 1
    You can repair the failing container by first copying this file from the container to your local machine "docker cp keycloak:/opt/jboss/tools/docker-entrypoint.sh ." Comment out the line "set -eou pipefail", copy it back into the container using "docker cp docker-entrypoint.sh keycloak:/opt/jboss/tools/" and restart the container. After a clean startup/shutdown, you could restore (uncomment) this line again. – Bruno Ranschaert Sep 13 '22 at 11:15
9

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
9

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
mbreat
  • 361
  • 1
  • 3
  • 5
7

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.

Werner Raath
  • 1,322
  • 3
  • 16
  • 34
4

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.

Jay
  • 41
  • 1
  • 1
    Thanks, My current installation hasn't a database (is a keycloak standalone image). To restore, I 1. create a new image by `docker commit my-keycloak-container` and `docker tag keycloak-temp`; 2. create a temporary container from the new image: `docker run -it --entrypoint=sh keycloak-temp`; 3. rename the file `keycloak-add-user.json` to `keycloak-add-user.json.old`; 4. create a new image 5. run a new container with `--entrypoint=/opt/jboss/tools/docker-entrypoint.sh` – Paulo Mateus Mar 18 '21 at 22:51
  • You need to add permissions to the shell script on host: `chmod +x custom-entrypoint.sh`. – Eduardo Lucio Dec 01 '22 at 16:41
1

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
Corné
  • 1,304
  • 3
  • 13
  • 32
ged
  • 33
  • 8
1

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

ferozpuri
  • 266
  • 2
  • 9
1

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.

Albert Hidalgo
  • 113
  • 2
  • 9
0

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.

0

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.

Toms Code
  • 1,439
  • 3
  • 15
  • 34
0

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)

FLCL
  • 2,445
  • 2
  • 24
  • 45
0

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

oruchkin
  • 1,145
  • 1
  • 10
  • 21
-1

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.

Ernesto Casanova
  • 149
  • 1
  • 2
  • 8
-6

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
  • 6
    This script will remove all of your docker volumes, not only the one for keycloak container. – msrc Dec 15 '20 at 13:21