From cdb3dbf13058ce4cb088ab5c41d6890128b882ab Mon Sep 17 00:00:00 2001 From: masb0ymas Date: Sat, 10 Dec 2022 18:21:50 +0700 Subject: [PATCH] fix: improve dockerfile --- Dockerfile | 79 +++++++++++++++++++++++++++-------------------- Dockerfile.dev | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ dev.Dockerfile | 71 ------------------------------------------ 3 files changed, 130 insertions(+), 104 deletions(-) create mode 100644 Dockerfile.dev delete mode 100644 dev.Dockerfile diff --git a/Dockerfile b/Dockerfile index 2508c6b2..a86ac69e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ # Install dependencies only when needed -FROM node:14-alpine as deps +FROM node:16.18-alpine as deps LABEL author="masb0ymas" +LABEL name="expresso" # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. RUN apk add --no-cache libc6-compat @@ -11,60 +12,72 @@ RUN npm install --quiet node-gyp -g # install for sharp library RUN apk add --update --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community --repository http://dl-3.alpinelinux.org/alpine/edge/main vips-dev -# Setup Timezone -RUN apk add tzdata -ENV TZ=Asia/Jakarta +# Set the Temp Working Directory inside the container +WORKDIR /temp-deps -RUN apk add nano +# copy package json +COPY ["package.json", "yarn.lock", "./"] -WORKDIR /app -COPY package.json ./ RUN yarn install --frozen-lockfile -# Rebuild the source code only when needed -FROM node:14-alpine AS builder -WORKDIR /app +FROM node:16.18-alpine as build_base +LABEL author="masb0ymas" +LABEL name="expresso" + +# Set the Temp Working Directory inside the container +WORKDIR /temp-build RUN export NODE_OPTIONS=\"--max_old_space_size=4096\" +# copy base code COPY . . +# copy environment RUN cp .env.docker-production .env -COPY --from=deps /app/node_modules ./node_modules +COPY --from=deps /temp-deps/node_modules ./node_modules + +# prune devDependencies RUN yarn build && yarn install --production --ignore-scripts --prefer-offline -# Production image, copy all the files and run next -FROM node:14-alpine AS runner +# image runner app +FROM node:16.18-alpine as runner +LABEL author="masb0ymas" +LABEL name="expresso" + +RUN apk add ca-certificates + +# install global package for runner +RUN npm config set scripts-prepend-node-path true +RUN npm i -g typescript +RUN npm i -g pm2 + +# Set the Current Working Directory inside the container WORKDIR /app ENV NODE_ENV production -# RUN addgroup -g 1001 -S nodejs -# RUN adduser -S expresso -u 1001 +# Setup Timezone +RUN apk add tzdata +ENV TZ=Asia/Jakarta -# Set config npm & install dependencies -RUN npm config set scripts-prepend-node-path true -RUN npm install -g typescript -RUN npm install -g pm2 - -# You only need to copy next.config.js if you are NOT using the default configuration -COPY --from=builder /app/public ./public -COPY --from=builder /app/node_modules ./node_modules -COPY --from=builder /app/package.json ./package.json -COPY --from=builder /app/tsconfig.json ./tsconfig.json -COPY --from=builder /app/.sequelizerc ./.sequelizerc -COPY --from=builder /app/.swcrc ./.swcrc -COPY --from=builder /app/logs ./logs -COPY --from=builder /app/dist ./dist -COPY --from=builder /app/src ./src -COPY --from=builder /app/.env ./.env +# editor cli with nano +RUN apk add nano + +COPY --from=build_base /temp-build/public ./public +COPY --from=build_base /temp-build/node_modules ./node_modules +COPY --from=build_base /temp-build/package.json ./package.json +COPY --from=build_base /temp-build/tsconfig.json ./tsconfig.json +COPY --from=build_base /temp-build/.swcrc ./.swcrc +COPY --from=build_base /temp-build/logs ./logs +COPY --from=build_base /temp-build/dist ./dist +COPY --from=build_base /temp-build/src ./src +COPY --from=build_base /temp-build/.env ./.env # initial app RUN node ./dist/scripts/generate.js -# USER expresso - +# This container exposes port 8000 to the outside world EXPOSE 8000 # Run for production diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 00000000..cb319a6e --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,84 @@ +# Install dependencies only when needed +FROM node:16.18-alpine as deps +LABEL author="masb0ymas" +LABEL name="expresso" + +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +RUN apk add --update --no-cache curl py-pip +RUN apk add --no-cache make python3 g++ gcc libgcc libstdc++ +RUN npm install --quiet node-gyp -g + +# install for sharp library +RUN apk add --update --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community --repository http://dl-3.alpinelinux.org/alpine/edge/main vips-dev + +# Set the Temp Working Directory inside the container +WORKDIR /temp-deps + +# copy package json +COPY ["package.json", "yarn.lock", "./"] + +RUN yarn install --frozen-lockfile + +FROM node:16.18-alpine as build_base +LABEL author="masb0ymas" +LABEL name="expresso" + +# Set the Temp Working Directory inside the container +WORKDIR /temp-build + +RUN export NODE_OPTIONS=\"--max_old_space_size=4096\" + +# copy base code +COPY . . + +# copy environment +RUN cp .env.docker-staging .env + +COPY --from=deps /temp-deps/node_modules ./node_modules + +# prune devDependencies +RUN yarn build && yarn install --production --ignore-scripts --prefer-offline + +# image runner app +FROM node:16.18-alpine as runner +LABEL author="masb0ymas" +LABEL name="expresso" + +RUN apk add ca-certificates + +# install global package for runner +RUN npm config set scripts-prepend-node-path true +RUN npm i -g typescript +RUN npm i -g pm2 + +# Set the Current Working Directory inside the container +WORKDIR /app + +ENV NODE_ENV staging + +# Setup Timezone +RUN apk add tzdata +ENV TZ=Asia/Jakarta + +# editor cli with nano +RUN apk add nano + +COPY --from=build_base /temp-build/public ./public +COPY --from=build_base /temp-build/node_modules ./node_modules +COPY --from=build_base /temp-build/package.json ./package.json +COPY --from=build_base /temp-build/tsconfig.json ./tsconfig.json +COPY --from=build_base /temp-build/.swcrc ./.swcrc +COPY --from=build_base /temp-build/logs ./logs +COPY --from=build_base /temp-build/dist ./dist +COPY --from=build_base /temp-build/src ./src +COPY --from=build_base /temp-build/.env ./.env + +# initial app +RUN node ./dist/scripts/generate.js + +# This container exposes port 8000 to the outside world +EXPOSE 8000 + +# Run for staging +CMD ["yarn", "serve:staging-docker"] diff --git a/dev.Dockerfile b/dev.Dockerfile deleted file mode 100644 index cad47883..00000000 --- a/dev.Dockerfile +++ /dev/null @@ -1,71 +0,0 @@ -# Install dependencies only when needed -FROM node:14-alpine as deps -LABEL author="masb0ymas" - -# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. -RUN apk add --no-cache libc6-compat -RUN apk add --update --no-cache curl py-pip -RUN apk add --no-cache make python3 g++ gcc libgcc libstdc++ -RUN npm install --quiet node-gyp -g - -# install for sharp library -RUN apk add --update --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community --repository http://dl-3.alpinelinux.org/alpine/edge/main vips-dev - -# Setup Timezone -RUN apk add tzdata -ENV TZ=Asia/Jakarta - -RUN apk add nano - -WORKDIR /app -COPY package.json ./ -RUN yarn install --frozen-lockfile - -# Rebuild the source code only when needed -FROM node:14-alpine AS builder -WORKDIR /app - -RUN export NODE_OPTIONS=\"--max_old_space_size=4096\" - -COPY . . - -RUN cp .env.docker-staging .env - -COPY --from=deps /app/node_modules ./node_modules -RUN yarn build && yarn install --production --ignore-scripts --prefer-offline - -# Production image, copy all the files and run next -FROM node:14-alpine AS runner -WORKDIR /app - -ENV NODE_ENV staging - -# RUN addgroup -g 1001 -S nodejs -# RUN adduser -S expresso -u 1001 - -# Set config npm & install dependencies -RUN npm config set scripts-prepend-node-path true -RUN npm install -g typescript -RUN npm install -g pm2 - -# You only need to copy next.config.js if you are NOT using the default configuration -COPY --from=builder /app/public ./public -COPY --from=builder /app/node_modules ./node_modules -COPY --from=builder /app/package.json ./package.json -COPY --from=builder /app/tsconfig.json ./tsconfig.json -COPY --from=builder /app/.sequelizerc ./.sequelizerc -COPY --from=builder /app/.swcrc ./.swcrc -COPY --from=builder /app/logs ./logs -COPY --from=builder /app/dist ./dist -COPY --from=builder /app/src ./src -COPY --from=builder /app/.env ./.env - -# initial app -RUN node ./dist/scripts/generate.js - -# USER expresso - -EXPOSE 8000 - -# Run for staging -CMD ["yarn", "serve:staging-docker"]