#I am facing issue that my .env file is not being loaded in production environment.

1 messages · Page 1 of 1 (latest)

serene nebulaBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

sour flare
#
# Remove .env.local after build (not needed in runtime)
RUN rm -f .env.local

well, environment variables are needed in runtime. you need to ensure that either an .env* file exists in runtime, or that runtime otherwise already has all environment variables configured

fierce sail
#

@sour flare let me give you bigger picture

project
-> dashboard/ nextjs project
-> backend/ some other API project
-> .env.staging
-> .env
-> dockerfiles/dockerfile.dashboard
-> dockerfiles/dockerfile.backend
-> docker-compose.yml

dockerfile.dashboard contains what i sent first

.env has everything even NEXT_PUBLIC_**** variables

here is docker-compose.yml

  dashboard:
    build:
      context: .
      dockerfile: ./dockerfiles/Dockerfile.dashboard
    container_name: dashboard
    platform: linux/amd64
    ports:
      - "10111:3000"
    # Load all environment variables from .env file
    env_file:
      - .env.staging # or .env 
    environment:
      - NODE_ENV=production
      - NEXTAUTH_URL=${NEXTAUTH_URL_DASHBOARD}
      - NEXTAUTH_SECRET=${NEXTAUTH_SECRET_DASHBOARD}

    depends_on:
      - qck-api-staging
    
    networks:
      - qck-network-staging
    
    restart: unless-stopped
sour flare
fierce sail
#

i have my .env file correctly working in backend. its similar setup

#

@sour flare i even copied my .env to dashboard/.env.local before running docker-compose it did not work

#

@cp .env.staging dashboard/.env.local
docker-compose --env-file .env.staging -f docker-compose.staging.yml up -d
in makefile

fierce sail
#

This issue is happening in my dev environment as well.

docker exec dashboard-dev grep "NEXT_PUBLIC" /app/.env.local
NEXT_PUBLIC_API_URL=http://localhost:10110
NEXT_PUBLIC_DASHBOARD_URL=http://localhost:10111
NEXT_PUBLIC_ADMIN_URL=http://localhost:10112
NEXT_PUBLIC_MARKETING_URL=http://localhost:10113
NEXT_PUBLIC_ENVIRONMENT=development
NEXT_PUBLIC_PASSWORD_RESET_TOKEN_MIN_LENGTH=32
NEXT_PUBLIC_PASSWORD_RESET_TOKEN_FORMAT_REGEX=^[A-Za-z0-9\-_=]+\.?[A-Za-z0-9\-_=]*\.?[A-Za-z0-9\-_=]*$
NEXT_PUBLIC_PASSWORD_RESET_SUCCESS_REDIRECT_DELAY_MS=3000
#

@sour flare

#

docker-composee

version: '3.8'

services:
  dashboard:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: qck-dashboard-test
    ports:
      - "4444:3000"
    volumes:
      - .:/app
      - /app/node_modules
      - /app/.next
    environment:
      - NODE_ENV=development
    command: npm run dev

dockerfile

FROM node:20-alpine

WORKDIR /app

# Copy package files
COPY package.json package-lock.json* ./

# Install dependencies
RUN npm install

# Copy the rest of the application
COPY . .

EXPOSE 3000

CMD ["npm", "run", "dev"]

and .env.local is full of variables

fierce sail
#

hey man @sour flare sorry for bothering i accidentaly deleted original post.. i was able to fix the issue it was something else not the ENV itself... the way i accessed the env.

i was doign process.env[key] to dynamically load many

sour flare