{"id":24106178,"url":"https://github.com/mikesparr/tutorial-react-docker","last_synced_at":"2025-05-12T13:22:40.572Z","repository":{"id":49778580,"uuid":"122128135","full_name":"mikesparr/tutorial-react-docker","owner":"mikesparr","description":"Boilerplate React app in Docker container with ENV args","archived":false,"fork":false,"pushed_at":"2018-06-27T14:53:09.000Z","size":125,"stargazers_count":68,"open_issues_count":1,"forks_count":43,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T22:33:05.469Z","etag":null,"topics":["create-react-app","docker","environment-variables","react"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikesparr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-19T22:23:16.000Z","updated_at":"2025-03-10T01:02:45.000Z","dependencies_parsed_at":"2022-08-29T14:30:13.043Z","dependency_job_id":null,"html_url":"https://github.com/mikesparr/tutorial-react-docker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikesparr%2Ftutorial-react-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikesparr%2Ftutorial-react-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikesparr%2Ftutorial-react-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikesparr%2Ftutorial-react-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikesparr","download_url":"https://codeload.github.com/mikesparr/tutorial-react-docker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253745267,"owners_count":21957335,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["create-react-app","docker","environment-variables","react"],"created_at":"2025-01-10T21:18:08.296Z","updated_at":"2025-05-12T13:22:40.546Z","avatar_url":"https://github.com/mikesparr.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Docker Tutorial\nThis repository demonstrates how to bootstrap a React app using `create-react-app` and add  Docker container and using ENV variables. This is the TL;DR version of normal trial and error to get Docker and ENV variables to work nicely in a React app. The goal is allowing you to package and deploy your app to any environment and adhere to 12-factor-app principles.\n\n# Requirements\n## NodeJS version 6 or later\nYou need NodeJS installed on your machine at least version 6, and a general understanding of NPM and how to install, configure, and start a Node app. You don't need experience with React to follow this tutorial, however it is preferred.\n\n## Docker\nIn order to test the Docker container you need to be able to perform `docker` commands. Familiarity with building and running images is preferred.\n\n# Setup\n1. Bootstrap your application with `create-react-app`\n```\nnpx create-react-app my-docker-react-app\n```\n\n2. Verify application runs\n```\ncd my-docker-react-app\nnpm install\nnpm start\n```\n\nYour browser will likely launch a window or tab for `http://localhost:3000` and you should see a React icon and welcome screen. If everything is running, then `CTRL + C` to stop the app and proceed. If it is not working, visit create-react-app's website for instructions and troubleshooting tips for your environment.\n\n# Customize app to use ENV variables\n## Using ENV variables\nBootstrapped React apps (using `create-react-app`) strip most environment variables with the exception of `NODE_ENV` and any variables that are prefixed with `REACT_APP_\u003cyour var\u003e`. For this tutorial we will prefix all variables with `REACT_APP_` to work with the default install configuration.\n\n1. Add local ENV variables\nAdd a `.env.local` file with variables `REACT_APP_ENVIRONMENT` and `REACT_APP_TIMEZONE` and some values. (SEE: env.local example but name with `.` prefix and `.gitignore` will ignore)\n\n2. Add a `Config.js` file to centralize ENV variables for use in the application. For a larger application you may want to move this to a `lib/` folder.\n\n3. Print variable values on default `App.js` screen to confirm the application is using them.\n\n4. Verify application runs (using `npm start` as before)\n\n# Add Docker container\nIf you tested the app above, and the values you set in your local environment appear, then proceed to package your app in a container.\n\n## Create Docker image and run script\n1. Add `Dockerfile` as example in this repository\n\n2. Add `run` script (this is the secret)\nAdding a separate script for the `CMD` of your `Dockerfile` allows you to inject the container's ENV params during `docker run ...`. If you build your React app in the `Dockerfile` it will not recognize the ENV variables you pass as arguments when you run it.\n\n3. Set script permissions\n**IMPORTANT** `chmod +x run` to allow Docker to execute the script during the `CMD`. Alternatively you could add another layer to the `Dockerfile` and a `RUN chmod+x run` step.\n\n## Build and run Docker image\n1. Build docker image\n```\ndocker build -t myimage .\n```\n\n2. Run image and add ENV variables to container\n```\ndocker run --name myapp -p 8080:5000 \\\n-e REACT_APP_ENVIRONMENT=docker \\\n-e REACT_APP_TIMEZONE=America/Pacific \\\n--rm myimage\n```\n\n3. Test that your app is running on port 8080 in local browser at http://localhost:8080 and you should see the default screen but the values match ENV variable values passed in your run command.\n\n# Congratulations\nIf you made it this far, then you have successfully extended `create-react-app` to use environment variables and added a Docker image that allows you to configure your application dynamically to run anywhere!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikesparr%2Ftutorial-react-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikesparr%2Ftutorial-react-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikesparr%2Ftutorial-react-docker/lists"}