'How to correctly deploy Next Js applications in kubernetes cluster with environments variables

I need my Next js application to read the .env file when deployed to kubernetes. What I do for this:

  1. As we know, in order for the next js application to read the .env file, this file must already be in the root folder of the application, before the "npm run build" command. So I create a docker file with this instruction "CMD npm run build; npm run start" so that when kubernetes deploy the container, it first does "npm run build" and after "npm run start".

FROM node:lts
WORKDIR /app
COPY . .
RUN npm install
RUN groupadd -g 1001 -r nodejs
RUN useradd -u 1001 -r nextjs
RUN mkdir -p /app/.next/cache/images && chown nextjs:nodejs /app/.next/cache/images
RUN chown -R nextjs /app/.next
VOLUME /app/.next/cache/images
USER nextjs
EXPOSE 3000
CMD npm run build; npm run start

  1. When deploying the application with "kubectl apply -f ...", I drop the env file into the root folder of the application using config map.

kind: ConfigMap
apiVersion: v1
metadata:
  name: environment-variables
  namespace: {{ .Values.namespace }}
data:
  .env: |-
    CMS_URL=http://192.168.****:13**/
    FRONT_URL=http://192.168.****:32***/
    AUTHORIZATION_SERVER_URL=http://192.168.****:32***
    RESOURCE_SERVER_URL=http://192.168.****:31***
    PORT=3000
    NODE_ENV=production
    NEXT_TELEMETRY_DISABLED=1
    NEXT_PUBLIC_CMS_URL=http://192.168.****:1***/
    NEXT_PUBLIC_FRONT_URL=http://192.168.****:32***/
    NEXT_PUBLIC_AUTHORIZATION_SERVER_URL=http://192.168.****:32***
    NEXT_PUBLIC_RESOURCE_SERVER_URL=http://192.168.****:31***
    NEXT_PUBLIC_PORT=3000
    NEXT_PUBLIC_NODE_ENV=production
    NEXT_PUBLIC_NEXT_TELEMETRY_DISABLED=1

apiVersion: apps/v1
kind: Deployment
metadata:
  name: register-next-deployment
  namespace: {{ .Values.namespace }}
  labels:
    app: {{ .Values.name }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Values.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.name }}
    spec:
      imagePullSecrets:
      - name: platformdevvops-registry-key
      containers:
      - name: registernext
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: IfNotPresent
        ports:
          - containerPort: {{ .Values.container.port }}
        volumeMounts:
          - mountPath: /app/.env
            name: environment-variables
            readOnly: true
            subPath: .env
      volumes:
        - name: environment-variables
          configMap:
            name: environment-variables
            items:
              - key: .env
                path: .env

In this way, my application successfully reads the env file. But I don't like this method. Are there other ways to do this? I need my .env file to get into the container during deployment to Kubernetes cluster and be read by the application.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source