<p>I have a NextJS &quot;^11.1.2&quot; app, which gets build in a Dockerfile and deployed to production via CI/CD. <strong>But my <code>process.env</code> variables are not rendered</strong></p>
<p>I have this in my client side code, which should be rendered at runtime:</p>
<p><code>const PublicApiUrl = process.env.NEXT_PUBLIC_API_URL;</code></p>
<p>In my (Gitlab) <strong>CI/CD Pipeline</strong> I added via AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS some <code>--build-args</code>, as well <code>ENV</code> and <code>ARG</code>:</p>
<pre class="lang-sh prettyprint-override"><code>AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS --build-arg=NEXT_PUBLIC_API_URL=https://my.api.com --build-arg=NEXT_PUBLIC_API_URL=https://my.api.com --build-arg=NEXT_PUBLIC_BUILDER_KEY=XXXXXX
NEXT_PUBLIC_API_URL=https://my.api.com
API_URL=https://my.api.com
</code></pre>
<p><strong>Dockerfile</strong></p>
<pre><code>ARG API_URL
ENV API_URL=$API_URL
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_BUILDER_KEY
ENV NEXT_PUBLIC_BUILDER_KEY=$NEXT_PUBLIC_BUILDER_KEY
RUN npm run build # which resolves in &quot;build&quot;: &quot;next build&quot;
</code></pre>
<p>This values below are definitely picked up (I did a <code>RUN env</code> and can see the variables are there).</p>
<p>This is my <code>configMap</code> at Kubernetes which mounts the <code>.env.local</code> file into the container:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: frontend-env-local
  annotations:
    &quot;helm.sh/resource-policy&quot;: keep
data:
  .env: |-
    NEXT_PUBLIC_API_URL=https://my.api.url
    API_URL=https://my.api.url
</code></pre>
<p>This is my deployment which mounts the <code>configMap</code> into the container as <code>.env.local</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  labels:
    app: frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      volumes:
      - configMap:
          defaultMode: 420
          items:
          - key: .env
            path: .env.local
          name: frontend-env-local
        name: frontend-env-local
      imagePullSecrets: 
        - name: gitlab-credentials
      containers:
        - name: frontend
          image: &quot;registry.gitlab.com/myapp:latest&quot;
          imagePullPolicy: Always
          ports:
            - name: http
              containerPort: 5000
              protocol: TCP
          volumeMounts:
          - mountPath: /app/.env.local
            name: frontend-env-local
            readOnly: true
            subPath: .env.local
</code></pre>
<p>When I locally build <code>next build</code> it works and my variable is rendered.</p>
<p>But when I push, build and deploy it and run the app, its an empty string:</p>
<pre><code>const PublicApiUrl = &quot;&quot;;
</code></pre>
<p><strong>Why is the variable not recognized by NextJS?</strong></p>
<p>I logged into production (Kubernetes pod) terminal and run <code>env</code>. The variables are present too.</p>
<h1>Any ideas why this happens?</h1>

Jan
<p>I had to define the variables also in my <code>next.config.js</code> like so:</p>
<pre class="lang-js prettyprint-override"><code>module.exports = {
    serverRuntimeConfig: {
        API_URL: process.env.API_URL,
    },
    // Will be available on both server and client
    publicRuntimeConfig: {
        NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
    }
}
</code></pre>
<p>After that change it seems that neither the <code>configMap</code> nor the mounted volume was needed... Only the <code>--build-arg</code> in my CI/CD as well and the <code>ARG</code> and <code>ENV</code> in the <code>Dockerfile</code></p>
