<p>I have the following Dockerfile that i have set up to use a new user rather than using root for my nginx server. The nginx server is built upon Redhat UBI image.
The image builds fine, however when I run the container I get the following error: nginx: [nginx: [emerg] open() &quot;/run/nginx.pid&quot; failed (13: Permission denied)</p>
<p>Below is my dockerfile.</p>
<pre><code>USER root
RUN microdnf --setopt=tsflags=nodocs install -y nginx procps shadow-utils net-tools ca-certificates dirmngr gnupg wget vim\
            &amp;&amp; microdnf clean all \
            &amp;&amp; rpm -q procps-ng

ENV NGINX_USER=&quot;api-gatway&quot; \
    NGINXR_UID=&quot;8987&quot; \
    NGINX_GROUP=&quot;api-gatway&quot; \
    NGINX_GID=&quot;8987&quot;     

RUN set -ex; \
  groupadd -r --gid &quot;$NGINX_GID&quot; &quot;$NGINX_GROUP&quot;; \
  useradd -r --uid &quot;$NGINXR_UID&quot; --gid &quot;$NGINX_GID&quot; &quot;$NGINX_USER&quot; 


COPY nginx.conf /etc/nginx/nginx.conf

RUN mkdir -p /var/lib/nginx/tmp /var/log/nginx \
    &amp;&amp; chown -R api-gatway:api-gatway /var/lib/nginx /var/log/nginx \
    &amp;&amp; chmod -R 755 /var/lib/nginx /var/log/nginx

EXPOSE 1080
USER api-gatway
CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
</code></pre>
<p>When i build the image, it builds without any errors, but when i deploy on my K8 cluster using helm, it gives me the following errors.</p>
<pre><code>nginx: [emerg] open() &quot;/run/nginx.pid&quot; failed (13: Permission denied)
</code></pre>
<p>Here is my nginx.conf file that I have set up</p>
<pre><code>worker_processes  1;
error_log  /tmp/error.log;
pid        /run/nginx.pid;

events {
  worker_connections  1024;
}
http {
  server {
    listen 1080;
    server_name localhost 127.0.0.1;
    access_log  /tmp/access.log;
    client_max_body_size 0;
    set  $allowOriginSite *;
    proxy_pass_request_headers on;
    proxy_pass_header Set-Cookie;
    # External settings, do not remove

    #ENV_ACCESS_LOG
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header Host            $host;
    proxy_set_header X-Real-IP       $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass_header Set-Cookie;
    proxy_set_header X-Forwarded-Proto $scheme;

    location /search/ {
               proxy_pass http://*******-svc:8983/***/;
    }
    location /auth/ {
      proxy_pass http://********:8080;
    }
    location /mapbox {
      rewrite ^/mapbox(.*)https://****$1 break;
    }
  }
}

</code></pre>
<p>How can I fix nginx: [emerg] open() &quot;/var/run/nginx.pid&quot; failed (13: Permission denied) and what have i done wrong in my configurations?</p>

Bilal Yousaf
<p>UPDATE</p>
<p>In order to fix my &quot;/var/run/nginx.pid&quot; permission denied error.</p>
<p>I had to add nginx.pid permission errors inside my dockerfile for the new user to work.</p>
<p>Below are the changes i made in my dockerfile</p>
<pre><code>RUN touch /run/nginx.pid \
 &amp;&amp; chown -R api-gatway:api-gatway /run/nginx.pid /cache/nginx

</code></pre>
