I have an SPA built with React (create-react-app) and I want to deploy it with docker.
I have a Dockerfile in my SPA folder and I want to build my SPA inside the container.
There is client-side routing in the app, so in my understanding, I can’t really just host the built files as regular static files.
I’m wondering what are the advantages and disadvantages of using Nginx (or something else) to host the SPA, versus what I’m doing atm with a npm module called serve (already installed in the dependencies).
At the moment my Dockerfile looks like this :
FROM node:alpine WORKDIR /usr/src/app COPY package.json . RUN npm install COPY . . RUN npm run-script build CMD ["node_modules/.bin/serve", "-s", "build", "-l", "3000"]
If I wanted to use nginx, I would remove serve from my dependencies and I would do the following changes to the Dockerfile:
FROM node:alpine as spa-builder WORKDIR /usr/src/app COPY package.json . RUN npm install COPY . . RUN npm run-script build FROM nginx COPY --from=spa-builder /usr/src/app/build /usr/share/nginx/html CMD ["nginx", "-g", "daemon off;"]
TO BE CLEAR :
My approach is working great, I just don’t know if it’s the best way.
It seems to me Nginx is very popular (hence my question) but I don’t see the advantages. Would you actually deploy this SPA differently ?