Skip to content

Commit e492518

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 e492518

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

‎scripts/keycloak-dev.sh‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,32 @@ 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+
54+
# In containerized CI (GitHub Actions with a job container), the Docker
55+
# CLI talks to the host daemon via a mounted socket. Port publishing
56+
# lands on the host, not inside this container. Share the job
57+
# container's network namespace so Keycloak is reachable on localhost.
58+
if [ "${GITHUB_ACTIONS:-}" = "true" ] &&
59+
[ -f /.dockerenv ] &&
60+
[ "$CTR" = "docker" ] &&
61+
$CTR inspect "$(hostname)" >/dev/null 2>&1; then
62+
port_args=()
63+
network_args=(--network "container:$(hostname)" --cap-drop ALL --security-opt no-new-privileges)
64+
keycloak_args=(start-dev --http-host=127.0.0.1 --http-port="${KEYCLOAK_PORT}" --import-realm)
65+
fi
66+
5067
$CTR run -d \
5168
--name "$CONTAINER_NAME" \
52-
-p "${KEYCLOAK_PORT}:8080" \
69+
"${network_args[@]}" \
70+
"${port_args[@]}" \
5371
-e KEYCLOAK_ADMIN=admin \
5472
-e KEYCLOAK_ADMIN_PASSWORD=admin \
5573
-v "${REALM_FILE}:/opt/keycloak/data/import/realm.json:ro,z" \
5674
"$KEYCLOAK_IMAGE" \
57-
start-dev --import-realm
75+
"${keycloak_args[@]}"
5876

5977
echo "Waiting for Keycloak to become healthy (up to ${HEALTH_TIMEOUT}s)..."
6078
local elapsed=0

0 commit comments

Comments
 (0)