Skip to content

Commit 5a0ed4b

Browse files
authored
add keycloak (#5)
* add keycloak * update docker-compose
1 parent 8eaa4f2 commit 5a0ed4b

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

.github/workflows/build-docker.yml

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ jobs:
3636
- run: |
3737
source ./tool.sh && build_image app-clash latest docker_app_clash/Dockerfile && push_image
3838
39+
## Keycloak
40+
docker_keycloak:
41+
name: 'keycloak'
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v4
45+
- run: |
46+
source ./tool.sh && build_image keycloak latest docker_keycloak/Dockerfile && push_image
47+
3948
## DevHub
4049
qpod_dev-hub:
4150
name: 'dev-hub'

docker_keycloak/Dockerfile

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Distributed under the terms of the Modified BSD License.
2+
3+
ARG BASE_NAMESPACE
4+
ARG BASE_IMG="jdk17"
5+
FROM ${BASE_NAMESPACE:+$BASE_NAMESPACE/}${BASE_IMG}
6+
7+
LABEL maintainer="haobibo@gmail.com"
8+
9+
COPY work /opt/utils/
10+
11+
RUN source /opt/utils/script-setup-keycloak.sh \
12+
&& setup_keycloak \
13+
&& echo "keycloak:x:0:root" >> /etc/group \
14+
&& echo "keycloak:x:1000:0:keycloak user:/opt/keycloak:/sbin/nologin" >> /etc/passwd \
15+
&& chown -R 1000:0 /opt/keycloak
16+
17+
ENV KEYCLOAK_HOME /opt/keycloak
18+
ENV KC_RUN_IN_CONTAINER true
19+
ENV KC_HEALTH_ENABLED true
20+
ENV KC_METRICS_ENABLED true
21+
22+
USER 1000
23+
EXPOSE 8080
24+
25+
WORKDIR ${KEYCLOAK_HOME}
26+
ENTRYPOINT ["tini", "-g", "--"]
27+
28+
# '-c' option make bash commands are read from string.
29+
# If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
30+
# '-o pipefail' prevents errors in a pipeline from being masked.
31+
# If any command in a pipeline fails, that return code will be used as the return code of the whole pipeline.
32+
# '--login': make bash first reads and executes commands from the file /etc/profile, if that file exists.
33+
# After that, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
34+
SHELL ["/bin/bash", "--login", "-o", "pipefail", "-c"]
35+
CMD ["/bin/bash", "--login", "bin/kc.sh", "start-dev"]
36+
37+
# HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD [ "curl", "--head", "-fsSk", "https://localhost:8080/health/ready" ]
38+
39+
# docker build -t qpod/keycloak --build-arg "BASE_NAMESPACE=qpod" .
40+
# (docker rm svc-keycloak || true) && docker run -d -p 8080:8080 --name=svc-keycloak --hostname=svc-keycloak qpod/keycloak
41+
# (docker rm svc-keycloak || true) && docker run -it -p 8080:8080 --name=svc-keycloak --hostname=svc-keycloak qpod/keycloak bin/kc.sh start-dev --verbose
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
version: '3.7'
2+
3+
services:
4+
db-postgres:
5+
# su postgres && psql -d keycloak -U pg-keycloak-username
6+
image: postgres:15
7+
container_name: db-postgres
8+
environment:
9+
POSTGRES_DB: keycloak
10+
POSTGRES_USER: pg-keycloak-username
11+
POSTGRES_PASSWORD: pg-keycloak-password
12+
13+
svc-keycloak:
14+
# image: qpod/keycloak
15+
build:
16+
context: ../
17+
dockerfile: Dockerfile
18+
args:
19+
BASE_NAMESPACE: qpod
20+
container_name: svc-keycloak
21+
# command: ["/bin/bash", "--login", "bin/kc.sh", "start-dev", "--verbose"]
22+
command: |
23+
/bin/bash -l -c '
24+
[ -f conf/server.keystore ] || keytool -genkeypair -storepass password -storetype PKCS12 -keyalg RSA -keysize 2048 \
25+
-dname "CN=server" -alias server -ext "SAN:c=DNS:localhost,IP:127.0.0.1" -keystore conf/server.keystore
26+
bin/kc.sh start --verbose --features=token-exchange \
27+
--https-port=8080
28+
# --db postgres --db-url jdbc:postgresql://db-postgres:5432/keycloak \
29+
# --db-username pg-keycloak-username \
30+
# --db-password pg-keycloak-password
31+
'
32+
environment:
33+
KC_HOSTNAME: localhost
34+
KC_DB: postgres
35+
KC_DB_URL: "jdbc:postgresql://db-postgres:5432/keycloak"
36+
KC_DB_USERNAME: "pg-keycloak-username"
37+
KC_DB_PASSWORD: "pg-keycloak-password"
38+
KEYCLOAK_ADMIN: keycloak-admin
39+
KEYCLOAK_ADMIN_PASSWORD: keycloak-password
40+
PROXY_ADDRESS_FORWARDING: "true"
41+
ports:
42+
- 8080:8080
43+
depends_on:
44+
- db-postgres
45+
healthcheck:
46+
test: ["CMD-SHELL", "curl", "--head", "-fsSk", "https://localhost:8080/health/ready" ]
47+
interval: 30s
48+
timeout: 30s
49+
start_period: 5s
50+
retries: 3
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
source /opt/utils/script-utils.sh
2+
3+
setup_keycloak() {
4+
# Install the latest (but not nightly) version of keycloak
5+
VERSION_KEYCLOAK=$(curl -sL https://github.com/keycloak/keycloak/releases.atom | grep 'releases/tag' | grep -v nightly | head -1 | grep -Po '\d[\d.]+' ) \
6+
&& URL_KEYCLOAK="https://github.com/keycloak/keycloak/releases/download/$VERSION_KEYCLOAK/keycloak-$VERSION_KEYCLOAK.tar.gz" \
7+
&& echo "Downloading Keycloak version ${VERSION_KEYCLOAK} from: ${URL_KEYCLOAK}" \
8+
&& install_tar_gz $URL_KEYCLOAK \
9+
&& mv /opt/keycloak-* /opt/keycloak && mkdir -pv /opt/keycloak/data \
10+
&& chmod -R g+rwX /opt/keycloak \
11+
&& echo 'export PATH=${PATH}:/opt/keycloak/bin' >> /etc/profile.d/path-keycloak.sh \
12+
&& export PATH=${PATH}:/opt/keycloak/bin \
13+
&& echo "@ Version of Keycloadk $(kc.sh --version)"
14+
}

0 commit comments

Comments
 (0)