Skip to content

Instantly share code, notes, and snippets.

@grantvanhorn
Created October 18, 2019 01:35
Show Gist options
  • Save grantvanhorn/000592ce46ab068c929e5b95d9451db8 to your computer and use it in GitHub Desktop.
Save grantvanhorn/000592ce46ab068c929e5b95d9451db8 to your computer and use it in GitHub Desktop.
Create React App Dockerfile
## Stage 1 (multi environment base)
# This is our inital setup. The minimum for production while still
# allowing for other stages to build successfully (without production settings)
FROM node:10.16.3-alpine AS base
EXPOSE 3000
ENV NODE_ENV=production
WORKDIR /opt
COPY package*.json ./
RUN npm ci \
&& npm cache clean --force
# --------------------------------------------------------------------------------------------------
## Stage 2 (development)
# We don't COPY in this stage because we want to bind-mount the source from
# the host machine.
FROM base AS dev
ENV NODE_ENV=development
ENV PATH=/opt/node_modules/.bin:$PATH
WORKDIR /opt
RUN npm install --only=development
WORKDIR /opt/app
CMD ["npm", "start"]
# --------------------------------------------------------------------------------------------------
## Stage 3 (building)
# We give building its own stage so it can have access to both dev and non dev packages. It
# also creates a nice base for other stages that rely on having dev packages.
FROM dev AS builder
WORKDIR /opt/app
COPY . .
RUN npm run build
# --------------------------------------------------------------------------------------------------
## Stage 4 (lint)
FROM builder AS lint
CMD ["npm", "run", "lint"]
# --------------------------------------------------------------------------------------------------
## Stage 5 (test)
FROM builder AS test
CMD ["npm", "run", "test"]
# --------------------------------------------------------------------------------------------------
## Stage 6 (prod)
# We need production to serve nginx. Replace the default nginx html with our react bundle.
FROM nginx:1.16.0-alpine AS production
COPY --from=builder /opt/app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
# --------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment