hadolint/DL3045¶
COPY to a relative destination without WORKDIR set.
| Property | Value |
|---|---|
| Severity | Warning |
| Category | Best Practice |
| Default | Enabled |
| Auto-fix | Yes (suggestion --fix-unsafe; safe with --slow-checks) |
Description¶
While copying to a relative path is not problematic per se, errors happen when changes are introduced to the WORKDIR without updating the
destination of the COPY command. It is assumed that when a WORKDIR is set, the programmer will make sure it works well together with the COPY
destinations.
Examples¶
Problematic code¶
Correct code¶
or:
Auto-fix¶
Inserts an explicit WORKDIR instruction before the first COPY with a relative destination.
- Fast path (no registry access): suggests
WORKDIR /appas a conventional default (FixSuggestion, requires--fix-unsafe). - With
--slow-checks: resolves the base image's actualWORKDIRfrom the registry and uses that value (FixSafe, applied with--fix).
# Before
FROM python:3.12
COPY requirements.txt .
# After (with --fix-unsafe, fast path)
FROM python:3.12
WORKDIR /app
COPY requirements.txt .
# After (with --fix --slow-checks, base image has WORKDIR /usr/src/app)
FROM python:3.12
WORKDIR /usr/src/app
COPY requirements.txt .