Building a docker image using n8n-custom ends up copying all of the dev dependencies to the new image. It results in an image over 1G most of which is in the node_modules directory. The public n8n docker image is half the size. It’s installing from the public npm library.
Does anyone have a solution to only include what’s needed for production?
I did a little more digging. Take with a grain of salt, I’m a lerna/npm novice. Appears the lerna production tag does not have an effect on the bootstrap call (despite the docs saying), it will always load/hoist all dependencies. Rough summary from a code owner - it’s for publishing, not for deployment. lerna bootstrap -- --production still install devDependencies · Issue #2531 · lerna/lerna · GitHub
I was able to get it down closer to what you have in the nightly docker image by publishing then installing package from a local package manager (using verdaccio).
Also note, not sure what the npm install --production does since there are no dependencies in the main package.json.
...
RUN lerna bootstrap --hoist -- --production
RUN npm run build
COPY .npmrc.local ./.npmrc #.npmrc needs credentials for publishing
RUN lerna publish from-package
RUN rm -f .npmrc
# 2. Start with a new clean image ....
...
RUN npm_config_user=root npm_config_registry=http://172.20.0.1:4873 npm install -g n8n #172.20.0.1:4873 verdaccio on docker host
...
it copies the entire data directory from the builder image, and the node_modules in there are 800M+ because lerna doesn’t respect the production flag.
the non-custom image (docker/images/n8n/Dockerfile) does build smaller because it benefits from installing from the repository, doesn’t have to build, and doesn’t download the devDependencies. That’s how I ended up with the workaround in my previous comment.