Build keeps hitting cache -> ENV variable is not changing

Hello! I have a github repo w/ a Dockerfile set up. On Railway, I changed the enviornment variable and redeployed, and am currently getting an error related to the fact that this environment variable was not updated. Specifically it is a Next.js project and the ENV variable is NEXT_PUBLIC_API_URL. Likewise, the env variable is updated to my production API url. Because this environment variable is not changed, my frontend can't reach the backend, and it errors.
Solution:
after line 6 of your dockerfile add
ARG NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_API_URL
this needs to come after the FROM build phase instruction because this variable is used during the build...
Jump to solution
41 Replies
Percy
Percy15mo ago
Project ID: b3d6999c-60f7-4134-8803-08aa389195aa
celestial
celestialOP15mo ago
b3d6999c-60f7-4134-8803-08aa389195aa
Brody
Brody15mo ago
what is the value you have set in that variable
celestial
celestialOP15mo ago
Original value is http://host.docker.internal:3000 and the new value should have been along the lines of https://myserver-production.up.railway.app The reason why I know the environment variable is the problem is because my fetch error gets printed out to the console. It says it can't reach the url at "host.docker.internal"
Brody
Brody15mo ago
can you please update it once again to use a reference variable? https://docs.railway.app/develop/variables#reference-variables
celestial
celestialOP15mo ago
NEXT_PUBLIC_API_URL=https://${{RAILWAY_SERVICE_server_URL}} does this look right?
Brody
Brody15mo ago
nope this is the format you want
API_URL=https://${{backend.RAILWAY_PUBLIC_DOMAIN}}
API_URL=https://${{backend.RAILWAY_PUBLIC_DOMAIN}}
so let's see what you got
celestial
celestialOP15mo ago
gimme a sec Same error
TypeError: fetch failed] {
cause: [Error: getaddrinfo ENOTFOUND host.docker.internal] {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'host.docker.internal'
}
}
[Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.] {
digest: '561305779'
}
TypeError: fetch failed] {
cause: [Error: getaddrinfo ENOTFOUND host.docker.internal] {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'host.docker.internal'
}
}
[Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.] {
digest: '561305779'
}
celestial
celestialOP15mo ago
my variables
No description
Brody
Brody15mo ago
what's the value set to
celestial
celestialOP15mo ago
${{shared.NEXT_PUBLIC_API_URL}}
Brody
Brody15mo ago
that's definitely not the correct value this format we will sort out why the build is still using that host.docker.internal url, but we first need to use the proper reference variable
celestial
celestialOP15mo ago
Same error NEXT_PUBLIC_API_URL=https://${{server.RAILWAY_PUBLIC_DOMAIN}}
Brody
Brody15mo ago
and the api service is named "server" right?
celestial
celestialOP15mo ago
yes
Brody
Brody15mo ago
okay looks good then
celestial
celestialOP15mo ago
i guess i could also try the internal railway URL but
Brody
Brody15mo ago
so you said you where using a dockerfile, could you send it?
celestial
celestialOP15mo ago
sure
FROM node:18-alpine AS base

RUN apk add --no-cache libc6-compat

# Build
FROM base AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# Production
FROM base AS production
WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next


COPY --from=builder /app/package*.json ./
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/node_modules ./node_modules

USER nextjs
EXPOSE 3000

ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["npm", "start"]
FROM node:18-alpine AS base

RUN apk add --no-cache libc6-compat

# Build
FROM base AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# Production
FROM base AS production
WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next


COPY --from=builder /app/package*.json ./
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/node_modules ./node_modules

USER nextjs
EXPOSE 3000

ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["npm", "start"]
This is essentially the same Dockerfile provided by next.js's examples
Brody
Brody15mo ago
that depends, the internal network is not available at build time, so if you only call this api at run time then you can call the internal domain
celestial
celestialOP15mo ago
Judging from the build logs i assumed that this being cached was the issue (but im not very good at infra stuff so idk)
No description
Brody
Brody15mo ago
I have ideas, need to know some more information to come to any real conclusions where do you get this error from, build logs or deployment logs
celestial
celestialOP15mo ago
deployment logs
Solution
Brody
Brody15mo ago
after line 6 of your dockerfile add
ARG NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_API_URL
this needs to come after the FROM build phase instruction because this variable is used during the build
Brody
Brody15mo ago
show me the dockerfile before you push anything though please
celestial
celestialOP15mo ago
FROM node:18-alpine as base

WORKDIR /server

FROM base as build
ARG NEXT_PUBLIC_API_URL
COPY package*.json ./
RUN npm ci

COPY . ./
RUN npx prisma generate
RUN npm run build

FROM base as production

COPY --from=build /server/build /server/build
COPY --from=build /server/node_modules /server/node_modules
COPY --from=build /server/prisma /server/prisma
COPY --from=build /server/.env* /server/
COPY --from=build /server/startup.sh /server/

EXPOSE 8080
ENV PORT 8080
CMD ["node", "/server/build/index.js"]
FROM node:18-alpine as base

WORKDIR /server

FROM base as build
ARG NEXT_PUBLIC_API_URL
COPY package*.json ./
RUN npm ci

COPY . ./
RUN npx prisma generate
RUN npm run build

FROM base as production

COPY --from=build /server/build /server/build
COPY --from=build /server/node_modules /server/node_modules
COPY --from=build /server/prisma /server/prisma
COPY --from=build /server/.env* /server/
COPY --from=build /server/startup.sh /server/

EXPOSE 8080
ENV PORT 8080
CMD ["node", "/server/build/index.js"]
Brody
Brody15mo ago
cleaned up that dockerfile nicely too, nice job push it
celestial
celestialOP15mo ago
👀 currently deploying. I noticed there was no cache message though
Brody
Brody15mo ago
interesting I see a .env copy, can you elaborate on that? are you using .env files in production?
celestial
celestialOP15mo ago
ope that was the wrong dockerfile i copy/pasted. that was my backend 😅 sorry bout that. the correct dockerfile is updated tho 👍 But yeah im using .env files is that a bad practice
Brody
Brody15mo ago
yes it is, you would want to use service variables
celestial
celestialOP15mo ago
Oh
Brody
Brody15mo ago
then there's no need for a .env file since the service variables are injected into the environment automatically
celestial
celestialOP15mo ago
do .env files override serivce variables? can that might be the issue
Brody
Brody15mo ago
yes the dotenv package would indeed overwrite variables from the environment
celestial
celestialOP15mo ago
😮 OH WAIT ITS WORKING Thank you Brody 🤝 I would not have thought that the ARG would be what I needed
Brody
Brody15mo ago
no problem but please use service variables going forward, you should not have an .env file in your repo as that is a massive security issue even if the repo is private, I've seen way too many .env files in repos that used to be private
celestial
celestialOP15mo ago
Will do my friend
Brody
Brody15mo ago
and the ARG <variable name> is only needed in the dockerfile if that specific variable is being used during build in your case it is technically used during runtime, but it's also used during build because the value of that variable is getting baked in to your app during build, therefore it is needed during build so you have to bring it in with ARG
celestial
celestialOP15mo ago
Ahh that makes more sense
Brody
Brody15mo ago
awesome
Want results from more Discord servers?
Add your server