How to access Kubernetes container environment variables from React.js application?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
A React application in the browser cannot directly read environment variables from a Kubernetes container at runtime. Those variables belong to the server-side process inside the pod, while the React code runs later in the user's browser, so you have to inject configuration into something the browser can actually load.
Why Direct Access Does Not Exist
This is the architectural boundary that matters:
- Kubernetes env vars exist inside the container process
- React frontend code is bundled into static JavaScript
- the browser only sees static files and HTTP responses
So if you set an environment variable on a pod, the browser does not magically gain access to it. The value must be copied into the built bundle, a runtime config file, or an API response.
Option 1: Build-Time Injection
If the value is known when the image is built, inject it during the frontend build. A typical Dockerfile for a React app can pass build arguments into the build process.
Then build the image with the value:
Inside the app:
This is simple, but the value is baked into the client bundle. That makes it appropriate only for public, non-secret configuration.
Option 2: Runtime Config File
If you want one image that can be reused across environments, a runtime config file is often the better approach. The container starts, reads its env vars, generates a small JavaScript file, and serves it alongside the static frontend.
Kubernetes can set the container env vars in the Deployment:
Then load config.js from index.html and read it in React:
This pattern keeps the image reusable while still making configuration visible to the browser.
ConfigMaps and Secrets
For non-secret values, a ConfigMap is a good source for those env vars or config-file contents. For secret values, the frontend should usually not receive them at all. If the browser can read a value, the user can inspect it too.
That is why API keys, database passwords, or private signing material do not belong in client-side React configuration, whether it comes from build args, ConfigMaps, or runtime files.
Option 3: Backend Endpoint
Sometimes the best answer is not environment variables in the frontend at all. If the configuration is dynamic, sensitive, or user-specific, expose it through a backend endpoint and let the React app fetch it after load.
That shifts the logic to a trusted server process and avoids treating frontend assets like a secret store.
Common Pitfalls
Expecting browser JavaScript to read Kubernetes pod environment variables directly is the core misunderstanding behind this question.
Putting secrets into a React bundle or runtime config file makes them visible to anyone using the app.
Rebuilding the frontend image for every environment can be wasteful when a runtime config file would let one image serve staging and production.
Forgetting framework-specific public prefixes can make build-time variables appear missing even though the build completed successfully.
Treating frontend configuration and backend secret management as the same problem leads to insecure designs.
Summary
- A React app in the browser cannot directly access Kubernetes container env vars.
- Use build-time injection for stable public values known during image creation.
- Use a runtime config file when one image should work across several environments.
- Use ConfigMaps for non-secret runtime values, not for browser-visible secrets.
- Move sensitive or dynamic configuration behind a backend API instead of exposing it to the client bundle.
Related reading
- How to access local Kubernetes minikube dashboard remotely?
- How to access mysql outside my kubernetes cluster?
- How to access NodePort in Minikube with docker driver?
- How to access private Docker Hub repository from Kubernetes on Vagrant
- How to access remote server with local phpMyAdmin client?
- How to access Spring-boot JMX remotely
- How to access POST form fields in Express
- How to access Session variables and set them in javascript?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.