Skip to content

Instantly share code, notes, and snippets.

@Cretezy
Last active October 18, 2019 21:44
Show Gist options
  • Save Cretezy/24eb198fe07be2b11ab1f387cc420628 to your computer and use it in GitHub Desktop.
Save Cretezy/24eb198fe07be2b11ab1f387cc420628 to your computer and use it in GitHub Desktop.
Node + Docker with proper caching
Dockerfile
node_modules
# Specify Node version
FROM node:latest
WORKDIR /tmp/build
ENV NODE_ENV=production
# Copy only required files for fetching dependencies
COPY package.json yarn.lock /tmp/build/
RUN yarn --prod
# Move node_modules outside (to avoid overriding)
RUN mv /tmp/build/node_modules /tmp/node_modules
# Copy the whole app in
COPY . /tmp/build
# Replace node_modules
RUN rm -rf /tmp/build/node_modules && cp -a /tmp/node_modules /tmp/build/node_modules
# Build the app
RUN yarn build
WORKDIR /usr/src/app
# Copy only app runtime (required files for app to run)
RUN cp -a /tmp/build/node_modules /tmp/build/dist /tmp/build/package.json /tmp/build/config /usr/src/app/ && rm -rf /tmp/build
# Set environment variables
ENV PORT=80
# Start app
CMD yarn start
EXPOSE 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment