How to run .NET Core 2 application in Docker on Linux as non-root

In linux, binding to a port less than 1024 requires the user to be superuser. You can just use the default port 5000 and then publish to port 80 on your host (if you don't have any reverse proxy).


Has anyone got this working. I have tired this as well with using port 5000 and still haven't been able to get this working with a custom user


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

To enable ASP.NET core to bind to a higher port, I set this environment variable in my dockerfile

ENV ASPNETCORE_URLS=http://*:8080

Sources: https://github.com/dotnet/aspnetcore/issues/4699#issuecomment-454818058