8000 Add how to install extensions to the container docs by andreaTP · Pull Request #11025 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions docs/guides/src/main/server/containers.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ includedOptions="db db-url db-username db-password features hostname https-key-s

Keycloak handles containerized environments such as Kubernetes or OpenShift as first-class citizens. This guide describes how to optimize and run the Keycloak container image to provide the best experience running a Keycloak container.

== Creating an optimized container image
For the best start up of your Keycloak container, build an optimized container image by running the `build` step before starting.
== Creating a customized and optimized container image
The default Keycloak container image ships ready to be configured and optimized.

For the best start up of your Keycloak container, build an image by running the `build` step during the container build.
This step will save time in every subsequent start phase of the container image.

=== Building your optimized Keycloak docker image
The following `Dockerfile` creates a pre-configured Keycloak image that enables the health and metrics endpoints, enables the token exchange feature, and uses a PostgreSQL database.
Expand All @@ -25,15 +28,15 @@ ENV KC_HEALTH_ENABLED=true
ENV KC_METRICS_ENABLED=true
ENV KC_FEATURES=token-exchange
ENV KC_DB=postgres
# Install custom providers
8000 RUN curl -sL https://github.com/aerogear/keycloak-metrics-spi/releases/download/2.5.3/keycloak-metrics-spi-2.5.3.jar -o /opt/keycloak/providers/keycloak-metrics-spi-2.5.3.jar
RUN /opt/keycloak/bin/kc.sh build

FROM quay.io/keycloak/keycloak:latest
COPY --from=builder /opt/keycloak/lib/quarkus/ /opt/keycloak/lib/quarkus/
COPY --from=builder /opt/keycloak/ /opt/keycloak/
@armujahid armujahid Apr 23, 2022 •
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to copy full /opt/keycloak ? Can't we just copy ./lib/quarkus/ and ./providers instead?

COPY --from=builder /opt/keycloak/lib/quarkus/ /opt/keycloak/lib/quarkus/
COPY --from=builder /opt/keycloak/providers/ /opt/keycloak/providers/

@armujahid armujahid Apr 23, 2022 •
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get about ~200MB lighter image if I follow this approach instead of fully copying /opt/keycloak/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreaTP any answer to that question? Would also be interested in it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@armujahid It should work fine because files at lib/quarkus are what really changes after a build.

If you want to send a PR with this suggestion to our guide, I'm happy to review it and make this more explicit.

WORKDIR /opt/keycloak
# for demonstration purposes only, please make sure to use proper certificates in production instead
RUN keytool -genkeypair -storepass password -storetype PKCS12 -keyalg RSA -keysize 2048 -dname "CN=server" -alias server -ext "SAN:c=DNS:localhost,IP:127.0.0.1" -keystore conf/server.keystore
ENV KEYCLOAK_ADMIN=admin
ENV KEYCLOAK_ADMIN_PASSWORD=change_me
# change these values to point to a running postgres instance
ENV KC_DB_URL=<DBURL>
ENV KC_DB_USERNAME=<DBUSERNAME>
Expand All @@ -43,12 +46,18 @@ ENTRYPOINT ["/opt/keycloak/bin/kc.sh", "start"]
----
The build process includes multiple stages:

* The `build` command applies options that create an optimized image.
* The files generated by the `build` process are copied into a new image.
* The `build` command applies options and includes custom providers to create an optimized image.
* The files generated by the `build` stage are copied into a new image.
* In this runner image, the specific run configuration is applied. That configuration contains a keystore, the environment-specific hostname configuration, and database configuration.
* In the entrypoint, the `start` command starts the image in production mode.

This example uses a multi-staged build to demonstrate the build and run steps. However, you can also run this process as a single-staged docker build.
This example uses a multi-staged build to demonstrate the build and run steps. However, you can also build a single-staged docker image by removing the following two lines:

[source, dockerfile]
----
FROM quay.io/keycloak/keycloak:latest
COPY --from=builder /opt/keycloak/ /opt/keycloak/
----

=== Building the docker image
To build the actual docker image, run the following command from the directory containing your Dockerfile:
Expand Down
0