Django app – Inject Docker environment variable into a JavaScript file?

  api, django, docker, javascript

I’m working on a Django app that is Dockerized. I implemented a 3rd party API in a JS file, making the call using Ajax and need to inject the API key into the url. I stored the API key in a .env file. I know using the API key on the server side I can just run a os.environ.get('API_KEY'). Now I’ve seen some previous posts that suggest calling the API in the Django view. But the call is being used in about 4 different places(address autofill api).

I’ve seen some examples but most seem to use Webpack, or using Node for the backend.

I’m really just trying to find the best, simple solution on how to inject a Docker environment variable into a JS script that will be rendered in the browser each time the api call is made. Is a custom shell script mandatory to be executed and thrown into the Dockerfile, or possibly a package that helps with this?

Any help is greatly appreciated!

Source: Docker Questions

LEAVE A COMMENT