feat: add WithReadOnlyRootFilesystem option #3303
Draft
+260
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add WithReadOnlyRootFilesystem option
Description
This PR implements the
WithReadOnlyRootFilesystem
option to allow running containers with read-only root filesystem, addressing the enhancement request in issue #2803.Changes
✅ Implementation
Added
WithReadOnlyRootFilesystem()
function inoptions.go
Sets Docker's
ReadonlyRootfs
field totrue
in HostConfigProperly handles existing HostConfigModifier functions by wrapping them
Follows existing codebase patterns and conventions
✅ Testing
Unit tests in
options_test.go
:Tests function works with no existing HostConfigModifier
Tests preservation of existing HostConfigModifier settings
Verifies ReadonlyRootfs field is correctly set
Integration tests in
readonly_integration_test.go
:Verifies write operations fail on read-only root filesystem
Confirms tmpfs mounts work for writable areas
Validates container configuration is properly applied
✅ Documentation
Updated
common_functional_options_list.md
with new optionAdded comprehensive documentation in
common_functional_options.md
Included usage examples and best practices
Created complete example in
examples/readonly/
Usage
Basic Usage
Advanced Usage with Tmpfs
Benefits
Security Enhancement: Prevents unauthorized writes to the root filesystem
Production Parity: Helps catch bugs that occur in production environments with read-only containers
Testing Reliability: Ensures applications work correctly with security hardening measures
Equivalent Docker Command
This option is equivalent to using the
--read-only
flag withdocker run
:Backward Compatibility
✅ Fully backward compatible - no breaking changes
✅ Works seamlessly with existing HostConfigModifier functions
✅ Follows established patterns in the codebase
Fixes #2803
Checklist
Implementation follows existing code patterns
Comprehensive unit tests added
Integration tests added
Documentation updated
Example code provided
Backward compatibility maintained
No breaking changes introduced