如何在 Fargate 上的 Amazon ECS 中以非根用户身份 运行 docker 容器

How to run docker containers as a non-root user in Amazon ECS on Fargate

非 root 用户可以使用 USER 指令 运行 docker 个容器。
参考:https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user

我想在 Fargate 上的 Amazon ECS 中执行此操作。

非根用户如何在 Fargate 上的 Amazon ECS 中 运行 docker 容器?

提前致谢。

如果您碰巧管理 Dockerfile,您可以只包含 USER 指令以防止生成的容器 运行 以 root 身份运行。您还可以创建一个 ad-hoc Dockerfile,其中 FROM 指令指向您想要 运行 作为 non-root 的图像,以及 USER 指令。所有这些都需要使用 Dockerfile 并自行构建图像。

其他替代方案是删除 shell,这是您可以在容器开始使用 rm 后执行的操作,但这是一个非常基本的缓解措施,不会阻止访问敏感数据在容器中。

最后,如果使用 Kubernetes,则有一个策略是 运行 作为用户或组。我将直接粘贴此答案的源代码片段:AWS - EKS Best Practices Guide

The Kubernetes podSpec includes a set of fields under spec.securityContext, that allow to let you specify the user and/or group to run your application as. These fields are runAsUser and runAsGroup respectively. You can mandate the use of these fields by creating a pod security policy. See https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups for further information on this topic.

当 运行 Amazon ECS 中的容器镜像作为 Fargate 任务时,有参数“user”、“privileged”,如下所述:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html 可用于指定要使用的用户对于 运行 容器。