This documentation covers using Keycloak with Docker in this repository.
This project only contains a sample ui and api that uses keycloak for authentication.
sequenceDiagram
participant UI
participant API
participant KC as Keycloak
UI->>KC: Login
KC-->>UI: Login flow completed (auth code)
UI->>API: Login by code
API->>KC: Request token using code
KC-->>API: Token created
API->>API: Read username claim
API->>API: Create API token
API-->>UI: Return access token
UI->>UI: Write access token to localStorage
UI->>UI: Redirect to index
Configurations provided for configuring Keycloak can be specified in the
environment, or they can also be provided in a .conf file in the format
<key-with-dashes>=<value>.
For a detailed example, see compose.yml.
For more information on configuration options and setup, see All Configs.
Note
start runs in production mode, start-dev runs in development mode
Warning
Production requires additional setup:
- Don't expose HTTP port (8080) in compose file
- Hostname configuration is required
- HTTPS/TLS configuration is required
For faster startup in containers, use the recommended flow:
- Build once normally
- Start with
--optimizedto reuse the build
If runtime build config conflicts with a pre-build, the pre-built assets take precedence.
Keycloak creates a realm named master by default in first start. An admin
login is required to perform operations in this master realm. We provide these
user credentials via the environment as KC_BOOTSTRAP_ADMIN_USERNAME and
KC_BOOTSTRAP_ADMIN_PASSWORD. This user is opened as a temporary user. It is
not recommended to continue using this user.
Tip
To manage keycloak, you can use cli script kcadm.sh in a separate docker
service from a shell script such as init.sh
Realms isolate users and configuration. The master realm is designated as the administrator realm. If a client and a regular user are to be added, a separate realm must be created.
To create a new realm,
- login using the admin user under the master realm (assuming it is newly
created, this will be the one you created with
KC_BOOTSTRAP_ADMIN_X). - You can create a new realm by simply entering a
nameand selectingenableon the manage realm page, accessible from the side menu.
After creating a realm, you automatically navigate to that realm. At this point, clients will be created within whichever realm you are currently in.
To create a client:
- Open the new client creation screen from the Clients tab in the side menu
- Enter a
Client IDand ensure that theClient Protocolis set toopenid-connect. Then click next - Make sure
client authenticationis enabled, then you can click next. - After entering the
Root URL, please enterValid redirect URIs, considering the possible redirect URLs as well. It is important to note that after the root URL is provided, it is added to the beginning of the redirect URL. If the redirect URL provided for the auth code is not within the valid scope, it is blocked - Now you can save it
When API requests are sent through this client, the client_secret must be
provided. You can find this secret information in the credentials section of the
Clients page.
User creation can also be easily done by going to their own page. However, the
user's password is set by going to the user's page after the user is created and
doing it from the Credentials tab.