react + docker, part 2
public repo for this post: https://github.com/lombardo-chcg/dockerized-create-react-app
–
In part 1 we created a basic javascript server that will serve up our React web app. Now we need to “containizer” the app using Docker.
As a reminder of our basic workflow:
create a simple server using express that will serve up the static UI content based based on an http request- After finishing up the UI work, use the
create-react-app
build script to create a “productionized” version of the React app - combine both pieces into a docker image
For step two, creating a production build with the create-react-app tool is as simple as running a script: npm run build
Now to part three. Since a JS project’s node_modules
can be amongst the largest around, and the necessary components were already compiled as part of the build step, it makes sense to skip those in our Docker build process. This way, the “build context” that is passed to the Docker daemon doesn’t include a bunch of junk we don’t need. We can specify that in a .dockerignore
file at the project root:
Now for the Dockerfile. Here’s an annotated version explaining all the steps:
All that’s left to do now is run it!
localhost
in any browser will show the React app.
And to prove the proxy is working, try curl localhost/proxy
from a terminal and you should see Google’s page content. Now just point that proxy target at your backend server and don’t sweat the CORS in the browser.