User 'admin' already added to Keycloak 8

Issue: I Cannot Start Keycloak Container Using Ansible and Docker Compose

I am attempting to start a Keycloak container using Ansible and Docker Compose, but I am receiving the following error:

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

Below is the code I am using for the 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

I am downgrading Keycloak to version 7 and it is starting normally. Does anyone have any idea why I am getting this error when trying to start the Keycloak container with version 8?

Answer:

The error message indicates that the admin user is already added to the Keycloak configuration file, so it cannot be added again. This may be because the container was started previously and the configuration file was not properly cleaned up.

To resolve this issue, you can try removing the Keycloak container and its associated volumes to start fresh. You can do this by running the following command:

docker-compose -p appauth down -v

Once the container and volumes are removed, you can try starting the Keycloak container again using the same Ansible task. This should create a new container with a fresh configuration file that does not have the admin user already added.