Environment variables Railway error

Anonymous
TRIAL

a year ago

Hello, I have a problem with environment variables within Railway.

11 days ago, my Next.js app was working correctly, but recently I deployed again and it failed. So, I added a Dockerfile, and the deploy worked. However, now the environment variables are not working. What am I missing?

The enviroment variables are "NEXTPUBLICBASE_URL"
Attached the DockerFile

Publish in my docker local and works correctly

3 Replies

Anonymous
TRIAL

a year ago

a2d0a173-be4f-421a-b1b0-ada4f0b76aa2


a year ago

your dockerfile is missing the reference to NEXT_PUBLIC_BASE_URL and whatever other variables you may need, please read this


Anonymous
TRIAL

a year ago

it didnt work

Add to dockerFile
ARG NEXTPUBLICBASEURL RUN echo $NEXTPUBLICBASEURL

command run echo
if see it value in build Logs Railway


a year ago

show me the dockerfile you have now please


Anonymous
TRIAL

a year ago

Install dependencies only when needed

FROM node:alpine AS deps

Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.

RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json yarn.lock ./

COPY package.json package-lock.json ./

RUN yarn install --frozen-lockfile

RUN npm ci

Rebuild the source code only when needed

FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/nodemodules ./nodemodules

RUN yarn build && yarn install --production --ignore-scripts --prefer-offline

RUN npm run build && npm install --production --ignore-scripts --prefer-offline

Production image, copy all the files and run next

FROM node:alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

You only need to copy next.config.js if you are NOT using the default configuration

COPY --from=builder /app/next.config.js ./

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/nodemodules ./nodemodules
COPY --from=builder /app/package.json ./package.json

Specify the variable you need RAILWAY

ARG NEXTPUBLICBASEURL RUN echo $NEXTPUBLICBASEURL

ARG NEXTPUBLICCOLOR_NAMES

ARG NEXTPUBLICSTATUS

RUN echo $NEXTPUBLICCOLOR_NAMES

RUN echo $NEXTPUBLICSTATUS

USER nextjs

EXPOSE 3000

Next.js collects completely anonymous telemetry data about general usage.

Learn more here: https://nextjs.org/telemetry

Uncomment the following line in case you want to disable telemetry.

ENV NEXTTELEMETRYDISABLED 1

CMD ["yarn", "start"]

CMD ["npm", "run", "start"]


a year ago

please enclose that in a code block


Anonymous
TRIAL

a year ago

sorry


Anonymous
TRIAL

a year ago

Install dependencies only when needed

FROM node:alpine AS deps

Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.

RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json yarn.lock ./

COPY package.json package-lock.json ./

RUN yarn install --frozen-lockfile

RUN npm ci

Rebuild the source code only when needed

FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/nodemodules ./nodemodules

RUN yarn build && yarn install --production --ignore-scripts --prefer-offline

RUN npm run build && npm install --production --ignore-scripts --prefer-offline

Production image, copy all the files and run next

FROM node:alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

You only need to copy next.config.js if you are NOT using the default configuration

COPY --from=builder /app/next.config.js ./

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/nodemodules ./nodemodules
COPY --from=builder /app/package.json ./package.json

Specify the variable you need RAILWAY

ARG NEXTPUBLICBASEURL RUN echo $NEXTPUBLICBASEURL

ARG NEXTPUBLICCOLOR_NAMES

ARG NEXTPUBLICSTATUS

RUN echo $NEXTPUBLICCOLOR_NAMES

RUN echo $NEXTPUBLICSTATUS

USER nextjs

EXPOSE 3000

Next.js collects completely anonymous telemetry data about general usage.

Learn more here: https://nextjs.org/telemetry

Uncomment the following line in case you want to disable telemetry.

ENV NEXTTELEMETRYDISABLED 1

CMD ["yarn", "start"]

CMD ["npm", "run", "start"]


Anonymous
TRIAL

a year ago

FROM node:alpine AS deps

RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json yarn.lock ./

COPY package.json package-lock.json ./

RUN yarn install --frozen-lockfile

RUN npm ci

Rebuild the source code only when needed

FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/nodemodules ./nodemodules

RUN yarn build && yarn install --production --ignore-scripts --prefer-offline

RUN npm run build && npm install --production --ignore-scripts --prefer-offline

Production image, copy all the files and run next

FROM node:alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

You only need to copy next.config.js if you are NOT using the default configuration

COPY --from=builder /app/next.config.js ./

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/nodemodules ./nodemodules
COPY --from=builder /app/package.json ./package.json

Specify the variable you need RAILWAY

ARG NEXTPUBLICBASEURL RUN echo $NEXTPUBLICBASEURL

ARG NEXTPUBLICCOLOR_NAMES

ARG NEXTPUBLICSTATUS

RUN echo $NEXTPUBLICCOLOR_NAMES

RUN echo $NEXTPUBLICSTATUS

USER nextjs

EXPOSE 3000

ENV NEXTTELEMETRYDISABLED 1

CMD ["yarn", "start"]

CMD ["npm", "run", "start"]


a year ago

please enclose that in a code block


Anonymous
TRIAL

a year ago


a year ago

that works too


a year ago

  1. the ARG lines are commented out

  2. these variables are needed during building of the next app, therefore you need to reference them in the same image layer that is responsible for building


Anonymous
TRIAL

a year ago

If the variable line 36 is present, (NEXTPUBLICBASE_URL)

The commented variables are only for testing


a year ago

okay that solves 1. please read 2.


Anonymous
TRIAL

a year ago

Thanks

Its working


a year ago

awesome


macluiggy
HOBBY

a year ago

what should i do about to fix it here, i have the same problem, the variables are not detected by railway:

FROM node:18-alpine3.16




# Create app directory
WORKDIR /app

# Install app dependencies
COPY package.json ./
COPY tsconfig.json ./

# Bundle app source
COPY . .



RUN npm install -g @nestjs/cli
RUN npm install --omit=dev


# railway env variables
ARG RAILWAY_PUBLIC_DOMAIN
ARG RAILWAY_PRIVATE_DOMAIN
ARG RAILWAY_PROJECT_NAME
ARG RAILWAY_ENVIRONMENT_NAME
ARG RAILWAY_SERVICE_NAME
ARG RAILWAY_PROJECT_ID
ARG RAILWAY_ENVIRONMENT_ID
ARG RAILWAY_SERVICE_ID
# app env variables
ARG DATABASE_URL
ARG JWT_SECRET
ARG JWT_EXPIRES_IN

ENV DATABASE_URL=$DATABASE_URL \
  JWT_SECRET=$JWT_SECRET \
  JWT_EXPIRES_IN=$JWT_EXPIRES_IN \
  RAILWAY_PUBLIC_DOMAIN=$RAILWAY_PUBLIC_DOMAIN \
  RAILWAY_PRIVATE_DOMAIN=$RAILWAY_PRIVATE_DOMAIN \
  RAILWAY_PROJECT_NAME=$RAILWAY_PROJECT_NAME \
  RAILWAY_ENVIRONMENT_NAME=$RAILWAY_ENVIRONMENT_NAME \
  RAILWAY_SERVICE_NAME=$RAILWAY_SERVICE_NAME \
  RAILWAY_PROJECT_ID=$RAILWAY_PROJECT_ID \
  RAILWAY_ENVIRONMENT_ID=$RAILWAY_ENVIRONMENT_ID \
  RAILWAY_SERVICE_ID=$RAILWAY_SERVICE_ID


RUN npm run build


ENV NODE_ENV=production
EXPOSE 3000


CMD ["npm", "run", "start:prod"]

saurs
PRO

6 months ago

brody just saved my life


galer7
HOBBY

15 days ago

Anyone came up with a solution for this yet?

According to the Next.js docs, NEXT_PUBLIC_ variables are "inlined" into the JavaScript bundle at BUILD TIME, not runtime. This means:

1. You need the environment variable available in the builder stage of your Dockerfile where next build happens

2. After the build, these values are frozen and won't respond to runtime changes

Yet this still doesn't work in my Dockerfile:

```

# In the builder stage where next build happens

FROM base AS builder

ARG RAILWAY_PUBLIC_DOMAIN # Get Railway's domain as build arg

# Set NEXT_PUBLIC_ var during build - this is when Next.js inlines it

ENV NEXT_PUBLIC_APP_URL=https://${RAILWAY_PUBLIC_DOMAIN}

# Then do your build

RUN pnpm build --filter=${PROJECT}

```