Skip to content

Commit

Permalink
fix: improve dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
masb0ymas committed Dec 10, 2022
1 parent 0b61730 commit cdb3dbf
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 104 deletions.
79 changes: 46 additions & 33 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
84 changes: 84 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -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"]
71 changes: 0 additions & 71 deletions dev.Dockerfile

This file was deleted.

0 comments on commit cdb3dbf

Please sign in to comment.