From c4b5871f996d6ea8c5eb0c18b5bb90ed4c4d9fdf Mon Sep 17 00:00:00 2001 From: Kyle2142 <19349418+Kyle2142@users.noreply.github.com> Date: Sun, 22 Nov 2020 11:49:00 +0200 Subject: [PATCH 1/4] Use alpine image to reduce docker image size --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ac72154..8fbad4c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # ottomated/crewlink-server -FROM node:14 +FROM node:14-alpine # Make a directory for the app, give node user permissions RUN mkdir /app && chown node:node /app From 2b1065c3b413b8067b422413afedae481b7a1f17 Mon Sep 17 00:00:00 2001 From: Kyle2142 <19349418+Kyle2142@users.noreply.github.com> Date: Sat, 28 Nov 2020 01:59:04 +0200 Subject: [PATCH 2/4] Update Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 8fbad4c..84cd0df 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # ottomated/crewlink-server -FROM node:14-alpine +FROM node:lts-alpine # Make a directory for the app, give node user permissions RUN mkdir /app && chown node:node /app From 59e22d9b15a21401b49ce0d0cc83213e7690c37b Mon Sep 17 00:00:00 2001 From: Kyle2142 <19349418+Kyle2142@users.noreply.github.com> Date: Sat, 28 Nov 2020 02:26:23 +0200 Subject: [PATCH 3/4] Reduce image size using multi stage build --- Dockerfile | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 84cd0df..1a3680a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,13 @@ # ottomated/crewlink-server -FROM node:lts-alpine -# Make a directory for the app, give node user permissions -RUN mkdir /app && chown node:node /app +# Stage 1, generate files separately so we only bring over what is necessary +FROM node:lts-alpine as builder -# Change to the /app directory *and* make it the default execution directory +# Set working directory for subsequent commands WORKDIR /app -# Do all remaining actions as node, and start the image as node -USER node - # Copy the repo contents from the build context into the image -COPY ./ /app/ +COPY . . # Install NPM packages RUN yarn install @@ -19,6 +15,17 @@ RUN yarn install # Compile project RUN yarn compile + +# Stage 2, output image +FROM node:lts-alpine + +# Change to the /app directory *and* make it the default execution directory +WORKDIR /app + # Copy relevant files from previous stage +COPY --chown=node:node --from=builder /app/dist ./dist COPY --chown=node:node --from=builder /app/node_modules ./node_modules + # Start the image as node +USER node + # Tell the Docker engine the default port is 9736 EXPOSE 9736 From 675f69c96f952c0cdb13cd9eaf78baae7ac00c25 Mon Sep 17 00:00:00 2001 From: Kyle2142 <19349418+Kyle2142@users.noreply.github.com> Date: Sat, 28 Nov 2020 02:29:06 +0200 Subject: [PATCH 4/4] Fix copy-paste --- Dockerfile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1a3680a..f131bc1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,9 +21,12 @@ FROM node:lts-alpine # Change to the /app directory *and* make it the default execution directory WORKDIR /app - # Copy relevant files from previous stage -COPY --chown=node:node --from=builder /app/dist ./dist COPY --chown=node:node --from=builder /app/node_modules ./node_modules - # Start the image as node + +# Copy relevant files from previous stage +COPY --chown=node:node --from=builder /app/dist ./dist +COPY --chown=node:node --from=builder /app/node_modules ./node_modules + +# Start the image as node USER node # Tell the Docker engine the default port is 9736