Using sudo in RUN

Make the RUN user explicit and remove unnecessary sudo use.

Description

RUN uses the current build user. Invoking sudo adds a dependency on the image’s sudo installation and policy.

Potential impact

A command can fail during the build if sudo is absent or the current account lacks permission to use it.

Remediation

Use USER to select the account a build step needs, then run the command directly. After privileged steps, select the account that should run the application.

Examples

Both excerpts assume a root build step on Alpine with apk repositories configured.

Before

dockerfile
RUN sudo apk add --no-cache curl

After

dockerfile
RUN apk add --no-cache curl

apk add still runs as root after sudo is removed.

References