Skip to content

Run docker container as non-root user - #12760

Draft
SchrodingersGat wants to merge 3 commits into
inventree:masterfrom
SchrodingersGat:non-root-docker
Draft

Run docker container as non-root user#12760
SchrodingersGat wants to merge 3 commits into
inventree:masterfrom
SchrodingersGat:non-root-docker

Conversation

@SchrodingersGat

Copy link
Copy Markdown
Member

@SchrodingersGat SchrodingersGat added this to the 1.6.0 milestone Sep 1, 2026
@SchrodingersGat SchrodingersGat added docker Docker / docker-compose setup Relates to the InvenTree setup / installation process documentation labels Sep 1, 2026
@netlify

netlify Bot commented Sep 1, 2026

Copy link
Copy Markdown

Deploy Preview for inventree-web-pui-preview ready!

Name Link
🔨 Latest commit ad882cf
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/6a9b751ca62aa90007f7e75e
😎 Deploy Preview https://deploy-preview-12760--inventree-web-pui-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 94 (🔴 down 1 from production)
Accessibility: 82 (🟢 up 1 from production)
Best Practices: 92 (no change from production)
SEO: 78 (no change from production)
PWA: -
View the detailed breakdown and full score reports
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI left a comment

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.

Pull request overview

This PR aims to address docker bind-mount permission issues by running the InvenTree application processes (server/worker) as a dedicated non-root user in the production container image, while still performing required setup steps at startup.

Changes:

  • Add a fixed inventree user/group (uid=1000, gid=1000) and use gosu to drop privileges at container startup.
  • Update the container entrypoint to adjust ownership before launching the main command.
  • Document the non-root behavior and directory ownership expectations in the Docker setup docs.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
docs/docs/start/docker.md Documents non-root execution and external volume ownership behavior.
docs/docs/start/docker_install.md Links install guidance to the directory ownership notes.
contrib/container/init.sh Adds startup-time chown + gosu privilege drop logic.
contrib/container/Dockerfile Installs gosu, adds inventree user, and adjusts Python userbase behavior for non-root runtime.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread contrib/container/init.sh
Comment on lines +58 to +65
# If requested (production image only), drop root privileges before launching the CMD.
# This lets the external data volume be bind-mounted with any host ownership -
# we fix it up here rather than requiring the operator to chown it in advance.
if [[ "$(id -u)" = "0" && -n "$INVENTREE_RUN_AS_USER" ]]; then
echo "Setting ownership of ${INVENTREE_DATA_DIR} to '${INVENTREE_RUN_AS_USER}'"
chown -R "${INVENTREE_RUN_AS_USER}:${INVENTREE_RUN_AS_USER}" "${INVENTREE_DATA_DIR}"
exec gosu "${INVENTREE_RUN_AS_USER}" "$@"
fi
Comment on lines 155 to +178
@@ -153,6 +169,14 @@ RUN bash -c "cd '${INVENTREE_HOME}' && invoke int.backend-compilemessages"
RUN pip-licenses --format=json --with-license-file --no-license-path > "${INVENTREE_BACKEND_DIR}/InvenTree/InvenTree/licenses.txt" \
&& test -s "${INVENTREE_BACKEND_DIR}/InvenTree/InvenTree/licenses.txt"

# Fix up ownership of the application code/static files, and open up read access to
# the --user installed python packages under /root/.local (not readable by other users
# by default) - init.sh drops to the non-root 'inventree' user before launch, and
# additionally fixes up ownership of the (host-mounted) data volume at that point.
RUN chown -R inventree:inventree ${INVENTREE_HOME} \
&& chmod 755 /root \
&& chmod -R o+rX /root/.local
Comment thread docs/docs/start/docker.md
Comment on lines +118 to +119
!!! info "Directory Ownership"
The `inventree-server` and `inventree-worker` containers run application code as a non-root user (`uid=1000`, `gid=1000`), rather than as `root`. On startup, each container automatically takes ownership of the directory mapped to `INVENTREE_EXT_VOLUME` (equivalent to `chown -R 1000:1000`) before dropping root privileges - no manual setup of this directory is required.
@matmair matmair added the full-run Always do a full QC CI run label Sep 1, 2026
@matmair

matmair commented Sep 1, 2026

Copy link
Copy Markdown
Member

Looks good in general; have you tested this starting up on an existing setup?

@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.13%. Comparing base (b51f710) to head (ad882cf).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12760      +/-   ##
==========================================
- Coverage   87.14%   87.13%   -0.01%     
==========================================
  Files        1483     1483              
  Lines      100534   100526       -8     
  Branches    11524    11605      +81     
==========================================
- Hits        87606    87597       -9     
+ Misses      12866    12865       -1     
- Partials       62       64       +2     
Flag Coverage Δ
backend 91.16% <ø> (-0.01%) ⬇️
migrations 38.23% <ø> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Backend Apps 92.43% <ø> (ø)
Backend General 94.68% <ø> (ø)
Frontend 79.77% <ø> (-0.01%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@SchrodingersGat

Copy link
Copy Markdown
Member Author

Looks good in general; have you tested this starting up on an existing setup?

Not yet. Still have to perform testing

@SchrodingersGat
SchrodingersGat marked this pull request as draft September 3, 2026 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docker Docker / docker-compose documentation full-run Always do a full QC CI run setup Relates to the InvenTree setup / installation process

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Docker] Container permissions

3 participants