BretFisher/node-docker-good-defaults

Multi-stage builds / decreasing size

robertvanhoesel opened this issue · 6 comments

First of all, thank you (+contributers) so much for creating this example project. It really helped me get past cold feet and actually set up a first nice Docker project!

As this guide popped up as my starting point I imagine others starting here as well. For completeness, I think it's worth highlighting multi-stage builds and smaller parent images to decrease your image size.
It's worth noting that the production version of this Hello World app still includes all the devDependencies, unit-tests and files that are not strictly necessary. Of course, multi-stage builds make more sense in larger applications with client-side js.

Also, just using node:alpine as parent image help getting from 600mb+ to ±70mb, worth mentioning too.

If leaving those out was intentional that's fine, you point to a good further reading resource, if you agree I'm more than happy to create a PR with these comments/changes implemented.

Cheers!

Hey there, great ideas, thanks for offering to help.

I started this as debian-only over a year ago because alpine was not as common. Now many have made it their default and I think a non-default dockerfile would be a great addition for comparison.

  1. I'd start with the Dockerfile.alpine PR if I were you. Then I can have Docker Hub auto-build a tagged image for alpine. Try to make it match line for line with the debian one so people can easily correlate. This should be easier since we're not doing apt installs in this example (but we likely should at some point just to highlight one of the big differences... package managers.

  2. If you'd like to start a PR for multi-stage I'm all for it. Along with that will need to be a workflow explanation of how it should be used. Maybe use target build options in compose file so local dev's can have all the dev tools while prod uses a later stage.... up to you.

Hey @BretFisher I would like to work on this and start with multi-stage dockerfile.

OK, see other PR's active as well.

Hey @BretFisher, I'd like to make a PR for multistage builds! Is there any specific way you'd like this done or anything you'd want included?

I was thinking of adding a build stage that would build with babel or typescript (would add support for #39) and a production stage that would install only production dependencies and copy the built files. Along with a development stage that would rebuild on nodemon restart and have all dependencies for development and debugging etc. This would be using the target build option in compose file (one issue would be that the compose file version would need to be bumped to 3.4).

What are your thoughts on this?

Sure, but also note I'm working on multi-stage myself, but it would be great to compare solutions. I haven't pushed a commit yet so feel free to try using multi-stage yourself and submitting a PR of what you find useful.

Since I have multiple multistage examples now listed in the README (which are in other repos), I'm going to move this to a discussion.