Skip to content

Instantly share code, notes, and snippets.

@tuxerrante
Created February 12, 2021 14:43
Show Gist options
  • Save tuxerrante/b8b3187a42c36e6a1da628b208ce1014 to your computer and use it in GitHub Desktop.
Save tuxerrante/b8b3187a42c36e6a1da628b208ce1014 to your computer and use it in GitHub Desktop.
How to expose an environment variable on a react app running in minikube, using configmaps
Dockerfile
==================
FROM bayesimpact/react-base
ENV REACT_APP_API_ENDPOINT=${REACT_APP_API_ENDPOINT:-"127.0.0.1:8081/atena/"}
==================
# I created a local private registry to sync my host with minikube
# You probably don't need to to this -> docker.local:5000/my-react:0.0.1
docker build -t docker.local:5000/my-react:0.0.1 .
kubectl create configmap --from-literal=REACT_APP_API_ENDPOINT=http://10.29.40.8:8081/atena/ cm-atena-api
kubectl create deployment atena-react --image=my_react --port=80 -o=yaml --dry-run=client > atena-react-dpl.yml
==================
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: atena-react
name: atena-react
spec:
replicas: 1
selector:
matchLabels:
app: atena-react
template:
metadata:
creationTimestamp: null
labels:
app: atena-react
spec:
containers:
- image: docker.local:5000/my-react:0.0.1
name: my-react
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
env:
- name: REACT_APP_API_ENDPOINT
valueFrom:
configMapKeyRef:
name: cm-atena-api
key: REACT_APP_API_ENDPOINT
==================
kubectl apply -f atena-react-dpl.yml
kubectl exec atena-react-7797fd6748-hvpvz -- printenv |grep atena
#> HOSTNAME=atena-react-7797fd6748-hvpvz
#> REACT_APP_API_ENDPOINT=http://10.29.40.8:8081/atena/
#====================================
# OPTIONAL: BUILD TAG AND PUSH LOCALLY
# this will run a local registry in docker to sync the host with minikube
# WARNING: other advanced configurations are needed -> https://stackoverflow.com/a/46068235/3673430
# minikube ssh
# echo "HOST_MACHINE_IP docker.local " >> /etc/hosts
# vim /etc/systemd/system/docker.service ==> --insecure-registry docker.local:5000
docker build -t docker.local:5000/my-react:0.0.1 .
docker run -d -p 5000:5000 --name registry registry:2
docker push docker.local:5000/my-react:0.0.1
curl -i http://docker.local:5000/v2/my-react/tags/list
==================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment