"Create React App" with Docker
Yeah, as aholbreich mentioned, I'd use npm install
/ npm start
locally on my machine for development, just because it's so easy. It's probably possible with docker-compose
, mounting volumes etc. too, but I think it could be a bit fiddly to set up.
For deployment you can then very easily use a Dockerfile. Here's an example Dockerfile I'm using:
FROM node:6.9# Create app directoryRUN mkdir -p /src/appWORKDIR /src/app# to make npm test run only once non-interactivelyENV CI=true# Install app dependenciesCOPY package.json /src/app/RUN npm install && \ npm install -g pushstate-server# Bundle app sourceCOPY . /src/app# Build and optimize react appRUN npm run buildEXPOSE 9000# defined in package.jsonCMD [ "npm", "run", "start:prod" ]
You need to add the start:prod
option to your package.json:
"scripts": { "start": "react-scripts start", "start:prod": "pushstate-server build", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject"},
You can run the tests on your CI service with:
docker run <image> npm test
There's nothing stopping you from running this docker container locally as well to make sure things work as expected.
I recently made a small project called hello-docker-react who just does what the op is looking for.
It's made with docker-compose, create-react-app, yarn, a node image, and a small entrypoint script.
Live reload work flawlessly and I haven't found any problems yet.
here is good gide for this https://mherman.org/blog/dockerizing-a-react-app/
for development
# base imageFROM node:9.6.1# set working directoryRUN mkdir /usr/src/appWORKDIR /usr/src/app# add `/usr/src/app/node_modules/.bin` to $PATHENV PATH /usr/src/app/node_modules/.bin:$PATH# install and cache app dependenciesCOPY package.json /usr/src/app/package.jsonRUN npm install --silentRUN npm install react-scripts@1.1.1 -g --silent# start appCMD ["npm", "start"]
for production
# build environmentFROM node:9.6.1 as builderRUN mkdir /usr/src/appWORKDIR /usr/src/appENV PATH /usr/src/app/node_modules/.bin:$PATHCOPY package.json /usr/src/app/package.jsonRUN npm install --silentRUN npm install react-scripts@1.1.1 -g --silentCOPY . /usr/src/appRUN npm run build# production environmentFROM nginx:1.13.9-alpineCOPY --from=builder /usr/src/app/build /usr/share/nginx/htmlEXPOSE 80CMD ["nginx", "-g", "daemon off;"]