Kubernetes environment variables read in static react project - reactjs

Is is possible to read environment variables, defined in containers, in a react-app which is created using create-react-app?
eg
Deployment.yml:
apiVersion: apps/v1
kind: Deployment
..
spec:
template:
spec:
containers:
- name: my-container
env:
- name: REACT_APP_MY_ENV_VARIABLE
value: abc
Dockerfile:
..
CMD PORT=8080 npm start
Package.json:
"start":"react-scripts start"
"build":"react-scripts build"
In App.js:
process.env.REACT_APP_MY_ENV_VARIABLE returns undefined

You can export those variables in your Dockerfile by doing:
ARG REACT_APP_MY_ENV_VARIABLE
ENV REACT_APP_MY_ENV_VARIABLE=${REACT_APP_MY_ENV_VARIABLE}
..
CMD PORT=8080 npm start

Related

Module not found in Nest JS after deploying to App engine

I tried to deploy a Nest JS app to App Engine through Google Build Cloud & Manually
Both tries gives same error Modules not found
Error when deploying manually through Shell
Error when dploying using clouf build
My cloudbuild.yaml
- name: node:14.15.1
entrypoint: npm
args: ["install"]
- name: node:14.15.1
entrypoint: npm
args: ["run", "build"]
- name: node:14.15.1
entrypoint: npm
args: ["run", "create-env"]
env:
- "_APP_LUNE_DEV_TOKEN=${_APP_LUNE_DEV_TOKEN}"
- "_BUCKET_NAME=${_BUCKET_NAME}"
- "_FIREBASE_API_KEY=${_FIREBASE_API_KEY}"
- "_FIREBASE_APP_ID=${_FIREBASE_APP_ID}"
- "_FIREBASE_AUTH_DOMAIN=${_FIREBASE_AUTH_DOMAIN}"
- "_FIREBASE_MESSAGING_SENDER_ID=${_FIREBASE_MESSAGING_SENDER_ID}"
- "_FIREBASE_PROJECT_ID=${_FIREBASE_PROJECT_ID}"
- "_FIREBASE_STORAGE_BUCKET=${_FIREBASE_STORAGE_BUCKET}"
- "_GOOGLE_APPLICATION_CREDENTIALS=${_GOOGLE_APPLICATION_CREDENTIALS}"
- "_LUNE_API_BASE_URL=${_LUNE_API_BASE_URL}"
- "_STRIPE_SECRET_KEY=${_STRIPE_SECRET_KEY}"
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy"]
timeout: "1600s"
options:
logging: CLOUD_LOGGING_ONLY
My app.yaml
runtime: nodejs14
service: backend
What I'm doing wrong ?
The problem was caused by Nest Js CLI Aliases, When I changed src/example/service to ../example/service the deployment worked
It's recommended to use typescript aliases in tsconfig file instead of CLI

How can I change the port of a React App running inside docker

I was dockerising an app of mine but I wanted to access it on port 80 on my machine, every time a change the port in docker-composer.yml it returnes the error:
ERROR: for site Cannot create container for service site: mount denied:
the source path "dcfffb89fd376c0d955b0903e3aae045df32a073a6743c7e44b3214325700576:D:\\projetos\\portfolio\\site\\node_modules:rw"
too many colons
ERROR: Encountered errors while bringing up the project.
Im running on windows
docker-composer.yml
version: '3.7'
services:
site:
container_name: site
build: ./site
volumes:
- 'D:\projetos\portfolio\site'
- 'D:\projetos\portfolio\site\node_modules'
ports:
- 3000:3000
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
- COMPOSE_CONVERT_WINDOWS_PATHS=true
command: npm start
Dockerfile
FROM node:16.13.1-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]
I was using the wrong path pattern, on windows you have to use /c/path/to/volume since the ":" is used inside docker stuff(don't know what), also removed the command COMPOSE_CONVERT_WINDOWS_PATHS=true and worked just fine.

How to override env variables in React JS application with Kuberenetes?

I've a simle React JS application and it's using a environment variable(REACT_APP_BACKEND_SERVER_URL) defined in .env file. Now I'm trying to deploy this application to minikube using Kubernetes.
This is my deployment file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-ui
spec:
replicas: 1
selector:
matchLabels:
app: test-ui
template:
metadata:
name: test-ui-pod
labels:
app: test-ui
spec:
containers:
- name: test-ui
image: test-ui:1.0.2
ports:
- containerPort: 80
env:
- name: "REACT_APP_BACKEND_SERVER_URL"
value: "http://127.0.0.1:59058"
When I run the application, it's working but REACT_APP_BACKEND_SERVER_URL is giving the value which I defined in .env file. Not the one I'm overriding. Can someone help me with this please? How to override the env variable using Kubernetes deployment?
After starting the app with your deployment YAML and checking for the environment variables I see the environment variables for that environment variable.
REACT_APP_BACKEND_SERVER_URL=http://127.0.0.1:59058
you can check that by doing an kubectl exec -it <pod-name> -- sh and running env command.
So you can see that REACT_APP_BACKEND_SERVER_URL is there in the environment variables. It's available for your application to use. I suspect that you may need to understand better from the React app side on how to use the .env file.

Network IP from Docker not working for React + Vite.js therefore can't access k8s pod

I have a very simple react typescript application and using Vite for the first time to replace Webpack.
I have the following vite.config.js:
server: {
watch: {
usePolling: true,
},
open: false,
host: '0.0.0.0',
},
and created a Dockerfile with these instructions:
FROM node:latest
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
COPY ./build-prod ./build-prod
COPY ./node_modules ./node_modules
RUN npm install husky -g --production
RUN npm install esbuild-linux-arm64 --production
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
When I now run docker run -p 3000:3000 hello-world-app-frontend I can access my app with http://localhost:3000/ but opening the network address http://172.17.0.3:3000/ just loads an untitled window.
I think this is especially a problem for me as I want to create a basic Kubernetes config like this:
kind: Deployment
apiVersion: apps/v1
metadata:
name: hello-world-app-frontend
spec:
replicas: 2
selector:
matchLabels:
app: hello-world-app-frontend
template:
metadata:
labels:
app: hello-world-app-frontend
spec:
containers:
- name: hello-world-app-frontend
image: hello-world-app-frontend
imagePullPolicy: Never
ports:
- containerPort: 3000
restartPolicy: Always
kind: Service
apiVersion: v1
metadata:
name: hello-world-app-frontend
spec:
type: NodePort
ports:
- port: 3000
targetPort: 3000
protocol: TCP
nodePort: 31000
selector:
app: hello-world-app-frontend
But opening the IP address from my Pod returns nothing in my Chrome (f.e. http://10.106.213.128:3000/).
NAMESPACE NAME READY STATUS RESTARTS AGE
default pod/hello-world-app-frontend-77899b46d7-cc4td 1/1 Running 0 16h
default pod/hello-world-app-frontend-77899b46d7-vqtbz 1/1 Running 0 16h
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/hello-world-app-frontend NodePort 10.106.213.128 <none> 3000:31000/TCP 16h
Can somebody give me a few hints how I can access the React application from my k8s pod?

How do I deploy a react pwa to IBM Cloud Kubernetes cluster?

I have created the free IBM Cloud Kubernetes cluster and try to deploy my react pwa(created with create-react-app) in it from Docker image. I managed to deploy the app in the cluster, but my service worker doesn't work.
When I do npm run build and serve -s build everything in the app is working fine in localhost:5000.
But in the deployed app the serviceWorker is not found in the navigator and it never register the service worker:
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
...
//never goes in here
registerValidSW(swUrl, config);
});
And also getting this error when trying to access caches in the deployed app:
Uncaught (in promise) ReferenceError: caches is not defined
at c (runtime.js:45)
at Generator._invoke (runtime.js:264)
at Generator.O.forEach.e.<computed> [as next] (runtime.js:98)
at r (asyncToGenerator.js:3)
at l (asyncToGenerator.js:25)
at asyncToGenerator.js:32
at new Promise (<anonymous>)
at t.getRequests (asyncToGenerator.js:21)
at t.value (index.js:142)
My Dockerfile looks like this:
FROM node:alpine as builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx
EXPOSE 80
COPY --from=builder /app/build /usr/share/nginx/html
And my deployment.yaml is looking like this:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
labels:
app: app
spec:
type: NodePort
ports:
- port: 80
name: http
selector:
app: app
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: app
spec:
containers:
- name: my-app
image: us.icr.io/my-app-namespace/my-app
ports:
- containerPort: 8080
Which node and npm version do you have installed locally?
You set up in your Dockerfile the latest version of node:
FROM node:alpine as builder
Identify the version that you have locally running:
node --version
Now select the right version here Docker Hub node tags
Example: If your node version is 8, you need set some version like:
FROM node:8-alpine

Resources