且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Linux 上的 Docker 中以非 root 用户身份运行 .NET Core 2 应用程序

更新时间:2023-10-20 22:48:34

因为这会带来大量流量,所以我添加了完成此操作所需的完整详细代码.

Because this gets so much traffic, I'm adding the fully detailed code that you need to get this done.

# Create a group and user so we are not running our container and application as root and thus user 0 which is a security issue.
RUN addgroup --system --gid 1000 customgroup 
    && adduser --system --uid 1000 --ingroup customgroup --shell /bin/sh customuser
  
# Serve on port 8080, we cannot serve on port 80 with a custom user that is not root.
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
  
# Tell docker that all future commands should run as the appuser user, must use the user number
USER 1000