【问题标题】:Adding ca-certificates to a Tomcat Docker container run with a designated user将 ca 证书添加到使用指定用户运行的 Tomcat Docker 容器
【发布时间】:2018-11-21 13:26:10
【问题描述】:

我有一个 Docker 卷直接安装到 /usr/local/share/ca-certificates -文件夹。

certificate-folder:/usr/local/share/ca-certificates:ro

我正在使用 Tomcat 进行此设置,但其他框架也可能遇到类似问题。 Dockerfile 的基础是这样的:

FROM       tomcat:8.5-jre8
# other Dockerfile configuration
CMD ["/start.sh"]

start.sh 包含关键行

#!/usr/bin/env bash
update-ca-certificates 
# other startup related tasks
catalina.sh run

此设置的问题在于,只要我以 root 用户身份运行容器,它就可以工作。但是,如果我尝试在 Dockerfile 末尾使用类似这样的方式更改为指定用户

ENV TOMCAT_USER="tomcat" \
    TOMCAT_UID="8080" \
    TOMCAT_GROUP="tomcat" \
    TOMCAT_GID="8080"
RUN groupadd -r --gid $TOMCAT_GID $TOMCAT_GROUP && \
    useradd -r --uid $TOMCAT_UID --gid $TOMCAT_GID $TOMCAT_USER
RUN chown -R $TOMCAT_USER:$TOMCAT_GROUP /usr/local/tomcat
USER $TOMCAT_USER

所以:

  • 由于 shell 脚本以 $TOMCAT_USER 身份运行,因此无法运行“update-ca-certificates”来安装证书。
  • 由于 Dockerfile 中没有添加证书,因此无法在 Dockerfile 中运行 update-ca-certificates。

因此,我最终会遇到这样的 SSL 问题

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: 
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

如果我仍希望将容器作为指定的 $TOMCAT_USER 运行,那么解决此类问题的正确方法是什么?

【问题讨论】:

    标签: docker ssl tomcat ssl-certificate dockerfile


    【解决方案1】:

    由于update-ca-certificates 需要root 权限来更新/etc/ssl/certs 中的证书,我只看到了三种可能的方法(以及一种可行的解决方案——下面的第三种):

    1. 从入口点删除 update-ca-certificates 并在 USER $TOMCAT_USER 行之前添加 RUN update-ca-certificates 命令。 (但是,您使用的卷在构建时不可用,所以这不起作用……)

    2. sudo 权限(无密码)授予您的$TOMCAT_USER,并将update-ca-certificates 替换为sudo update-ca-certificates。 (但是,从安全角度来看,这种解决方案可能并不令人满意……)

    3. 从 Dockerfile 中删除 USER $TOMCAT_USER;如果您愿意,请保留 CMD ["/start.sh"]ENTRYPOINT ["/start.sh"];并依赖gosu 工具,其主要用例正是从 root 降级到非特权用户,同时享受比 sudo w.r.t 更好的行为。 TTY 和信号转发。

      您只需要安装gosu,例如:

      RUN apt-get update -y -q && \
        DEBIAN_FRONTEND=noninteractive \
        apt-get install -y -q --no-install-recommends gosu
      

      (因为tomcat:8.5-jre8是基于Debian的)并通过编写来使用它:

      start.sh

      #!/usr/bin/env bash
      update-ca-certificates 
      # other startup related tasks
      exec gosu $TOMCAT_UID:$TOMCAT_GID catalina.sh run
      

    【讨论】:

    猜你喜欢
    • 2023-02-10
    • 2020-02-25
    • 2017-04-21
    • 2018-12-21
    • 2018-12-26
    • 2022-10-03
    • 1970-01-01
    • 2018-04-06
    • 2017-01-14
    相关资源
    最近更新 更多