COPY --chmod over a separate COPY followed by RUN chmod.
| Property | Value |
|---|---|
| Severity | Info |
| Category | Style |
| Default | Enabled |
| Auto-fix | Yes (--fix) |
Description
Detects aCOPY instruction immediately followed by a RUN chmod that targets the same file, and suggests merging them into a single COPY --chmod
instruction.
The --chmod flag sets file permissions at copy time, eliminating an extra layer and
the overhead of running a shell container just to change permissions.
Why use COPY —chmod?
- Fewer layers: Merging two instructions into one reduces image layer count
- Performance:
COPY --chmodsets permissions without spawning a shell container - Readability: A single instruction is cleaner and easier to understand
Detected Patterns
The rule flags consecutiveCOPY + RUN chmod pairs where:
- The COPY has a single source file, heredoc, or single-dest content (not a glob or multiple sources)
- The
RUNis a standalonechmodcommand (shell-form or exec-form, not chained with other commands) - The chmod target matches the COPY effective destination (resolved against
WORKDIRfor relative paths)
755, 0755) and symbolic (+x, u+rwx, -x) chmod modes are supported.
Merging with existing --chmod
When the COPY already has --chmod, the rule still fires if a RUN chmod follows:
- Symbolic overlay:
COPY --chmod=644+RUN chmod +xmerges toCOPY --chmod=0755 - Octal override:
COPY --chmod=644+RUN chmod 755merges toCOPY --chmod=755 - Redundant chmod:
COPY --chmod=777+RUN chmod +xflags the useless RUN (777 already includes execute)
Examples
Before (violation)
After (fixed with —fix)
Auto-fix Conditions
The fix is emitted when:- The COPY has a single source file or heredoc content
- The
RUNis a standalone chmod (single command, not recursive, shell-form or exec-form) - The chmod target matches the COPY destination (absolute path or resolved via
WORKDIR)
--chmod flag value.
When merging with an existing --chmod, the result is formatted as octal.
Cross-Rule Interactions
tally/prefer-copy-heredoc: ConvertsRUN echo > filetoCOPY <<EOF. No overlap — this rule only acts onCOPYinstructions, notRUNfile creation. Both rules use the same fix priority (99) to avoid position drift when applied together.
Limitations
- Only detects consecutive COPY + RUN chmod pairs (no intervening instructions)
- Skips COPY with glob patterns (
*.sh) or multiple file sources - Skips
chmod -R(recursive) since--chmodapplies per-file - Does not detect ADD + RUN chmod patterns (only COPY)
Options
| Option | Type | Default | Description |
|---|---|---|---|
enabled | boolean | true | Enable or disable the rule |