Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**/.env
node_modules
packages/node-dev
packages/*/node_modules
packages/*/dist
packages/*/.turbo
.git
35 changes: 35 additions & 0 deletions .github/workflows/docker-base-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Docker Base Image CI

on:
workflow_dispatch:
inputs:
node_version:
description: 'Node.js version to build this image with.'
type: string
required: true
default: '16'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
netroy marked this conversation as resolved.
Show resolved Hide resolved
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build
uses: docker/build-push-action@v2
with:
context: ./docker/images/n8n-base
build-args: |
NODE_VERSION=${{github.event.inputs.node_version}}
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: |
${{ secrets.DOCKER_USERNAME }}/base:${{ github.event.inputs.node_version }}
netroy marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion .github/workflows/docker-images-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
file: ./docker/images/n8n-custom/Dockerfile
platforms: linux/amd64
push: true
tags: n8nio/n8n:${{ github.event.inputs.tag || 'nightly' }}
tags: ${{ secrets.DOCKER_USERNAME }}/n8n:${{ github.event.inputs.tag || 'nightly' }}
no-cache: true
- name: Call Success URL - optionally
run: |
Expand Down
4 changes: 4 additions & 0 deletions docker/images/n8n-base/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
audit = false
fund = false
loglevel = warn
update-notifier = false
20 changes: 20 additions & 0 deletions docker/images/n8n-base/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
71 changes: 25 additions & 46 deletions docker/images/n8n-custom/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,55 +1,34 @@
# 1. Create an image to build n8n
FROM node:16-alpine as builder

# Update everything and install needed dependencies
USER root
ARG NODE_VERSION=16

# 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
# 1. Create an image to build n8n
FROM n8nio/base:${NODE_VERSION} as builder

WORKDIR /data
RUN \
apk --no-cache add git && \
npm install -g run-script-os turbo

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


# 2. Start with a new clean image with just the code that is needed to run n8n
FROM node:16-alpine

USER root
USER node
netroy marked this conversation as resolved.
Show resolved Hide resolved

RUN apk add --update graphicsmagick tzdata tini su-exec git
RUN \
npm ci && \
netroy marked this conversation as resolved.
Show resolved Hide resolved
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/*

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
# 2. Start with a new clean image with just the code that is needed to run n8n
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"]
15 changes: 3 additions & 12 deletions docker/images/n8n-custom/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
50 changes: 18 additions & 32 deletions docker/images/n8n/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

# # 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
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

USER node
COPY docker-entrypoint.sh ./
ENTRYPOINT ["tini", "--", "./docker-entrypoint.sh"]
13 changes: 2 additions & 11 deletions docker/images/n8n/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
Loading