Skip to main content

nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)

This error typically occurs in an Alpine-based Docker image. The official nginx:alpine image already ships with a built-in nginx user — the fix is to reuse that user and assign the correct file ownership before switching to it.
For more context, see the Stack Overflow reference that documents this pattern.
Dockerfile
FROM nginx:alpine

RUN touch /var/run/nginx.pid \
    && chown -R nginx:nginx /var/cache/nginx /var/run/nginx.pid

COPY --chown=nginx:nginx <your-files> /usr/share/nginx/html
COPY --chown=nginx:nginx <your-nginx-config> /etc/nginx/conf.d/default.conf

USER nginx
The --chown=nginx:nginx flag on COPY instructions ensures that copied files are immediately owned by the nginx user, avoiding a separate RUN chown layer.

Build docs developers (and LLMs) love