N8n-custom docker image > 1G

Describe the issue/error/question

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?

Thanks.

1 Like

That is strange, it should not do that. The docker image is set up to install only production dependencies:
Screenshot from 2022-01-25 10-06-02

The nightly image which does gets build using that docker image is also just 266MB and not 1G+.

So sadly no idea right now why it behaves that way for you.

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

...

Hey @sfenig,

Can you share the process you followed to build the container and include any changes you made?

I just checked an image I made that I pushed to dockerhub to test a new node and that comes in at 225mb.

Hi @jon ,
I was building the n8n-custom docker image like this.

docker build -f docker/images/n8n-custom/Dockerfile .

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.

Thanks for the example @sfenig. How did you launch verdaccio from Dockerfile? Are you able to share the complete Dockerfile with your example?