#Dynamically change env vars based if build or dev is being ran using docker

13 messages · Page 1 of 1 (latest)

hexed path
#

Hi! I have an astro app (using some react for the ui) and self built api. I already have my docker files configured for a production build, but right now, on the web app side, My api urls are hardcoded to my production api url...

Is there a way that I can be able to change these urls dynamically based on whether i'm running/building a build in docker or running a development environment?

I've tried messing around with the astro.config and environment variables but haven't had much luck

hearty quail
#

If you provide a different .env when building and in development, I don't see why it wouldn't work. For example:

In dev mode (ie astro dev)

API_URL=some_dev_url

When building for docker (ie astro build)

API_URL=some_prod_url
#

if you want to do both locally and don't want to change manually the url, I think astro supports .env extension, let me check

hexed path
#

So something like this I would presume

axios.get(import.meta.env.API_URL)

npm run astro dev -- --config API_URL=some_url
hexed path
hearty quail
#

yes I think the .env.development and .env.production using import.meta.env.API_URL is the way to go here

#

If you need to use your env on the client, remember you have to use the PUBLIC_ prefix

#

DEV is injected by vite 😉

hexed path
#

thanks! if i have another question on the same topic should i open a new thread or bump this?

hearty quail
#

If it's directly related to this API_URL thing, write here. Otherwise, open up a new thread 👍