From e017133d2459a5ac8800322a281b07b7e642893b Mon Sep 17 00:00:00 2001 From: Jack Baron Date: Sat, 17 Jul 2021 13:49:09 +0100 Subject: [PATCH 1/5] feat: optimize dockerfile use new dockerfile syntaxes leverage multi-stage builds strip yarn cache from image use stricter yarn install command run as a non-root user --- Dockerfile | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index f64cc622c6..ba388d8f64 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,43 @@ -FROM node:14-alpine +# syntax=docker/dockerfile:1.2 +FROM node:14-alpine AS deps-common -ENV APP_PATH /opt/outline -RUN mkdir -p $APP_PATH +ARG APP_PATH /opt/outline +WORKDIR $APP_PATH +COPY ./package.json ./yarn.lock ./ +# --- +FROM deps-common AS deps-dev +RUN yarn install --no-optional --frozen-lockfile && \ + yarn cache clean + +# --- +FROM deps-common AS deps-prod +RUN yarn install --production=true --frozen-lockfile && \ + yarn cache clean + +# --- +FROM node:14-alpine AS builder WORKDIR $APP_PATH -COPY package.json ./ -COPY yarn.lock ./ - -RUN yarn --pure-lockfile - COPY . . +COPY --from=deps-dev $APP_PATH/node_modules ./node_modules +RUN yarn build -RUN yarn build && \ - yarn --production --ignore-scripts --prefer-offline && \ - rm -rf shared && \ - rm -rf app +# --- +FROM node:14-alpine AS runner +WORKDIR $APP_PATH ENV NODE_ENV production -CMD yarn start + +COPY --from=builder $APP_PATH/build ./build +COPY --from=deps-prod $APP_PATH/node_modules ./node_modules +COPY --from=builder $APP_PATH/package.json ./package.json + +RUN addgroup -g 1001 -S nodejs && \ + adduser -S nodejs -u 1001 && \ + chown -R nodejs:nodejs $APP_PATH/build + +USER nodejs EXPOSE 3000 +CMD ["yarn", "start"] From 2d2626d1752896839174cc8e68d959efb6d4c270 Mon Sep 17 00:00:00 2001 From: Jack Baron Date: Sat, 17 Jul 2021 13:49:42 +0100 Subject: [PATCH 2/5] fix: mark yarn-deduplicate as a required dep `yarn --production` will fail on a clean install otherwise --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 6dbbbac4c8..b7b7937ddd 100644 --- a/package.json +++ b/package.json @@ -165,7 +165,8 @@ "turndown": "^7.1.1", "utf8": "^3.0.0", "uuid": "^8.3.2", - "validator": "5.2.0" + "validator": "5.2.0", + "yarn-deduplicate": "^3.1.0" }, "devDependencies": { "@babel/cli": "^7.10.5", @@ -203,8 +204,7 @@ "webpack-cli": "^3.3.12", "webpack-manifest-plugin": "^3.0.0", "webpack-pwa-manifest": "^4.3.0", - "workbox-webpack-plugin": "^6.1.0", - "yarn-deduplicate": "^3.1.0" + "workbox-webpack-plugin": "^6.1.0" }, "resolutions": { "prosemirror-view": "1.18.1", From cba3382b958de8656ea2b5062bd6de7fa45cdfee Mon Sep 17 00:00:00 2001 From: Jack Baron Date: Sat, 17 Jul 2021 23:57:30 +0100 Subject: [PATCH 3/5] fix: add sequelize required files for migrations --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index ba388d8f64..2bc18b44ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,6 +30,8 @@ WORKDIR $APP_PATH ENV NODE_ENV production COPY --from=builder $APP_PATH/build ./build +COPY --from=builder $APP_PATH/server ./server +COPY --from=builder $APP_PATH/.sequelizerc ./.sequelizerc COPY --from=deps-prod $APP_PATH/node_modules ./node_modules COPY --from=builder $APP_PATH/package.json ./package.json From a3dbec50918dc759b267987822d2ad71fa151d06 Mon Sep 17 00:00:00 2001 From: Jack Baron Date: Sun, 18 Jul 2021 00:52:17 +0100 Subject: [PATCH 4/5] fix: use correct ARG syntax for multistage builds --- Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 2bc18b44ac..d6a918c1f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ # syntax=docker/dockerfile:1.2 +ARG APP_PATH=/opt/outline FROM node:14-alpine AS deps-common -ARG APP_PATH /opt/outline +ARG APP_PATH WORKDIR $APP_PATH COPY ./package.json ./yarn.lock ./ @@ -17,6 +18,8 @@ RUN yarn install --production=true --frozen-lockfile && \ # --- FROM node:14-alpine AS builder + +ARG APP_PATH WORKDIR $APP_PATH COPY . . @@ -26,6 +29,7 @@ RUN yarn build # --- FROM node:14-alpine AS runner +ARG APP_PATH WORKDIR $APP_PATH ENV NODE_ENV production From 7585b54adba60b7693cc17d2ed2ee1dc65a8cca2 Mon Sep 17 00:00:00 2001 From: Jack Baron Date: Mon, 19 Jul 2021 22:48:29 +0100 Subject: [PATCH 5/5] revert: mark yarn-deduplicate as a required dep no longer required as of 0b3adad7515abe1f9997ea7470783394b28baff3 --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index b7b7937ddd..6dbbbac4c8 100644 --- a/package.json +++ b/package.json @@ -165,8 +165,7 @@ "turndown": "^7.1.1", "utf8": "^3.0.0", "uuid": "^8.3.2", - "validator": "5.2.0", - "yarn-deduplicate": "^3.1.0" + "validator": "5.2.0" }, "devDependencies": { "@babel/cli": "^7.10.5", @@ -204,7 +203,8 @@ "webpack-cli": "^3.3.12", "webpack-manifest-plugin": "^3.0.0", "webpack-pwa-manifest": "^4.3.0", - "workbox-webpack-plugin": "^6.1.0" + "workbox-webpack-plugin": "^6.1.0", + "yarn-deduplicate": "^3.1.0" }, "resolutions": { "prosemirror-view": "1.18.1",