Question

App-Level Environment Variables not available in Python app but available in python command line.

Hello,

I have deployed an app through docker and the env variables are set through App-level Environment variable option.

When i tried to get print(os.getenv(“key”)) in the python application file (say, params.py), i am getting “None” value as output in the browser. However if i get into docker exec -it /bin/bash and exectute the same commands it works fine. Infact i tried running the samefile by prefixing #python3 params.py it works perfectly fine. any ideas why its not working when i access the file through browser?

Breaking my head for the past two days. every help would be appreciated.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi @rkrk

I assume you are facing issues while accessing the defined environment variables in your app when using a Dockerfile.

Environment variables are only available as build-args for Dockerfile builds in App Platform. So you should declare them in the Dockerfile as an ARG and then set the ENV variable in the Dockerfile from the build-arg.

If for example you set APP_VAR environment variable in application spec file or via App Platform UI, then something like this may work to actually pass that value into the container. You need to add the below lines in Dockerfile.

ARG APP_VAR ENV APP_VAR=${APP_VAR}

Here’s a community link on the same and I assure you it works: https://www.digitalocean.com/community/questions/how-to-use-app-platform-s-bindable-environment-variables-in-dockerfile

I hope this helps!

Cheers, Taha Fatima

Hello,

When defining your environment variables you need to make sure that you’ve set the scope to RUN_TIME and not only BUILD_TIME as described here:

https://docs.digitalocean.com/products/app-platform/how-to/use-environment-variables/

That way the environment variables will be available not only during the build stage but also during the run time of your app.

Let me know how it goes.

Best,

Bobby