Skip to content

Improve Dockerfile non-root user setup#26

Merged
justinkumpe merged 2 commits intomasterfrom
PreProd
Jun 28, 2025
Merged

Improve Dockerfile non-root user setup#26
justinkumpe merged 2 commits intomasterfrom
PreProd

Conversation

@justinkumpe
Copy link
Member

@justinkumpe justinkumpe commented Jun 28, 2025

Summary by Sourcery

Optimize Dockerfile by establishing a non-root user with a proper home directory and setting correct permissions for application and pip cache directories.

Enhancements:

  • Create the non-root user earlier in the build with a home directory
  • Add dedicated pip cache and local directories under the non-root home and apply ownership
  • Remove duplicate non-root user creation step

@justinkumpe justinkumpe self-assigned this Jun 28, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 28, 2025

Reviewer's Guide

Refactors the Dockerfile to properly establish a non-root user with a home directory before installing dependencies, configure pip cache and local directories with correct ownership, and ensure the application directory is owned by that user.

Flow diagram for improved non-root user setup in Dockerfile

flowchart TD
    Start([Start Docker build])
    CreateGroup[Create non-root group]
    CreateUser[Create non-root user with home directory]
    InstallDeps[Install Python dependencies]
    CopyFiles[Copy application files]
    ChownApp[Chown /app to non-root]
    MkdirPip[Create /home/non-root/.cache/pip and .local]
    ChownHome[Chown /home/non-root to non-root]
    SwitchUser[Switch to non-root user]
    RunApp[Run application]

    Start --> CreateGroup --> CreateUser --> InstallDeps --> CopyFiles --> ChownApp --> MkdirPip --> ChownHome --> SwitchUser --> RunApp
Loading

File-Level Changes

Change Details Files
Reorder and enhance non-root user creation
  • Move groupadd and useradd ahead of pip install
  • Add -m flag to useradd to create a home directory
  • Remove redundant useradd invocation later in the file
Dockerfile
Set up pip cache and local directories in the user home
  • Add mkdir -p for /home/non-root/.cache/pip and /home/non-root/.local
  • Adjust ownership of these directories to non-root:non-root
Dockerfile
Ensure application directory ownership
  • Retain chown -R non-root:non-root on /app
Dockerfile

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai bot changed the title @sourcery-ai title Improve Dockerfile non-root user setup Jun 28, 2025
@justinkumpe
Copy link
Member Author

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @justinkumpe - I've reviewed your changes - here's some feedback:

  • Consider combining the user/group creation, directory setup, and chown commands into a single RUN instruction to reduce the number of Docker layers.
  • You’re creating a pip cache directory but still using --no-cache-dir; either remove that flag to leverage the cache or drop the cache setup step if it isn’t needed.
  • Add ENV HOME=/home/non-root (or similar) so that the non-root user’s home directory is correctly picked up by pip and other tools.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider combining the user/group creation, directory setup, and chown commands into a single RUN instruction to reduce the number of Docker layers.
- You’re creating a pip cache directory but still using --no-cache-dir; either remove that flag to leverage the cache or drop the cache setup step if it isn’t needed.
- Add ENV HOME=/home/non-root (or similar) so that the non-root user’s home directory is correctly picked up by pip and other tools.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@justinkumpe justinkumpe merged commit aff1ebf into master Jun 28, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant