<p>Below is the logic in my docker file.
I am using nginx to build the application.</p>
<pre><code>FROM node:14-alpine as builder

COPY package.json ./
RUN npm install &amp;&amp; mkdir /app &amp;&amp; mv ./node_modules ./app
WORKDIR /app

COPY . .

RUN npm run build


FROM nginx:1.16.0-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

EXPOSE 3000

CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
</code></pre>
<p>Below is the nginx.conf file</p>
<pre><code>server {
   listen 3000;
      
   location / {
     root /usr/share/nginx/html;
     index index.html index.htm;
     try_files $uri $uri/ /index.html =404;
   }
  
   location /api {
    #  rewrite /api/api(.*) /$1 break;
     proxy_pass http://${backend_host}:${backend_port}/api;
   }
      
   include /etc/nginx/extra-conf.d/*.conf;
}
</code></pre>
<p>backend_host and backend_port in the proxy_pass URL will be provided while deploying the image using Deployment file.</p>
<p>Is this possible?
If not is there any alternative way for this?</p>

Narendra Valiveti
<p>If you want to dynamically mount the <strong>nginx.conf</strong> I would suggest using the <strong>config map</strong> with your <strong>deployment.yaml</strong></p>
<p>So this way you re-use your docker image multiple times without recreating it and pass the <strong>config map</strong> to update it.</p>
<p>You docker file will be</p>
<pre><code>FROM node:14-alpine as builder
COPY package.json ./
RUN npm install &amp;&amp; mkdir /app &amp;&amp; mv ./node_modules ./app
WORKDIR /app
COPY . .
RUN npm run build


FROM nginx:1.16.0-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
EXPOSE 3000
CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
</code></pre>
<p><strong>Example configmap</strong></p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  default.conf: |-
    server {
            listen 80 default_server;
            root /var/www/html;
            server_name  _;
            index index.php;
            location / {
                try_files $uri $uri/ /index.php?$args;
            }
            location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param   PATH_INFO       $fastcgi_path_info;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            }
        }
</code></pre>
<p><strong>Mount the config map to deployment</strong></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
  labels:
    app: app
spec:
  selector:
    matchLabels:
      app: app
  replicas: 1
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
        - name: app
          image: app-image
          ports:
          - containerPort: 80
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
      volumes:
        - name: nginx-config
          configMap:
            name: confnginx
</code></pre>
<p>For more details read at : <a href="https://blog.meain.io/2020/dynamic-reverse-proxy-kubernetes/" rel="nofollow noreferrer">https://blog.meain.io/2020/dynamic-reverse-proxy-kubernetes/</a></p>
