From 1b90539248104fe33d88718c086efb57504c2255 Mon Sep 17 00:00:00 2001 From: Gary Lo Date: Sun, 28 Jun 2020 23:41:22 +0800 Subject: [PATCH 1/2] add SB02 solution --- SB02-docker-build/repo/.dockerignore | 13 +++++++++++++ SB02-docker-build/repo/Dockerfile | 21 +++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 SB02-docker-build/repo/.dockerignore diff --git a/SB02-docker-build/repo/.dockerignore b/SB02-docker-build/repo/.dockerignore new file mode 100644 index 0000000..e8f765d --- /dev/null +++ b/SB02-docker-build/repo/.dockerignore @@ -0,0 +1,13 @@ +# Commmon +**/.git +**/.gitignore +**/README.md +**/.idea +**/.vscode +**/*.log + +# NodeJs Specific +**/node_modules + +# Project Specific +.next diff --git a/SB02-docker-build/repo/Dockerfile b/SB02-docker-build/repo/Dockerfile index bac701a..f52dbda 100644 --- a/SB02-docker-build/repo/Dockerfile +++ b/SB02-docker-build/repo/Dockerfile @@ -1,18 +1,27 @@ -FROM node:12 +# Use a more specific version for your production containers can help resolve issues +FROM node:12.18.1-alpine3.12 # Create app directory WORKDIR /usr/src/app # Installing dependencies -COPY package*.json ./ +COPY package.json yarn.lock ./ -RUN npm install +# Should use same package management as same as your development workflow +# The dependencies lock file is critical for reproduce to same behaviour +RUN yarn install --frozen-lockfile + +# Copying Assets files +COPY public ./public # Copying source files -COPY . . +COPY pages ./pages # Building app -RUN npm run build +RUN yarn build + +# This don't actually publish the port but to give better documentation to your DevOps colleagues +EXPOSE 3000 # Running the app -CMD [ "npm", "start" ] +CMD [ "yarn", "start" ] From 8f2cb367861244992ff37479bda3dace357b8c4e Mon Sep 17 00:00:00 2001 From: Gary Lo Date: Mon, 29 Jun 2020 00:57:22 +0800 Subject: [PATCH 2/2] add multi stage build optimization --- SB02-docker-build/repo/Dockerfile | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/SB02-docker-build/repo/Dockerfile b/SB02-docker-build/repo/Dockerfile index f52dbda..0c0e05e 100644 --- a/SB02-docker-build/repo/Dockerfile +++ b/SB02-docker-build/repo/Dockerfile @@ -1,5 +1,5 @@ # Use a more specific version for your production containers can help resolve issues -FROM node:12.18.1-alpine3.12 +FROM node:12.18.1-alpine3.12 as FirstStage # Create app directory WORKDIR /usr/src/app @@ -17,8 +17,18 @@ COPY public ./public # Copying source files COPY pages ./pages -# Building app -RUN yarn build +# 1. Building app +# 2. Remove Dev Dependencies +RUN yarn build \ + && yarn install --production --frozen-lockfile --offline + +# Final Stage +FROM node:12.18.1-alpine3.12 + +WORKDIR /usr/src/app + +# Copy all files from builder +COPY --from=FirstStage /usr/src/app /usr/src/app # This don't actually publish the port but to give better documentation to your DevOps colleagues EXPOSE 3000