Skip to content

Commit

Permalink
📦 Improve docker build process
Browse files Browse the repository at this point in the history
Reduce docker image size
  • Loading branch information
wei committed Nov 23, 2024
1 parent fe9dc3d commit 76ef894
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/wicked-colts-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"socialify": patch
---

Improve docker build process
Reduce docker image size
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ yarn-error.log*

.dockerignore
Dockerfile
.*
.*
*.md
48 changes: 38 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
# Source: https://github.com/vercel/next.js/blob/2161d8c012dcd98eb8690814bd275d56c45bf00a/examples/with-docker/Dockerfile

FROM node:22-alpine AS base

# Stage 1: Install dependencies
FROM base AS deps
WORKDIR /app
COPY package.json yarn.lock ./
# postinstall script requires a public directory
RUN mkdir -p /app/public && \
yarn install --frozen-lockfile

# Stage 2: Build the application
FROM base AS builder

ENV NODE_ENV=production
ENV CI=true
ENV NEXT_TELEMETRY_DISABLED=1

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .
COPY --from=deps /app/public ./public

RUN yarn build

# Stage 3: Prepare the production image
FROM base AS runner

ENV GITHUB_TOKEN=
ENV PROJECT_URL=http://localhost:3000
ENV GTM_ID=

ENV PORT=3000
ENV NODE_ENV=production
ENV CI=true
ENV NEXT_TELEMETRY_DISABLED=1

WORKDIR /app

COPY package.json yarn.lock* ./
COPY public ./public/
RUN yarn --frozen-lockfile
# Copy only the necessary files and install production dependencies
COPY package.json yarn.lock ./
RUN mkdir -p /app/public && \
yarn install --production --frozen-lockfile && \
yarn cache clean

COPY . .

RUN yarn build

CMD ["yarn", "start"]
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/next.config.js /app/custom-rewrites.js ./

EXPOSE 3000

CMD ["yarn", "start"]

0 comments on commit 76ef894

Please sign in to comment.