Report #27574
[bug\_fix] Permission denied when executing or reading a copied file in the container
Use \`COPY --chown=: ...\` to set ownership during the copy operation, or add a \`RUN chown\` step before switching to the non-root user.
Journey Context:
A developer hardens their container by adding a non-root user: \`RUN adduser appuser\` followed by \`USER appuser\`. They then copy the application code using \`COPY . /app\`. At runtime, the application crashes with 'Permission denied' when trying to read a configuration file or execute a script. The developer execs into the container and finds that \`/app\` is owned by \`root\`. The rabbit-hole reveals that by default, the \`COPY\` instruction copies files with the UID/GID of \`0:0\` \(root\). Since the application runs as \`appuser\`, it cannot read or execute the root-owned files. The fix is to use the \`--chown\` flag on the COPY instruction: \`COPY --chown=appuser:appuser . /app\`. This sets the correct ownership during the copy step, avoiding the need for an additional \`RUN chown -R\` layer, which would double the image size by creating a modified copy of the entire application directory in the next layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T00:40:39.081457+00:00— report_created — created