Files
CasService/docker/Dockerfile
T
chenxing dc8a0924a8
build / build-push (push) Successful in 2m45s
修复容器 hostname 无法解析导致的启动报错
现象:CAS banner 后持续 ERROR Could not determine local host name
UnknownHostException: <容器短ID>: Temporary failure in name resolution。

根因:容器 hostname 默认为容器短 ID,当前网络环境下 /etc/hosts 未含该行、
内网 DNS 亦无法解析,InetAddress.getLocalHost() 失败;连锁导致 CAS
TicketIdSanitizationUtils 静态初始化抛 ExceptionInInitializerError(票据日志带病)。

修复:ENTRYPOINT 改为 entrypoint.sh,启动前把 hostname 兜底写入 /etc/hosts
(已存在则跳过),再 exec java -jar cas.war。不依赖 projman 侧改动,任何部署方式均生效。
2026-08-31 20:11:11 +08:00

33 lines
2.1 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CAS 5.3.16 运行镜像(单阶段,war 预构建)
# CI 已先 mvn package 并在容器内把产物 cp 成固定名 target/app.jar(= cas.war,可执行 WAR)
# 构建上下文 = 仓库根:docker build -f docker/Dockerfile .
# 健康检查(projman 应用配置 healthPath=/cas/status + HTTPS 自签名开):curl -fk https://localhost:8443/cas/status
# 时区 Asia/Shanghai;JAVA_OPTS 由 projman 部署 env 注入(默认 -Xms512m -Xmx1024m)
FROM eclipse-temurin:8-jre
# CAS 5.3 默认要求 HTTPS(安全 Cookie),生成自签名密钥库(密码固定 changeit,过渡期用)
RUN mkdir -p /etc/cas/config \
&& keytool -genkeypair -alias cas -keyalg RSA -keysize 2048 -validity 3650 \
-storetype JKS -keystore /etc/cas/cas.keystore \
-storepass changeit -keypass changeit -dname "CN=cas.example.org" \
&& apt-get update && apt-get install -y --no-install-recommends curl tzdata \
&& ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY target/app.jar /app/cas.war
COPY docker/cas.properties /etc/cas/config/cas.properties
COPY etc/cas/config/log4j2.xml /etc/cas/config/log4j2.xml
# 镜像内 cas.properties 仅保留环境无关配置(HTTPS/端口/日志/管理页白名单);
# 业务配置(cas.server.name / cas.authn.jdbc / Redis / custom.login.* 等)来自 projman
# 配置管理挂载的 /app/config/application.yml(Boot 1.5 自动加载 file:./config/ 且优先于 classpath),镜像保持环境无关。
EXPOSE 8443
# 兜底:把容器 hostname 写入 /etc/hosts。Docker 默认 hostname=容器短ID,某些 network_mode 下
# /etc/hosts 不含该行且内网 DNS 无法解析 → InetAddress.getLocalHost 抛 UnknownHostException,
# 导致 CAS TicketIdSanitizationUtils 类初始化失败(ExceptionInInitializerError),票据日志带病运行。
RUN printf '#!/bin/sh\nHN=$(hostname)\ngrep -qw "$HN" /etc/hosts || echo "127.0.0.1 $HN" >> /etc/hosts\nexec java ${JAVA_OPTS:--Xms512m -Xmx1024m} -Dfile.encoding=UTF-8 -jar /app/cas.war\n' > /app/entrypoint.sh \
&& chmod +x /app/entrypoint.sh
ENTRYPOINT ["/app/entrypoint.sh"]