Set env var in dockerfile during build
I've got the following in my dockerfile
My goal here is to have the build read SQLX_OFFLINE from my variables on railway during build and set the SQLX_OFFLINE environment var for the docker build. However I only want this to be set during builds on railway, and not when running docker locally.
Any guidance will be greatly appreciated!
92 Replies
Project ID:
d7c966b3-07dd-4a31-9cb9-82de5002e54f
You might find these helpful:
- Start command variable passing failing to pass vars
- Invalid environment variables unless ENV included in Dockerfile
- environment variables are not picked up in railway build but works locally
⚠️ experimental feature
d7c966b3-07dd-4a31-9cb9-82de5002e54f
ARG SQLX_OFFLINE will read variables and default to 0
Yeah I tried that and the build is still trying to perform compile time checks
I did follow the last item in Percy's list though, apparently docker vars must be at the top of the file. But that didn't work either
i think the sqlx checking comes in another command - not cargo build
It comes during compile unless the SQLX_OFFLINE var is set to "true"
During build
oh. so i guess you need to default to true, not 0/1
Yeah I'm defaulting to "false" actually, and have the variable in my service set to "true". The idea was that when my team builds locally they'll be building against a local postgres instance which will always be up to date with migrations, therefore checks will all pass. But in Railway the migrations for some reason always happen last, so the compile checks always fail on PR environments
cargo sqlx migrate?
u might have better luck using nixpacks tbh
I'm still having trouble getting it to work 😦
No idea what's wrong with the nixpacks build
got any logs
No but I can have some generated. Give me a few moments
sure
Interestingly enough the nixpacks build actually made it to the point where it failed due to compile time checks
Not sure why it even tried to check, aren't variables set at build time as well?
variables are in every stage. can we see where it is failing with some logs?
is DATABASE_URL being provided to this service?
Yes
are they in the same project?
or
Yep same project
okay, weird. one sec
send the table at the top of the build logs
You mean the table structure or?
yea
just a sccrfeenshot of it
error: error returned from database: relation "tokens" does not exist
Oh
need to the see the contents
Sure one sec
like the table
nono not the db table
the table tha was logged at the top of table at the top of build logs in ur deployment that is being logged by nixpacks
can u send ur nixpacks.toml
I don't have one
env variables? anything to do with nixpacks?
That's all I use
?????? something is wrong here
u must have a nixpacks.toml somewhere
Nope, I'm just letting the deployment decide what to do
I never needed the nixpacks file because I was using docker
ok time to figujre out why it thinks musl iS good
rust-toolchain.toml?
I think railway builds to musl by default
@Percy cargo musl builds?
it doesnjt
You might find these helpful:
- Nixpacks doesn't include directory when building
- Nixpacks build fail
- nixos not building
⚠️ experimental feature
ye it doersnt
nvm, it kinda does
have u got a rust-toolchain file
Nope
ok cool
Avoiding all of this is why we used Docker
We wanna keep the railway builds identical to local builds with the exception of env vars
well it has decided to do something we're not telling it to do
we have never done anytthing to make rust:build stages
someone on ur team has
Hm
Where would that be configured?
any nixpacks.toml / nixpacks.json
do u have a railway config file
Yes
But i've ignored it for the duration of trying nixpacks by just renaming it
it
ok go to service settings
find what builderis
what is the file called?
it's still using ur dockerfile
Oh right my bad I just tried switching back
One sec
rename the dockerfile to somethjign else
then trigger deploy then send a csreenshot of that
get rid of build command
save
then redeploy
it shoulda uto redeloy
ok can u send ur file structure
from the root
oh. in service settings set root path to backend
That's already set
is there a railway.toml/nixpacks.json at root?
Nope
can u send a screenshot of sertvice settings under builder
rename dockerfile to _dockerfile
and railway.toml to _railway.toml
Okay
No changes on that window
no env variables?
I've got them set in the ui
this is acc weird
ugh]
Yeah this is why we use docker
Everyone here hates the railway build steps lol
well, as we are railway, we'll try fix the issue with nixpacks
everyone here as in who
My team
cc @coffeecup
nixpacks isn't detecting a rust app
d7c966b3-07dd-4a31-9cb9-82de5002e54f
d7c966b3-07dd-4a31-9cb9-82de5002e54f
In the meanwhile is it safe for me to continue trying to get the arg passed into docker or should I branch off here for future troubleshooting?
u can keep trying, sure
but our focus now is prob fixing nixpacks lol
No worries I'll just make another branch from this and make a dummy PR to get the environment spun up
Update: I got nixpacks to run, I just had to set the language provider to rust. However I still get the same error as a few weeks ago post-build
failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount358826668/app/bin: lstat /var/lib/docker/tmp/buildkit-mount358826668/app/bin: no such file or directory
wtfff
I did get the docker file to work though, turns out everything was set correctly all this time and the sqlx-data.json file was out of sync with the actual db state
lol
Yeah totally my bad there
But I will still hold off on nixpacks due to that above thing though, I remember you saying it's a bug a while back
yeah, unfortunately
we should try and fix this tho
Let me know if I can help in any way
Very strange. I can take a look today.
can u let me know how/if u fix it
+1 to that, I'm happy to help if I can