DEV Community

Cover image for Keycloak & Docker integration tutorial: Learn how to do it step by step
Michał Zięba
Michał Zięba

Posted on • Originally published at pretius.com

Keycloak & Docker integration tutorial: Learn how to do it step by step

Do you want to configure a Keycloak - Docker integration for your Angular app? Read on to find out how to set everything up.

Keycloak is a powerful authentication and authorization solution that provides plenty of useful features, such as roles and subgroups, advanced password policy and single sign-on (if you’re new to Keycloak, check out our Keycloak SSO guide for 2024. It’s also very easy to integrate with other solutions. 

We’ve already shown you how to connect Keycloak to your Angular app, but there’s more you can do. For example, by integrating this technology with Cypress, you can enable the simulation of real-user login scenarios, including multi-factor authentication and social logins, ensuring that security protocols are correctly implemented and functioning as expected.

Most importantly, you can also use Docker containers to provide a portable and consistent environment across different platforms (possibly with container image scanning, for increased security). This integration ensures easy deployment, scalability, and efficient dependency management, streamlining the process of securing applications and services. Additionally, Docker Compose can be used to orchestrate multiple containers, simplifying complex configurations and enhancing the overall management of Keycloak instances.

This guide will show you precisely how to set all of this up. Let’s get started!

Prerequisites

The article is based on the contents of a GitHub repository consisting of several elements:

  • Frontend application written in Angular
  • Keycloak configuration
  • E2E tests written in Cypress
  • Docker configuration for the whole stack

The point of this tech stack is to allow users to work with Angular/Keycloak/Cypress locally and also in Docker containers. 

Keycloak configuration

Image description

We’ll start by setting up Keycloak, which is a crucial part of both configurations. The idea is to run it inside a Docker container and exposed at http://localhost:8080. Keycloak has predefined configurations, including users, realm and client ID, so setting it up for this project requires minimum effort.

Normal user

Your normal user in the Keycloak panel should be configured using the following details:

  • User: test
  • Password: sIjKqg73MTf9uTU

Keycloak administrator

Here’s the default configuration for the admin user (of course, you probably shouldn’t use default settings for the admin account in real-world scenarios).

  • User: admin
  • Password: admin

Local configuration

This configuration allows you to work locally with an Angular application in dev mode along with E2E tests. It requires Keycloak to be run and available on http://localhost:8080. This is set in the Docker configuration, which is partially used here. To run the configuration locally, use the following commands in the command line. 

First, in the main project directory:

npm install
Enter fullscreen mode Exit fullscreen mode

In /e2e directory:

npm install
Enter fullscreen mode Exit fullscreen mode

In the main directory for frontend application development:

npm run start
Enter fullscreen mode Exit fullscreen mode

In /e2e directory:

npm run cy:run
Enter fullscreen mode Exit fullscreen mode

In the main project directory:

docker-compose up -d keycloak
Enter fullscreen mode Exit fullscreen mode

Docker configuration

Installing and configuring Docker is a relatively simple matter – the solution provides detailed documentation you can use if you run into any problems.

In the context of our project, the Docker configuration does several key things:

  1. Running Keycloak and importing the predefined realm along with users
  2. Building and exposing the Angular application on http://localhost:4200 via nginx on a separate Docker container
  3. Running e2e container to allow you to run tests via Cypress

To run a dockerized configuration, type in the command line in the main project directory:

  docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

To run Cypress tests inside the container, use the following command:

docker container exec -ti e2e bash
Enter fullscreen mode Exit fullscreen mode

Then, inside the container, run:

  npm run cy:run
Enter fullscreen mode Exit fullscreen mode

Test artifacts are connected to the host machine via volume, so test reports, screenshots and videos will be available immediately on path /e2e/cypress/ in the following folders: reports, screenshots and videos.

Conclusion

And that’s about it. As you can see, integrating Keycloak (or rather an Angular app that uses Keycloak), Docker and Cypress is a relatively straightforward process. There are only a couple of steps you must take to get a consistent, containerized environment for easy deployment, scaling and efficient dependency management – with the added benefit of real-user login scenario simulation thanks to Cypress for top-notch security.

Feel free to use our repository on GitHub. And if you need to set up an enterprise-grade project based on these technologies, let us know at hello@pretius.com (or by using the contact form below this blogpost). At Pretius, we strive in such software projects so there’s a big chance that we could help you do it.

In the meantime, if you’re interested in content related to Keycloak or Angular, you can also check out some of the other publications on our blog:

  1. Keycloak-Angular integration: Practical tutorial for connecting your app to a powerful IAM system
  2. Keycloak SSO guide for 2024: Advantages, installation, protips, and the real cost
  3. Keycloak tutorial: Learn how to use this technology's advanced features
  4. AngularJS upgrade: Why 2023 is the time for migrating and how to do it
  5. Angular vs React: In-depth comparison of the most popular front-end technologies

Top comments (0)