简体   繁体   English

从现有的 Dockerfile 派生 + 将 USER 设置为非 root

[英]Deriving FROM existing Dockerfile + setting USER to non-root

I'm trying to find a generic best practice for how to:我正在尝试找到一个通用的最佳实践来:

  1. Take an arbitrary (parent) Dockerfile, eg one of the official Docker images that run their containerized service as root,采用任意(父)Dockerfile,例如,以 root 身份运行其容器化服务的官方 Docker 映像之一,
  2. Derive a custom (child) Dockerfile from it (via FROM... ),从中派生一个自定义(子)Dockerfile(通过FROM... ),
  3. Adjust the child in the way that it runs the same service as the parent , but as non-root user.调整child使其运行与parent相同的服务,但作为非 root 用户。

I've been searching and trying for days now but haven't been able to come up with a satisfying solution.我已经搜索和尝试了好几天,但一直未能找到令人满意的解决方案。

I'd like to come up with an approach eg similar to the following, simply for adjusting the user the original service runs as:我想提出一种方法,例如类似于以下内容,只是为了调整用户,原始服务运行为:

FROM mariadb:10.3

RUN chgrp -R 0 /var/lib/mysql && \
    chmod g=u /var/lib/mysql

USER 1234

However, the issue I'm running into again and again is whenever the parent Dockerfile declares some path as VOLUME (in the example above actually VOLUME /var/lib/mysql ), that effectively makes it impossible for the child Dockerfile to adjust file permissions for that specific path.但是,我一次又一次遇到的问题是,每当Dockerfile 将某些路径声明为VOLUME (在上面的示例中实际上是VOLUME /var/lib/mysql )时,这实际上使Dockerfile 无法调整文件权限对于该特定路径。 The chgrp & chmod are without effect in that case, so the resulting docker container won't be able to start successfully, due to file access permission issues.在这种情况下chgrpchmod无效,因此由于文件访问权限问题,生成的 docker 容器将无法成功启动。

I understand that the VOLUME directive works that way by design and also why it's like that , but to me it seems that it completely prevents a simple solution for the given problem: Taking a Dockerfile and adjusting it in a simple, clean and minimalistic way to run as non-root instead of root.我知道VOLUME指令是按设计方式工作的,以及为什么会这样,但对我来说,它似乎完全阻止了给定问题的简单解决方案:采用 Dockerfile 并以简单、干净和简约的方式对其进行调整以非 root 而不是 root 身份运行。

The background is: I'm trying to run arbitrary Docker images on an Openshift Cluster.背景是:我正在尝试在 Openshift 集群上运行任意 Docker 图像。 Openshift by default prevents running containers as root , which I'd like to keep that way, as it seems quite sane and a step into the right direction, security-wise. 默认情况下,Openshift 会阻止以 root 身份运行容器,我想保持这种方式,因为它看起来很理智,并且朝着正确的方向迈出了一步,安全方面。

This implies that a solution like gosu , expecting the container to be started as root in order to drop privileges during runtime isn't good enough here.这意味着像gosu这样的解决方案,期望容器以 root 身份启动以便在运行时放弃特权,在这里还不够好。 I'd like to have an approach that doesn't require the container to be started as root at all, but only as the specified USER or even with a random UID.我想要一种方法,它根本不需要容器以 root 身份启动,而只需要以指定的USER甚至随机 UID 身份启动。

The unsatisfying approaches that I've found until now are:到目前为止,我发现的不满意的方法是:

  • Copy the parent Dockerfile and adjust it in the way necessary (effectively duplicating code)复制Dockerfile 并以必要的方式调整(有效复制代码)
  • sed / awk through all the service's config files during build time to replace the original VOLUME path with an alternate path, so the chgrp and chmod can work (leaving the original VOLUME path orphaned). sed / awk在构建期间通过所有服务的配置文件将原始VOLUME路径替换为备用路径,因此chgrpchmod可以工作(使原始VOLUME路径成为孤立的)。

I really don't like these approaches, as they require to really dig into the logic and infrastructure of the parent Dockerfile and how the service itself operates.我真的不喜欢这些方法,因为它们需要真正深入了解Dockerfile 的逻辑和基础架构以及服务本身的运行方式。

So there must be better ways to do this, right?所以必须有更好的方法来做到这一点,对吧? What is it that I'm missing?我错过了什么? Help is greatly appreciated.非常感谢您的帮助。

Permissions on volume mount points don't matter at all, the mount covers up whatever underlying permissions were there to start with.卷挂载点的权限根本不重要,挂载覆盖了任何底层权限。 Additionally you can set this kind of thing at the Kubernetes level rather than worrying about the Dockerfile at all.此外,您可以在 Kubernetes 级别设置这种东西,而不用担心 Dockerfile。 This is usually though a PodSecurityPolicy but you can also set it in the SecurityContext on the pod itself.这通常是一个 PodSecurityPolicy,但您也可以在 Pod 本身的 SecurityContext 中设置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM