Skip to content

Commit 0e43f1c

Browse files
fix(ci): share network namespace with Keycloak in containerized CI
In GitHub Actions job containers, Docker port publishing lands on the host, not inside the job container. Detect this environment and attach Keycloak to the job container's network namespace instead, with hardened defaults (cap-drop ALL, no-new-privileges, loopback-only listener). Signed-off-by: Derek Carr <[email protected]>
1 parent 595195c commit 0e43f1c

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

‎scripts/keycloak-dev.sh‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,65 @@ cmd_start() {
4747

4848
echo "Starting Keycloak ($KEYCLOAK_IMAGE) on port $KEYCLOAK_PORT..."
4949

50+
local port_args=(-p "${KEYCLOAK_PORT}:8080")
51+
local network_args=()
52+
local keycloak_args=(start-dev --import-realm)
53+
local mount_args=(-v "${REALM_FILE}:/opt/keycloak/data/import/realm.json:ro,z")
54+
55+
# In containerized CI (GitHub Actions with a job container), the Docker
56+
# CLI talks to the host daemon via a mounted socket. Port publishing
57+
# lands on the host, not inside this container. Share the job
58+
# container's network namespace so Keycloak is reachable on localhost.
59+
if [ "${GITHUB_ACTIONS:-}" = "true" ] &&
60+
[ -f /.dockerenv ] &&
61+
[ "$CTR" = "docker" ] &&
62+
$CTR inspect "$(hostname)" >/dev/null 2>&1; then
63+
port_args=()
64+
network_args=(--network "container:$(hostname)" --cap-drop ALL --security-opt no-new-privileges)
65+
keycloak_args=(start-dev --http-host=127.0.0.1 --http-port="${KEYCLOAK_PORT}" --import-realm)
66+
67+
# The Docker daemon runs on the runner host. /__w exists only
68+
# inside the job container; /home/runner/_work is mounted at the
69+
# same path in both namespaces.
70+
case "$REALM_FILE" in
71+
/__w/*)
72+
local host_realm_file="/home/runner/_work/${REALM_FILE#/__w/}"
73+
;;
74+
*)
75+
echo "Error: unexpected GitHub workspace path: $REALM_FILE" >&2
76+
exit 1
77+
;;
78+
esac
79+
80+
if [ ! -f "$host_realm_file" ]; then
81+
echo "Error: host-visible realm file not found: $host_realm_file" >&2
82+
exit 1
83+
fi
84+
85+
# --mount fails when the source is absent; -v would silently create
86+
# a directory and let Keycloak start without importing the realm.
87+
mount_args=(
88+
--mount
89+
"type=bind,src=${host_realm_file},dst=/opt/keycloak/data/import/realm.json,readonly"
90+
)
91+
fi
92+
5093
$CTR run -d \
5194
--name "$CONTAINER_NAME" \
52-
-p "${KEYCLOAK_PORT}:8080" \
95+
"${network_args[@]}" \
96+
"${port_args[@]}" \
5397
-e KEYCLOAK_ADMIN=admin \
5498
-e KEYCLOAK_ADMIN_PASSWORD=admin \
55-
-v "${REALM_FILE}:/opt/keycloak/data/import/realm.json:ro,z" \
99+
"${mount_args[@]}" \
56100
"$KEYCLOAK_IMAGE" \
57-
start-dev --import-realm
101+
"${keycloak_args[@]}"
58102

59103
echo "Waiting for Keycloak to become healthy (up to ${HEALTH_TIMEOUT}s)..."
60104
local elapsed=0
61105
while [ $elapsed -lt $HEALTH_TIMEOUT ]; do
62-
if curl -sf "http://localhost:${KEYCLOAK_PORT}/realms/master" >/dev/null 2>&1; then
106+
if curl -sf \
107+
"http://localhost:${KEYCLOAK_PORT}/realms/openshell/.well-known/openid-configuration" \
108+
>/dev/null 2>&1; then
63109
echo "Keycloak is ready."
64110
print_info
65111
return 0

0 commit comments

Comments
 (0)