Dockerfile has no USER instruction

Check the final image’s default user and run the application with only the privileges it needs.

Description

The final stage inherits its base image’s user. If neither sets USER, the default is root.

Potential impact

Running as root gives the application broader access to container files and processes. A new base image or a deployment override can change those permissions.

Remediation

Select a non-root account with USER in the final stage. Give it the file access it needs, including write access to its data directories. Test startup and file access with the user your deployment actually selects.

Examples

Supply app/app.py and test it with the chosen dependency versions. Keep those versions up to date.

Before

dockerfile
FROM python:3.13-slim
RUN pip install Flask==3.1.2
RUN useradd -ms /bin/bash appuser
COPY --chown=appuser:appuser app /app
WORKDIR /app
CMD ["python", "app.py"]

This base image runs as root even after appuser is created and given ownership of /app.

After

dockerfile
FROM python:3.13-slim
RUN pip install Flask==3.1.2
RUN useradd -ms /bin/bash appuser
COPY --chown=appuser:appuser app /app
WORKDIR /app
USER appuser
CMD ["python", "app.py"]

USER appuser selects the account that runs app.py.

References