Skip to content

Run docker container as non-root user - #12760

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

SchrodingersGat wants to merge 9 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 canceled.

Name Link
🔨 Latest commit c6b50b2
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/6b00c8b8a3705b197071ab6b

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 90.37%. Comparing base (9d7aeb4) to head (c6b50b2).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12760      +/-   ##
==========================================
+ Coverage   90.34%   90.37%   +0.02%     
==========================================
  Files         901      901              
  Lines       83675    83675              
  Branches     4300     4300              
==========================================
+ Hits        75595    75618      +23     
+ Misses       7062     7041      -21     
+ Partials     1018     1016       -2     
Flag Coverage Δ
backend 93.91% <ø> (+<0.01%) ⬆️
migrations-mysql 31.70% <ø> (+<0.01%) ⬆️
migrations-postgresql 31.71% <ø> (+<0.01%) ⬆️
migrations-sqlite 31.71% <ø> (+<0.01%) ⬆️

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

Components Coverage Δ
Backend Apps 94.41% <ø> (ø)
Backend General 95.49% <ø> (ø)
Frontend 77.38% <ø> (+0.11%) ⬆️
🚀 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
@SchrodingersGat SchrodingersGat modified the milestones: 1.6.0, 2.0.0, 1.7.0 Oct 16, 2026
@matmair

matmair commented Nov 17, 2026

Copy link
Copy Markdown
Member

@SchrodingersGat can we merge this in 1.8.0 and see what breaks?

@SchrodingersGat SchrodingersGat mentioned this pull request Nov 20, 2026
2 tasks done
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