Next.js Discord

Discord Forum

NEXTJS env Variabel not found

Unanswered
Jesper posted this in #help-forum
Open in Discord
When I build my nextjs page and put it on the server, it doesn't find the env variables from the docker env

24 Replies

can you send your docker config?
FROM node:21

WORKDIR /home/disbot

# Install app dependencies
COPY . /home/disbot
RUN cd /home/disbot && npm install -g npm@latest --force
RUN cd /home/disbot && npm install --force
RUN cd /home/disbot && npm run build
RUN cd /home/disbot

RUN cd /home/disbot && ls -a

ENV BASEURL=0
ENV MONGODBURL=0
ENV API_TOKEN=0
ENV API_URL=0
ENV CLIENT_SECRET=0
ENV CLIENT_ID=0
ENV REDIRECTURL=0
ENV AUTHURL=0
ENV BOT_TOKEN=0
ENV MONGODBDBNAME=disbot-db

CMD [ "npm", "run", "start" ]
i try sec
(new Dockerfile)

FROM node:21

WORKDIR /home/disbot

# Install app dependencies
COPY . /home/disbot
RUN cd /home/disbot && npm install -g npm@latest --force
RUN cd /home/disbot && npm install --force

ENV BASEURL=0
ENV MONGODBURL=0
ENV API_TOKEN=0
ENV API_URL=0
ENV CLIENT_SECRET=0
ENV CLIENT_ID=0
ENV REDIRECTURL=0
ENV AUTHURL=0
ENV BOT_TOKEN=0
ENV MONGODBDBNAME=disbot-db

RUN cd /home/disbot && npm run build
RUN cd /home/disbot

RUN cd /home/disbot && ls -a

CMD [ "npm", "run", "start" ]
OK
# docker-compose.yml
version: '3.7'
services:
  frontend:
    image: ghcr.io/....
    ports:
      - 2321:3000
    environment:
      MONGODBURL: ""
      MONGODBDBNAME: ""
      API_TOKEN: ""
      API_URL: ""
      BOT_TOKEN: ""
      CLIENT_SECRET: ""
      CLIENT_ID: ""
      REDIRECTURL: ""
      BASEURL: ""
      AUTHURL: ""
FROM node:21

WORKDIR /home/disbot

# Install app dependencies
COPY . /home/disbot
RUN cd /home/disbot && npm install -g npm@latest --force
RUN cd /home/disbot && npm install --force

ARG BASEURL
ARG MONGODBURL
ARG API_TOKEN
ARG API_URL
ARG CLIENT_SECRET
ARG CLIENT_ID
ARG REDIRECTURL
ARG AUTHURL
ARG BOT_TOKEN
ARG MONGODBDBNAME

ENV BASEURL=${BASEURL}
ENV MONGODBURL=0${MONGODBURL}
ENV API_TOKEN=${API_TOKEN}
ENV API_URL=${API_URL}
ENV CLIENT_SECRET=${CLIENT_SECRET}
ENV CLIENT_ID=${CLIENT_ID}
ENV REDIRECTURL=${REDIRECTURL}
ENV AUTHURL=${AUTHURL}
ENV BOT_TOKEN=${BOT_TOKEN}
ENV MONGODBDBNAME=${MONGODBDBNAME}

RUN cd /home/disbot && npm run build
RUN cd /home/disbot

RUN cd /home/disbot && ls -a

CMD [ "npm", "run", "start" ]
@Jesper # docker-compose.yml version: '3.7' services: frontend: image: ghcr.io/.... ports: - 2321:3000 environment: MONGODBURL: "" MONGODBDBNAME: "" API_TOKEN: "" API_URL: "" BOT_TOKEN: "" CLIENT_SECRET: "" CLIENT_ID: "" REDIRECTURL: "" BASEURL: "" AUTHURL: ""
you added it as environment which adds to runtime env. You need to do smth like this:
services:
  nextjs:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - DATABASE_URL=${DATABASE_URL} # here
    env_file:
      - .env
    ports:
      - 3000:3000
    depends_on:
      - postgres
@Jesper So I build my image via github actions
did you use docker compose build or docker build
buildx using github actions
with a workflow
which workflow
# 
# Copyright (c) https://github.com/gOOvER/own-pterodactyl-images/blob/main/LICENSE
#

name: DisBot Docker Builder (Dashbaord)

on:
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
  schedule:
    - cron: "0 0 * * 1"
  push:
    branches:
      -main
env:
  GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
jobs:
  push:
    name: "Dashboard Builder"
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      id token: write
    strategy:
      fail-fast: false

    steps:
      - uses: actions/checkout@v4
      - name: get-npm-version
        id: package version
        uses: martinbeentjes/npm-get-version-action@v1.3.1
        with:
          path: .
      - uses: docker/setup-qemu-action@v3

      - name: install libs
        # pkg-config
        run: sudo apt-get update -y && sudo add-apt-repository ppa:deadsnakes/ppa -y && sudo apt update && sudo apt install python3.12 -y && sudo apt install pkgconf build-essential libcairo2-dev libpango1.0 -dev libjpeg-dev libgif-dev librsvg2-dev -y && python3 --version
      - uses: docker/setup-buildx-action@v3
        with:
          buildkitd flags: --debug

      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - uses: docker/build-push-action@v5
        with:
          context: .
          file: DashDockerfile
          platforms: linux/amd64
          push: true
          tags: |
            ghcr.io/...
            ghcr.io/...
you should read the docs of build-push-action on how to add build time env vars
nextjs uses build time env vars
I have another project with the same workflow and it works
could you possibly help me?