Skip to content

Don't run Docker as root user#234

Merged
baynezy merged 5 commits intodevelopfrom
copilot/fix-233
Sep 8, 2025
Merged

Don't run Docker as root user#234
baynezy merged 5 commits intodevelopfrom
copilot/fix-233

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Sep 8, 2025

This PR addresses a security vulnerability where the Docker container was running as the root user, which violates secure coding practices and increases the attack surface if the container is compromised.

Changes Made

  • Create a non-root user in the Dockerfile
  • Switch to non-root user in the runtime stage
  • Maintain proper instruction sequence in Dockerfile
  • Remove TestResults files from version control

The Dockerfile has been updated to follow Docker security best practices by:

  1. Creating a non-root user: Added commands to create a dedicated nonroot group and user in the Alpine-based runtime stage
  2. Switching to non-root user: Added USER nonroot instruction to ensure the container runs with reduced privileges
  3. Maintaining proper instruction sequence: Ensured the user creation happens before copying files and switching users occurs before the entrypoint

Before and After

Before:

FROM mcr.microsoft.com/dotnet/runtime:9.0-alpine
COPY --from=build-env /app/out .
ENTRYPOINT [ "dotnet", "/SemVer.Json.dll" ]

After:

FROM mcr.microsoft.com/dotnet/runtime:9.0-alpine
RUN addgroup -S nonroot \
    && adduser -S nonroot -G nonroot
COPY --from=build-env /app/out .
USER nonroot
ENTRYPOINT [ "dotnet", "/SemVer.Json.dll" ]

Security Impact

  • Reduced attack surface: If the container is compromised, the attacker will only have non-root privileges
  • Principle of least privilege: The application runs with only the minimum permissions required
  • Compliance: Meets SonarQube security recommendations and Docker best practices

Testing

  • All existing unit tests continue to pass
  • Build process remains unchanged
  • Dockerfile syntax and instruction sequence verified with custom validation script

The implementation follows the compliant solution pattern for Alpine-based multi-stage builds as recommended in the security guidelines.

Fixes #233.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits September 8, 2025 15:18
Co-authored-by: baynezy <1049999+baynezy@users.noreply.github.com>
Co-authored-by: baynezy <1049999+baynezy@users.noreply.github.com>
Copilot AI changed the title [WIP] Don't run Docker as root user Don't run Docker as root user Sep 8, 2025
Copilot AI requested a review from baynezy September 8, 2025 15:26
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Sep 8, 2025

Test Results

8 tests   8 ✅  0s ⏱️
1 suites  0 💤
1 files    0 ❌

Results for commit 21e529c.

♻️ This comment has been updated with latest results.

@@ -0,0 +1,27 @@
<testsuites>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot - all files in any folder called TestResults should not be in version control.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. I've removed the TestResults files from version control in commit 21e529c. The .gitignore already had the correct rules to exclude TestResults directories, but the file was accidentally added in my previous commits.

Co-authored-by: baynezy <1049999+baynezy@users.noreply.github.com>
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Sep 8, 2025

Copilot AI requested a review from baynezy September 8, 2025 15:41
@baynezy baynezy marked this pull request as ready for review September 8, 2025 15:42
@baynezy baynezy merged commit 32d4384 into develop Sep 8, 2025
22 checks passed
@baynezy baynezy deleted the copilot/fix-233 branch September 8, 2025 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't run Docker as root user

2 participants