feat(docker): reduce the size of alpine docker images (#3973)

* reduce redundant code between the two alpine Dockerfiles
* reduce the docker image build times
* reduce the `n8n` image size from 1.17GB to 462MB
* reduce the `n8n-custom` image size from 671MB to 460MB
* reduce the size of npm tree by making all typing packages devDependencies

Partially resolves this ticket: N8N-3252
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2022-08-31 15:25:52 +02:00
committed by GitHub
parent 8fc9f07f39
commit 398adb23e8
13 changed files with 277 additions and 185 deletions

View File

@@ -0,0 +1,4 @@
audit = false
fund = false
loglevel = warn
update-notifier = false

View File

@@ -0,0 +1,20 @@
ARG NODE_VERSION=16
FROM node:${NODE_VERSION}-alpine
WORKDIR /home/node
COPY .npmrc /usr/local/etc/npmrc
RUN \
apk add --update graphicsmagick tini tzdata ca-certificates && \
npm install -g npm@latest full-icu && \
rm -rf /var/cache/apk/* /root/.npm /tmp/* && \
# Install fonts
apk --no-cache add --virtual fonts msttcorefonts-installer fontconfig && \
update-ms-fonts && \
fc-cache -f && \
apk del fonts && \
find /usr/share/fonts/truetype/msttcorefonts/ -type l -exec unlink {} \; && \
rm -rf /var/cache/apk/* /tmp/*
ENV NODE_ICU_DATA /usr/local/lib/node_modules/full-icu
EXPOSE 5678/tcp

View File

@@ -1,55 +1,34 @@
ARG NODE_VERSION=16
# 1. Create an image to build n8n
FROM node:16-alpine as builder
FROM n8nio/base:${NODE_VERSION} as builder
# Update everything and install needed dependencies
USER root
RUN \
apk --no-cache add git && \
npm install -g run-script-os turbo
# Install all needed dependencies
RUN apk --update add --virtual build-dependencies python3 build-base ca-certificates git && \
npm_config_user=root npm install -g npm@latest run-script-os turbo
WORKDIR /data
COPY turbo.json .
COPY package.json .
COPY package-lock.json .
COPY packages/cli/ ./packages/cli/
COPY packages/core/ ./packages/core/
COPY packages/design-system/ ./packages/design-system/
COPY packages/editor-ui/ ./packages/editor-ui/
COPY packages/nodes-base/ ./packages/nodes-base/
COPY packages/workflow/ ./packages/workflow/
RUN rm -rf node_modules packages/*/node_modules packages/*/dist
COPY turbo.json package.json package-lock.json ./
COPY packages ./packages
RUN chown -R node:node .
RUN npm config set legacy-peer-deps true
RUN npm install --loglevel notice
RUN npm run build
USER node
RUN \
npm ci && \
npm run build && \
# TODO: removing dev dependecies is deleting `bn.js`, which breaks the Snowflake node
npm prune --omit=dev && \
npm i --omit=dev bn.js && \
find . -type f -name "*.ts" -o -name "*.js.map" -o -name "*.vue" | xargs rm &&\
rm -rf node_modules/.cache packages/*/node_modules/.cache packages/*/.turbo .config .npm /tmp/*
# 2. Start with a new clean image with just the code that is needed to run n8n
FROM node:16-alpine
USER root
RUN apk add --update graphicsmagick tzdata tini su-exec git
WORKDIR /data
# Install all needed dependencies
RUN npm_config_user=root npm install -g npm@latest full-icu
# Install fonts
RUN apk --no-cache add --virtual fonts msttcorefonts-installer fontconfig && \
update-ms-fonts && \
fc-cache -f && \
apk del fonts && \
find /usr/share/fonts/truetype/msttcorefonts/ -type l -exec unlink {} \;
ENV NODE_ICU_DATA /usr/local/lib/node_modules/full-icu
COPY --from=builder /data ./
COPY docker/images/n8n-custom/docker-entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT ["tini", "--", "/docker-entrypoint.sh"]
EXPOSE 5678/tcp
FROM n8nio/base:${NODE_VERSION}
COPY --from=builder /home/node ./
COPY docker/images/n8n-custom/docker-entrypoint.sh ./
USER node
ENV NODE_ENV=production
ENTRYPOINT ["tini", "--", "./docker-entrypoint.sh"]

View File

@@ -1,25 +1,16 @@
#!/bin/sh
if [ -d /root/.n8n ] ; then
chmod o+rx /root
chown -R node /root/.n8n
ln -s /root/.n8n /home/node/
fi
chown -R node /home/node
if [ "$#" -gt 0 ]; then
# Got started with arguments
COMMAND=$1;
if [[ "$COMMAND" == "n8n" ]]; then
shift
(cd packages/cli; exec su-exec node ./bin/n8n "$@")
(cd packages/cli; exec node ./bin/n8n "$@")
else
exec su-exec node "$@"
exec node "$@"
fi
else
# Got started without arguments
exec su-exec node ./packages/cli/bin/n8n
cd packages/cli; exec node ./bin/n8n
fi

View File

@@ -1,36 +1,22 @@
FROM node:16-alpine
ARG NODE_VERSION=16
FROM n8nio/base:${NODE_VERSION}
ARG N8N_VERSION
RUN if [ -z "$N8N_VERSION" ] ; then echo "The N8N_VERSION argument is missing!" ; exit 1; fi
# Update everything and install needed dependencies
RUN apk add --update graphicsmagick tzdata git tini su-exec
ENV NODE_ENV=production
RUN set -eux; \
apkArch="$(apk --print-arch)"; \
case "$apkArch" in \
'armv7') apk --no-cache add --virtual build-dependencies python3 build-base;; \
esac && \
npm install --loglevel=info -g --omit=dev n8n@${N8N_VERSION} && \
case "$apkArch" in \
'armv7') apk del build-dependencies;; \
esac && \
find /usr/local/lib/node_modules/n8n -type f -name "*.ts" -o -name "*.js.map" -o -name "*.vue" | xargs rm && \
rm -rf /root/.npm
# # Set a custom user to not have n8n run as root
USER root
# Install n8n and the also temporary all the packages
# it needs to build it correctly.
RUN apk --update add --virtual build-dependencies python3 build-base ca-certificates && \
npm config set python "$(which python3)" && \
npm_config_user=root npm install -g npm@latest full-icu n8n@${N8N_VERSION} && \
apk del build-dependencies \
&& rm -rf /root /tmp/* /var/cache/apk/* && mkdir /root;
# Install fonts
RUN apk --no-cache add --virtual fonts msttcorefonts-installer fontconfig && \
update-ms-fonts && \
fc-cache -f && \
apk del fonts && \
find /usr/share/fonts/truetype/msttcorefonts/ -type l -exec unlink {} \; \
&& rm -rf /root /tmp/* /var/cache/apk/* && mkdir /root
ENV NODE_ICU_DATA /usr/local/lib/node_modules/full-icu
WORKDIR /data
COPY docker-entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT ["tini", "--", "/docker-entrypoint.sh"]
EXPOSE 5678/tcp
USER node
COPY docker-entrypoint.sh ./
ENTRYPOINT ["tini", "--", "./docker-entrypoint.sh"]

View File

@@ -1,17 +1,8 @@
#!/bin/sh
if [ -d /root/.n8n ] ; then
chmod o+rx /root
chown -R node /root/.n8n
ln -s /root/.n8n /home/node/
fi
chown -R node /home/node
if [ "$#" -gt 0 ]; then
# Got started with arguments
exec su-exec node "$@"
exec node "$@"
else
# Got started without arguments
exec su-exec node n8n
exec n8n
fi