Skip to content

fix: resolve dockerfile_template with secure=False during containerize#5596

Open
Abhishek8108 wants to merge 2 commits intobentoml:mainfrom
Abhishek8108:fix/containerize-dockerfile-template-outside-cwd
Open

fix: resolve dockerfile_template with secure=False during containerize#5596
Abhishek8108 wants to merge 2 commits intobentoml:mainfrom
Abhishek8108:fix/containerize-dockerfile-template-outside-cwd

Conversation

@Abhishek8108
Copy link
Copy Markdown

Summary

Fixes #5566.

bentoml containerize raised ValueError: Accessing file outside of current working directory is not allowed for any bento that uses dockerfile_template. This is a regression introduced in #5548.

Root cause: generate_containerfile called resolve_user_filepath(user_templates, build_ctx) with the default secure=True. The secure=True check verifies that the resolved path is relative to os.getcwd(). During bentoml containerize, build_ctx is a BentoML-managed temp directory (e.g. /tmp/...), which is never under the user's CWD — so the check fires as a false positive.

Fix: Pass secure=False when resolving the template path inside generate_containerfile. This is correct because:

  • The template was already validated and copied into the bento archive by build_config.py during bentoml build (where secure=True is appropriate)
  • At containerize time, build_ctx is a BentoML-controlled temp dir, not a user-supplied path
  • The path being resolved is relative and scoped to build_ctx — there is no security concern

Change: One argument added to one call site in generate.py. Regression test added to test_generate.py that reproduces the failure before the fix and passes after.

Test plan

  • New test test_generate_containerfile_dockerfile_template_outside_cwd passes (creates a template in tmp_path, which is outside CWD, and calls generate_containerfile — previously raised ValueError)
  • Existing test test_generate_containerfile_quotes_system_packages still passes
  • Run pytest tests/unit/_internal/container/test_generate.py -v

resolve_user_filepath was called with the default secure=True in
generate_containerfile, which checks that the resolved path is relative
to os.getcwd(). During `bentoml containerize` the build context is a
BentoML-managed temp directory (e.g. /tmp/...), not the user's project
directory, so the check is a false positive and raises ValueError.

Pass secure=False since the path is resolved against a BentoML-controlled
directory, not a user-supplied path — the template was already validated
and copied into the bento archive during `bentoml build`.

Fixes bentoml#5566
@Abhishek8108 Abhishek8108 requested a review from a team as a code owner April 9, 2026 17:52
@Abhishek8108 Abhishek8108 requested review from larme and removed request for a team April 9, 2026 17:52
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.

bug: bentoml containerize fails with ValueError: Accessing file outside of current working directory when using dockerfile_template (v1.4.36)

1 participant