Skip to content

Instantly share code, notes, and snippets.

@fed135
Last active June 9, 2021 19:26
Show Gist options
  • Save fed135/af42be63989e734acc025090fd120186 to your computer and use it in GitHub Desktop.
Save fed135/af42be63989e734acc025090fd120186 to your computer and use it in GitHub Desktop.
Docker commands to shed some extra weight from your node modules
# Final production image
# ===============================
FROM node:14 as final
RUN mkdir /app && chown node:node /app
RUN wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.2/dumb-init_1.2.2_amd64
RUN chmod +x /usr/local/bin/dumb-init
USER node
WORKDIR /app
EXPOSE 9001
ENTRYPOINT ["/usr/local/bin/dumb-init", "--"]
# Base utils image
# ===============================
FROM node:14 AS base
RUN mkdir /app && chown node:node /app
WORKDIR /app
USER node
# Run tests
# ===============================
COPY --chown=node:node . .
RUN yarn
RUN yarn run lint && yarn run test
# Remove dev dependencies
# ===============================
RUN rm -fr node_modules
RUN yarn --prod
# Remove some extra weight from the image and dependencies
RUN echo du -a /var | sort -n -r | head -n 10
RUN find . -name "*.d.ts" -type f -delete
RUN find . -name "*.md" -type f -delete
RUN find . -name "*.markdown" -type f -delete
RUN find . -name "*.editorconfig" -type f -delete
RUN find . -name "*.eslintrc" -type f -delete
RUN find . -name "*.travis.yml" -type f -delete
RUN find . -name "*.istambul.yml" -type f -delete
RUN find . -name "*.jscs.json" -type f -delete
RUN find . -name "package-lock.json" -type f -delete
RUN find . -name ".npmignore" -type f -delete
RUN find . -name "yarn.lock" -type f -delete
RUN find . -name "Makefile" -type f -delete
RUN find . -name "test.js" -type f -delete
RUN find . -name "*.js.flow" -type f -delete
RUN find . -name "*.js.map" -type f -delete
RUN find . -name "*.mjs" -type f -delete
RUN find . -name "Dockerfile" -type f -delete
RUN find . -name "AUTHORS" -type f -delete
RUN find . -name "*.dockerignore" -type f -delete
RUN find . -name "tslint.json" -type f -delete
RUN find . -name "bower.json" -type f -delete
RUN find . -name "benchmark.js" -type f -delete
RUN find . -name "*.test.js" -type f -delete
RUN find . -name "*.test.ts" -type f -delete
RUN find . -name "*.spec.js" -type f -delete
RUN find . -name "*.spec.ts" -type f -delete
RUN find . -name "*.nycrc" -type f -delete
RUN find . -name test -type d -print0|xargs -0 -r rm -rf --
RUN find . -name tests -type d -print0|xargs -0 -r rm -rf --
RUN find . -name example -type d -print0|xargs -0 -r rm -rf --
RUN find . -name examples -type d -print0|xargs -0 -r rm -rf --
RUN find . -name benchmark -type d -print0|xargs -0 -r rm -rf --
RUN find . -name benchmarks -type d -print0|xargs -0 -r rm -rf --
# Final image
# ===============================
FROM final
COPY --from=base --chown=node:node /app .
CMD ["node","index.js"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment