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:Jump to solution
after line 6 of your dockerfile add
this needs to come after the FROM build phase instruction because this variable is used during the build...
41 Replies
Project ID:
b3d6999c-60f7-4134-8803-08aa389195aa
b3d6999c-60f7-4134-8803-08aa389195aa
what is the value you have set in that variable
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"can you please update it once again to use a reference variable?
https://docs.railway.app/develop/variables#reference-variables
NEXT_PUBLIC_API_URL=https://${{RAILWAY_SERVICE_server_URL}}
does this look right?nope
this is the format you want
so let's see what you got
gimme a sec
Same error
my variables
what's the value set to
${{shared.NEXT_PUBLIC_API_URL}}
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
Same error
NEXT_PUBLIC_API_URL=https://${{server.RAILWAY_PUBLIC_DOMAIN}}
and the api service is named "server" right?
yes
okay looks good then
i guess i could also try the internal railway URL
but
so you said you where using a dockerfile, could you send it?
sure
This is essentially the same Dockerfile provided by next.js's examples
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
Judging from the build logs i assumed that this being cached was the issue (but im not very good at infra stuff so idk)
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
deployment logs
Solution
after line 6 of your dockerfile add
this needs to come after the FROM build phase instruction because this variable is used during the build
show me the dockerfile before you push anything though please
cleaned up that dockerfile nicely too, nice job
push it
👀 currently deploying. I noticed there was no cache message though
interesting
I see a .env copy, can you elaborate on that? are you using .env files in production?
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
yes it is, you would want to use service variables
Oh
then there's no need for a .env file since the service variables are injected into the environment automatically
do .env files override serivce variables? can that might be the issue
yes the dotenv package would indeed overwrite variables from the environment
😮 OH WAIT ITS WORKING
Thank you Brody 🤝
I would not have thought that the
ARG
would be what I neededno 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
Will do my friend
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 ARGAhh
that makes more sense
awesome