ENV variables not being called in to services
felixggj
FREEOP

a year ago

Not receiving any environment variables at runtime on the build logs.

I've set variables like NEXT_PUBLIC_API_KEY in the frontend service and API_KEY in the backend service and more into the service's "Variables" tab, but my application logs show that process.env.NEXT_PUBLIC_API_KEY get a 401 error when in local with the exact same configs it works perfectly.

The build log shows a successful build. I have two services running, each of them with their individual docker container. BE and FE, which should communicate with each other.

Can you please investigate why the variables are not being injected into my container?

Solved$10 Bounty

Pinned Solution

a year ago

Okay wonderful thanks for clarifying!

The other thing I can think is that during the build cycle this isnt properly passing the variables in.

https://docs.railway.com/guides/dockerfiles#using-variables-at-build-time

Looks like you need to declare them in your Dockerfile like:

ARG NEXT_PUBLIC_API_KEY etc.

24 Replies

Railway
BOT

a year ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


felixggj
FREEOP

a year ago

For the record: I have committed no mistypings, runs perfectly locally, but variables are not pulled in for some reason.


felixggj
FREEOP

a year ago

When I do 'railway variables' via CLI i get the variables correctly..


a year ago

Hey there! We had something similar happen recently.

Since setting the variables have you saved them?

On that, have you redeployed the service after saving the new variables?


noahd

Hey there! We had something similar happen recently. Since setting the variables have you saved them? On that, have you redeployed the service _after_ saving the new variables?

felixggj
FREEOP

a year ago

Yes to both questions. That's why I am quite confused.


felixggj

Yes to both questions. That's why I am quite confused.

a year ago

Always love to clear up the given simple things!

So in your script if you log process.env.NEXT_PUBLIC_API_KEY you are getting a 401 error?

Can you try printing the value to console of that variable?


felixggj
FREEOP

a year ago

Yes, I get 'undefined'.


noahd

Always love to clear up the given simple things! So in your script if you log `process.env.NEXT_PUBLIC_API_KEY` you are getting a 401 error? Can you try printing the value to console of that variable?

felixggj
FREEOP

a year ago

Yes, I get 'undefined'.


felixggj

Yes, I get 'undefined'.

felixggj
FREEOP

a year ago

For all my variables, not just this one.


a year ago

Okay great that clears things up more!

For a bit more cursory info, are you using nixpacks/railpacks or a Dockerfile?


felixggj
FREEOP

a year ago

Dockerfile in a monorepo, one for backend and one for frontend, two different railway services:

be :

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY app ./app

CMD ["sh", "-c", "python -m uvicorn app.api.main:app --host 0.0.0.0 --port ${PORT:-8080}"]

fe:

FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build

CMD ["sh", "-c", "npm start -- --port ${PORT:-8080} --hostname 0.0.0.0"]

The builds and deployments are successful, I set it up with git root directories for each service.


a year ago

Thank you so much! Doing some poking around now


noahd

Thank you so much! Doing some poking around now

felixggj
FREEOP

a year ago

okay thanks a lot, let me know!


felixggj

okay thanks a lot, let me know!

a year ago

https://nextjs.org/docs/14/app/building-your-application/configuring/environment-variables

so I did find some things about env variables with nextjs.

Did you follow this where you declare in next conf the env variables it would be referencing? That could be it but I'm not entirely sure.


noahd

<https://nextjs.org/docs/14/app/building-your-application/configuring/environment-variables> so I did find some things about env variables with nextjs. Did you follow this where you declare in next conf the env variables it would be referencing? That could be it but I'm not entirely sure.

felixggj
FREEOP

a year ago

just tried implementing this like this:

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: "standalone",
  env: {
    NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
    NEXT_PUBLIC_API_KEY: process.env.NEXT_PUBLIC_API_KEY,
  },
};

module.exports = nextConfig;

in a next.config.js file, re-deployed, but still getting that 401 issue :(


felixggj

just tried implementing this like this: ```javascript /** @type {import('next').NextConfig} */ const nextConfig = { output: "standalone", env: { NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL, NEXT_PUBLIC_API_KEY: process.env.NEXT_PUBLIC_API_KEY, }, }; module.exports = nextConfig; ``` in a next.config.js file, re-deployed, but still getting that 401 issue :(

a year ago

So 401 is unauthorized.

I'm a bit confused as to why the env variables are causing a 401 (other then an undefined value mismatching provided value).

If we're just talking about the value of the NEXT_PUBLIC_API_URL/KEY is that being propagated? As in, printing to console is the value undefined?


noahd

So 401 is unauthorized. I'm a bit confused as to why the env variables are causing a 401 (other then an undefined value mismatching provided value). If we're just talking about the value of the NEXT\_PUBLIC\_API\_URL/KEY is that being propagated? As in, printing to console is the value undefined?

felixggj
FREEOP

a year ago

yes exactly, in console it says exactly 'undefined', printed. It's a 401 because it's an api that requires a key to call. I am calling it in my frontend and it's returning 401 error. headers are empty for x-api-key .

In local it works as expected, no issues.


a year ago

Okay wonderful thanks for clarifying!

The other thing I can think is that during the build cycle this isnt properly passing the variables in.

https://docs.railway.com/guides/dockerfiles#using-variables-at-build-time

Looks like you need to declare them in your Dockerfile like:

ARG NEXT_PUBLIC_API_KEY etc.


ralyodio
PRO

a year ago

are you using docker by chance? I had to use ARG to copy over my env variables to the container.


a year ago

He is! Just advised to use ARG so we'll see if that fixes it


ralyodio
PRO

a year ago

I just deployed a custom docker this morning. it was a pita. https://github.com/profullstack/qryptchat-web/blob/master/Dockerfile


a year ago

Wow that is complex! Does look like ARG will fix that then. Wish next was easier to deal with


felixggj
FREEOP

a year ago

It worked!! thanks a lot


felixggj

It worked!! thanks a lot

a year ago

Happy I could help!

Noted for the future too.


Status changed to Solved noahd 11 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...