Git Product home page Git Product logo

rafaelcalleja / react-js-env-runtime-docker Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 189 KB

Example React App production build with environment variables replaced in runtime docker/kubernetes

Dockerfile 30.55% Makefile 10.13% Shell 5.33% HTML 53.98%
react kubernetes docker environment-variables runtime install npm production reactjs yarn envsubst nginx demo example hello-world replaced

react-js-env-runtime-docker's Introduction

React

src/index.js

const hello = process.env.REACT_APP_HELLO;
const world = process.env.REACT_APP_WORLD;

root.render(
    <h1>{ hello }, {world}</h1>
);

docker

docker run -it --rm \
       -p 80:80 \
       -e REACT_APP_HELLO=runtime -e REACT_APP_WORLD=replace \
       docker.io/rafaelcalleja/react-app-hello-world:latest

html render output

runtime, replace

how it works - Dockerfile

create .env file during build phase setting value as environment variable name to later replace it using envsubst

# define runtime variables as VAR=\${VAR}
ENV REACT_APP_HELLO "\\\${REACT_APP_HELLO}"
ENV REACT_APP_WORLD "\\\${REACT_APP_WORLD}"

# create react app production build
RUN rm -f .env* ; \
    printenv > .env && \
    yarn build

FROM nginx image create an entrypoint to replace text ${VAR} in builded js files using environment variables values with envsubst command at runtime

#create nginx entrypoint to replace environment variables in build js at runtime
RUN echo "\
export ENV_LIST=\"\$(printenv | awk -F= '{print $1}' | sed 's/^/\$/g' | paste -sd,)\"; \
find /usr/share/nginx/html/static/ -type f -name \"*.js\"| \
xargs -I % sh -c 'envsubst \"\${ENV_LIST}\" < \"%\" > \"%.tmp\" && mv \"%.tmp\" \"%\"'" \
> /docker-entrypoint.d/40-envsubst-react-environment.sh && chmod +x /docker-entrypoint.d/40-envsubst-react-environment.sh

kubernetes

kubernetes/pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: react-app
spec:
  containers:
    - name: web
      image: docker.io/rafaelcalleja/react-app-hello-world:latest
      env:
        - name: REACT_APP_HELLO
          value: "runtime"
        - name: REACT_APP_WORLD
          value: "replace"
      ports:
        - name: web
          containerPort: 80
          protocol: TCP 
kubectl -n test apply -f kubernetes/pod.yaml 

react-js-env-runtime-docker's People

Contributors

rafaelcalleja avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.